Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

Commit 2ea1480

Browse files
authored
Merge pull request #8 from jeckel/rest-json-checks
Rest json checks
2 parents 2599040 + 9db22e0 commit 2ea1480

11 files changed

+479
-6
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ phpmd:
1616

1717
codecept:
1818
@${PHP} ./vendor/bin/codecept run --coverage
19+
20+
test: codecept

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"require": {
2424
"php": ">=7.1",
25+
"ext-json": "*",
2526
"codeception/codeception": "^2.5",
2627
"behat/behat": "^3.5"
2728
},

src/AssertContext.php

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Jeckel\Gherkin;
44

5+
use Codeception\Util\Fixtures;
6+
57
/**
68
* Class AssertContext
79
* @package Jeckel\Gherkin
@@ -94,4 +96,13 @@ public function iShouldSeeNotEquals($arg1, $arg2)
9496
{
9597
$this->assertNotEquals($arg1, $arg2);
9698
}
99+
100+
/**
101+
* @Given I clear fixtures
102+
* @SuppressWarnings(PHPMD.StaticAccess)
103+
*/
104+
public function clearFixtures()
105+
{
106+
Fixtures::cleanup();
107+
}
97108
}

src/FilePath/FileHelper.php

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Jeckel\Gherkin\FilePath;
4+
5+
use Codeception\Configuration;
6+
use Codeception\Module;
7+
8+
/**
9+
* Class FileHelper
10+
* @package Jeckel\Gherkin\FilePath
11+
*/
12+
class FileHelper extends Module
13+
{
14+
const PATH_TO_PROJECT = 'project';
15+
const PATH_TO_DATA = 'data';
16+
const PATH_TO_SUPPORT = 'support';
17+
18+
/** @var string */
19+
private $projectDir;
20+
21+
/** @var string */
22+
private $dataDir;
23+
24+
/** @var string */
25+
private $supportDir;
26+
27+
/**
28+
* @param string $filepath
29+
* @param string $pathTo
30+
* @return string
31+
*/
32+
public function getAbsolutePathTo(string $filepath, string $pathTo = ''): string
33+
{
34+
return $this->getBasePath($pathTo) . $filepath;
35+
}
36+
37+
/**
38+
* @param string $pathTo
39+
* @return string
40+
*/
41+
private function getBasePath(string $pathTo)
42+
{
43+
switch ($pathTo) {
44+
case FileHelper::PATH_TO_PROJECT:
45+
return $this->getProjectDir();
46+
case FileHelper::PATH_TO_DATA:
47+
return $this->getDataDir();
48+
case FileHelper::PATH_TO_SUPPORT:
49+
return $this->getSupportDir();
50+
}
51+
return '';
52+
}
53+
54+
/**
55+
* @return string
56+
* @SuppressWarnings(PHPMD.StaticAccess)
57+
*/
58+
public function getProjectDir()
59+
{
60+
if (empty($this->projectDir)) {
61+
return Configuration::projectDir();
62+
}
63+
return $this->projectDir;
64+
}
65+
66+
/**
67+
* @param string $projectDir
68+
* @return self
69+
*/
70+
public function setProjectDir(string $projectDir): self
71+
{
72+
$this->projectDir = $projectDir;
73+
return $this;
74+
}
75+
76+
/**
77+
* @return string
78+
* @SuppressWarnings(PHPMD.StaticAccess)
79+
*/
80+
public function getDataDir(): string
81+
{
82+
if (empty($this->dataDir)) {
83+
return Configuration::dataDir();
84+
}
85+
return $this->dataDir;
86+
}
87+
88+
/**
89+
* @param string $dataDir
90+
* @return self
91+
*/
92+
public function setDataDir(string $dataDir): self
93+
{
94+
$this->dataDir = $dataDir;
95+
return $this;
96+
}
97+
98+
/**
99+
* @return string
100+
* @SuppressWarnings(PHPMD.StaticAccess)
101+
*/
102+
public function getSupportDir(): string
103+
{
104+
if (empty($this->supportDir)) {
105+
return Configuration::supportDir();
106+
}
107+
return $this->supportDir;
108+
}
109+
110+
/**
111+
* @param string $supportDir
112+
* @return self
113+
*/
114+
public function setSupportDir(string $supportDir): self
115+
{
116+
$this->supportDir = $supportDir;
117+
return $this;
118+
}
119+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Jeckel\Gherkin\FilePath;
4+
5+
/**
6+
* Interface FileHelperAwareInterface
7+
* @package Jeckel\Gherkin\FilePath
8+
*/
9+
interface FileHelperAwareInterface
10+
{
11+
public function getFileHelper(): FileHelper;
12+
}

src/FilePath/FileHelperAwareTrait.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Jeckel\Gherkin\FilePath;
4+
5+
use Codeception\Lib\ModuleContainer;
6+
7+
/**
8+
* Trait FileHelperAwareTrait
9+
* @package Jeckel\Gherkin\FilePath
10+
*/
11+
trait FileHelperAwareTrait
12+
{
13+
/** @var FileHelper */
14+
protected $fileHelper;
15+
16+
/**
17+
* @return FileHelper
18+
*/
19+
public function getFileHelper(): FileHelper
20+
{
21+
if (empty($this->fileHelper)) {
22+
/** @var ModuleContainer */
23+
$moduleContainer = $this->moduleContainer;
24+
25+
if (!$moduleContainer->hasModule(FileHelper::class)) {
26+
$moduleContainer->create(FileHelper::class);
27+
}
28+
/** @var FileHelper $module */
29+
$module = $moduleContainer->getModule(FileHelper::class);
30+
$this->fileHelper = $module;
31+
}
32+
return $this->fileHelper;
33+
}
34+
35+
/**
36+
* @param FileHelper $fileHelper
37+
* @return self
38+
*/
39+
public function setFileHelper(FileHelper $fileHelper): self
40+
{
41+
$this->fileHelper = $fileHelper;
42+
return $this;
43+
}
44+
}

src/RestContext.php

+43-6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
namespace Jeckel\Gherkin;
44

55
use Behat\Gherkin\Node\TableNode;
6+
use Codeception\Configuration;
67
use Codeception\Lib\Interfaces\DependsOnModule;
8+
use Codeception\Lib\ModuleContainer;
79
use Codeception\Module\REST;
810
use Codeception\Util\Fixtures;
911
use Exception;
12+
use Jeckel\Gherkin\FilePath\FileHelper;
13+
use Jeckel\Gherkin\FilePath\FileHelperAwareInterface;
14+
use Jeckel\Gherkin\FilePath\FileHelperAwareTrait;
1015

1116
/**
1217
* Class RestHelper
1318
* @package Jeckel\Gherkin
1419
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
1520
*/
16-
class RestContext extends ContextAbstract implements DependsOnModule
21+
class RestContext extends ContextAbstract implements DependsOnModule, FileHelperAwareInterface
1722
{
23+
use FileHelperAwareTrait;
24+
1825
/**
1926
* Allows to explicitly set what methods have this class.
2027
*
@@ -28,7 +35,6 @@ class RestContext extends ContextAbstract implements DependsOnModule
2835
protected $rest;
2936

3037
// phpcs:disable
31-
3238
/**
3339
* @return array
3440
*/
@@ -148,12 +154,43 @@ public function iShouldSeeResponseJsonMatchesJsonPath(string $path)
148154
}
149155

150156
/**
151-
* @Then the JSON should be equal to :json
152-
* @param string $json
157+
* @Then I should see response contains json :arg1
158+
* @param string $arg1
159+
*/
160+
public function iShouldSeeResponseContainsJson(string $arg1)
161+
{
162+
$this->checkResponseContainsJson($arg1);
163+
}
164+
165+
/**
166+
* @Then I should see response contains json from file :filepath
167+
* @param string $filepath
168+
*/
169+
public function iShouldSeeResponseContainsJSONFromFile(string $filepath)
170+
{
171+
$fileContent = file_get_contents(
172+
$this->getFileHelper()->getAbsolutePathTo($filepath, FileHelper::PATH_TO_DATA)
173+
);
174+
if (! $fileContent) {
175+
throw new \InvalidArgumentException(sprintf('Enable to open file %s', $filepath));
176+
}
177+
$this->checkResponseContainsJson($fileContent);
178+
}
179+
180+
/**
181+
* @param string $jsonString
153182
*/
154-
public function theJSONShouldBeEqualTo(string $json)
183+
protected function checkResponseContainsJson(string $jsonString)
155184
{
156-
$this->rest->seeResponseEquals($json);
185+
var_dump($jsonString);
186+
$json = json_decode($jsonString, true);
187+
188+
if (null === $json && json_last_error() != JSON_ERROR_NONE) {
189+
throw new \InvalidArgumentException(
190+
sprintf('Argument provided is not valid JSON: %s', json_last_error_msg())
191+
);
192+
}
193+
$this->rest->seeResponseContainsJson($json);
157194
}
158195

159196
/**

tests/unit/AssertTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* User: jeckel
4+
* Date: 23/04/19
5+
* Time: 14:42
6+
*/
7+
namespace Test\Jeckel\Gherkin;
8+
9+
use Codeception\Lib\ModuleContainer;
10+
use Codeception\Util\Fixtures;
11+
use Jeckel\Gherkin\AssertContext;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
14+
class AssertTest extends \Codeception\Test\Unit
15+
{
16+
/** @var AssertContext */
17+
protected $helper;
18+
19+
/**
20+
* Setup
21+
*/
22+
public function setUp()
23+
{
24+
/** @var MockObject | ModuleContainer $moduleContainer */
25+
$moduleContainer = $this->createMock(ModuleContainer::class);
26+
27+
$this->helper = new AssertContext($moduleContainer);
28+
return parent::setUp(); // TODO: Change the autogenerated stub
29+
}
30+
31+
/**
32+
* @test clearFixtures
33+
*/
34+
public function testClearFixtures()
35+
{
36+
$data = new \stdClass();
37+
Fixtures::add('foo', $data);
38+
$this->assertSame($data, Fixtures::get('foo'));
39+
40+
$this->helper->clearFixtures();
41+
42+
$this->assertFalse(Fixtures::exists('foo'));
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* User: jeckel
4+
* Date: 23/04/19
5+
* Time: 17:56
6+
*/
7+
namespace Test\Jeckel\Gherkin\FilePath;
8+
9+
use Codeception\Lib\ModuleContainer;
10+
use Codeception\Test\Unit;
11+
use Jeckel\Gherkin\FilePath\FileHelper;
12+
use Jeckel\Gherkin\FilePath\FileHelperAwareTrait;
13+
14+
class FileHelperAwareTest extends Unit
15+
{
16+
public function testSetGetFileHelper()
17+
{
18+
$module = $this->getObjectForTrait(FileHelperAwareTrait::class);
19+
20+
/** @var FileHelper $fileHelper */
21+
$fileHelper = $this->createMock(FileHelper::class);
22+
23+
$this->assertSame($module, $module->setFileHelper($fileHelper));
24+
$this->assertSame($fileHelper, $module->getFileHelper());
25+
}
26+
27+
public function testGetFileHelperFromModuleContainer()
28+
{
29+
$module = $this->getObjectForTrait(FileHelperAwareTrait::class);
30+
31+
$fileHelper = $this->createMock(FileHelper::class);
32+
33+
$moduleContainer = $this->createMock(ModuleContainer::class);
34+
$module->moduleContainer = $moduleContainer;
35+
36+
$moduleContainer->expects($this->at(0))
37+
->method('hasModule')
38+
->with(FileHelper::class)
39+
->willReturn(false);
40+
$moduleContainer->expects($this->at(1))
41+
->method('create')
42+
->with(FileHelper::class);
43+
$moduleContainer->expects($this->at(2))
44+
->method('getModule')
45+
->with(FileHelper::class)
46+
->willReturn($fileHelper);
47+
48+
$this->assertSame($fileHelper, $module->getFileHelper());
49+
}
50+
}

0 commit comments

Comments
 (0)