-
-
Notifications
You must be signed in to change notification settings - Fork 844
/
JsonApiContext.php
188 lines (149 loc) · 4.58 KB
/
JsonApiContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behatch\Context\RestContext;
use Behatch\Json\Json;
use Behatch\Json\JsonInspector;
use Doctrine\Common\Persistence\ManagerRegistry;
final class JsonApiContext implements Context
{
private $restContext;
private $inspector;
private $doctrine;
/**
* @var \Doctrine\Common\Persistence\ObjectManager
*/
private $manager;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
$this->manager = $doctrine->getManager();
}
/**
* Gives access to the Behatch context.
*
* @param BeforeScenarioScope $scope
*
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
/** @var InitializedContextEnvironment $environment */
$environment = $scope->getEnvironment();
$this->restContext = $environment->getContext(RestContext::class);
$this->inspector = new JsonInspector('javascript');
}
/**
* @Then I save the response
*/
public function iSaveTheResponse()
{
$content = $this->getContent();
if (null === ($decoded = json_decode($content))) {
throw new \RuntimeException('JSON response seems to be invalid');
}
$fileName = __DIR__.'/response.json';
file_put_contents($fileName, $content);
return $fileName;
}
/**
* @Then I validate it with jsonapi-validator
*/
public function iValidateItWithJsonapiValidator()
{
$fileName = $this->iSaveTheResponse();
$validationResponse = exec(sprintf('cd %s && jsonapi-validator -f response.json', __DIR__));
$isValidJsonapi = 'response.json is valid JSON API.' === $validationResponse;
unlink($fileName);
if (!$isValidJsonapi) {
throw new \RuntimeException('JSON response seems to be invalid JSON API');
}
}
/**
* Checks that given JSON node is equal to an empty array.
*
* @Then the JSON node :node should be an empty array
*/
public function theJsonNodeShouldBeAnEmptyArray($node)
{
$actual = $this->getValueOfNode($node);
if (!is_array($actual) || !empty($actual)) {
throw new \Exception(
sprintf("The node value is '%s'", json_encode($actual))
);
}
}
/**
* Checks that given JSON node is a number.
*
* @Then the JSON node :node should be a number
*/
public function theJsonNodeShouldBeANumber($node)
{
$actual = $this->getValueOfNode($node);
if (!is_numeric($actual)) {
throw new \Exception(
sprintf('The node value is `%s`', json_encode($actual))
);
}
}
/**
* Checks that given JSON node is not an empty string.
*
* @Then the JSON node :node should not be an empty string
*/
public function theJsonNodeShouldNotBeAnEmptyString($node)
{
$actual = $this->getValueOfNode($node);
if ($actual === '') {
throw new \Exception(
sprintf('The node value is `%s`', json_encode($actual))
);
}
}
private function getValueOfNode($node)
{
$json = $this->getJson();
return $this->inspector->evaluate($json, $node);
}
private function getJson()
{
return new Json($this->getContent());
}
private function getContent()
{
return $this->restContext->getMink()->getSession()->getDriver()->getContent();
}
/**
* @Given there is a RelatedDummy
*/
public function thereIsARelatedDummy()
{
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy with friends');
$this->manager->persist($relatedDummy);
$this->manager->flush();
}
/**
* @Given there is a DummyFriend
*/
public function thereIsADummyFriend()
{
$friend = new DummyFriend();
$friend->setName('DummyFriend');
$this->manager->persist($friend);
$this->manager->flush();
}
}