11
11
12
12
declare (strict_types=1 );
13
13
14
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \CircularReference ;
14
15
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \DummyFriend ;
15
16
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \RelatedDummy ;
16
17
use Behat \Behat \Context \Context ;
17
- use Behat \Behat \Context \Environment \InitializedContextEnvironment ;
18
18
use Behat \Behat \Hook \Scope \BeforeScenarioScope ;
19
19
use Behatch \Context \RestContext ;
20
20
use Behatch \Json \Json ;
21
21
use Behatch \Json \JsonInspector ;
22
+ use Behatch \Json \JsonSchema ;
22
23
use Doctrine \Common \Persistence \ManagerRegistry ;
24
+ use JsonSchema \RefResolver ;
25
+ use JsonSchema \Uri \UriRetriever ;
26
+ use JsonSchema \Validator ;
23
27
24
28
final class JsonApiContext implements Context
25
29
{
26
- private $ restContext ;
27
-
28
30
private $ inspector ;
29
-
30
- private $ doctrine ;
31
-
32
- /**
33
- * @var \Doctrine\Common\Persistence\ObjectManager
34
- */
31
+ private $ jsonApiSchemaFile ;
32
+ private $ restContext ;
35
33
private $ manager ;
36
34
37
- public function __construct (ManagerRegistry $ doctrine )
35
+ public function __construct (ManagerRegistry $ doctrine, string $ jsonApiSchemaFile )
38
36
{
39
- $ this ->doctrine = $ doctrine ;
37
+ if (!is_file ($ jsonApiSchemaFile )) {
38
+ throw new \InvalidArgumentException ('The JSON API schema doesn \'t exist. ' );
39
+ }
40
+
41
+ $ this ->inspector = new JsonInspector ('javascript ' );
42
+ $ this ->jsonApiSchemaFile = $ jsonApiSchemaFile ;
40
43
$ this ->manager = $ doctrine ->getManager ();
41
44
}
42
45
@@ -49,48 +52,21 @@ public function __construct(ManagerRegistry $doctrine)
49
52
*/
50
53
public function gatherContexts (BeforeScenarioScope $ scope )
51
54
{
52
- /** @var InitializedContextEnvironment $environment */
53
- $ environment = $ scope ->getEnvironment ();
54
-
55
- $ this ->restContext = $ environment ->getContext (RestContext::class);
56
-
57
- $ this ->inspector = new JsonInspector ('javascript ' );
55
+ $ this ->restContext = $ scope ->getEnvironment ()->getContext (RestContext::class);
58
56
}
59
57
60
58
/**
61
- * @Then I save the response
59
+ * @Then the JSON should be valid according to the JSON API schema
62
60
*/
63
- public function iSaveTheResponse ()
61
+ public function theJsonShouldBeValidAccordingToTheJsonApiSchema ()
64
62
{
65
- $ content = $ this ->getContent ();
63
+ $ refResolver = new RefResolver (new UriRetriever ());
64
+ $ refResolver ::$ maxDepth = 15 ;
66
65
67
- if (null === ($ decoded = json_decode ($ content ))) {
68
- throw new \RuntimeException ('JSON response seems to be invalid ' );
69
- }
70
-
71
- $ fileName = __DIR__ .'/response.json ' ;
72
-
73
- file_put_contents ($ fileName , $ content );
74
-
75
- return $ fileName ;
76
- }
77
-
78
- /**
79
- * @Then I validate it with jsonapi-validator
80
- */
81
- public function iValidateItWithJsonapiValidator ()
82
- {
83
- $ fileName = $ this ->iSaveTheResponse ();
84
-
85
- $ validationResponse = exec (sprintf ('cd %s && jsonapi-validator -f response.json ' , __DIR__ ));
86
-
87
- $ isValidJsonapi = 'response.json is valid JSON API. ' === $ validationResponse ;
88
-
89
- unlink ($ fileName );
90
-
91
- if (!$ isValidJsonapi ) {
92
- throw new \RuntimeException ('JSON response seems to be invalid JSON API ' );
93
- }
66
+ (new JsonSchema (file_get_contents ($ this ->jsonApiSchemaFile ), 'file:// ' .__DIR__ ))
67
+ ->resolve ($ refResolver )
68
+ ->validate ($ this ->getJson (), new Validator ())
69
+ ;
94
70
}
95
71
96
72
/**
@@ -100,9 +76,7 @@ public function iValidateItWithJsonapiValidator()
100
76
*/
101
77
public function theJsonNodeShouldBeAnEmptyArray ($ node )
102
78
{
103
- $ actual = $ this ->getValueOfNode ($ node );
104
-
105
- if (!is_array ($ actual ) || !empty ($ actual )) {
79
+ if (!is_array ($ actual = $ this ->getValueOfNode ($ node )) || !empty ($ actual )) {
106
80
throw new \Exception (
107
81
sprintf ("The node value is '%s' " , json_encode ($ actual ))
108
82
);
@@ -116,9 +90,7 @@ public function theJsonNodeShouldBeAnEmptyArray($node)
116
90
*/
117
91
public function theJsonNodeShouldBeANumber ($ node )
118
92
{
119
- $ actual = $ this ->getValueOfNode ($ node );
120
-
121
- if (!is_numeric ($ actual )) {
93
+ if (!is_numeric ($ actual = $ this ->getValueOfNode ($ node ))) {
122
94
throw new \Exception (
123
95
sprintf ('The node value is `%s` ' , json_encode ($ actual ))
124
96
);
@@ -132,43 +104,22 @@ public function theJsonNodeShouldBeANumber($node)
132
104
*/
133
105
public function theJsonNodeShouldNotBeAnEmptyString ($ node )
134
106
{
135
- $ actual = $ this ->getValueOfNode ($ node );
136
-
137
- if ($ actual === '' ) {
107
+ if ('' === $ actual = $ this ->getValueOfNode ($ node )) {
138
108
throw new \Exception (
139
109
sprintf ('The node value is `%s` ' , json_encode ($ actual ))
140
110
);
141
111
}
142
112
}
143
113
144
- private function getValueOfNode ($ node )
145
- {
146
- $ json = $ this ->getJson ();
147
-
148
- return $ this ->inspector ->evaluate ($ json , $ node );
149
- }
150
-
151
- private function getJson ()
152
- {
153
- return new Json ($ this ->getContent ());
154
- }
155
-
156
- private function getContent ()
157
- {
158
- return $ this ->restContext ->getMink ()->getSession ()->getDriver ()->getContent ();
159
- }
160
-
161
114
/**
162
115
* @Given there is a RelatedDummy
163
116
*/
164
117
public function thereIsARelatedDummy ()
165
118
{
166
119
$ relatedDummy = new RelatedDummy ();
167
-
168
120
$ relatedDummy ->setName ('RelatedDummy with no friends ' );
169
121
170
122
$ this ->manager ->persist ($ relatedDummy );
171
-
172
123
$ this ->manager ->flush ();
173
124
}
174
125
@@ -178,11 +129,43 @@ public function thereIsARelatedDummy()
178
129
public function thereIsADummyFriend ()
179
130
{
180
131
$ friend = new DummyFriend ();
181
-
182
132
$ friend ->setName ('DummyFriend ' );
183
133
184
134
$ this ->manager ->persist ($ friend );
135
+ $ this ->manager ->flush ();
136
+ }
185
137
138
+ /**
139
+ * @Given there is a CircularReference
140
+ */
141
+ public function thereIsACircularReference ()
142
+ {
143
+ $ circularReference = new CircularReference ();
144
+ $ circularReference ->parent = $ circularReference ;
145
+
146
+ $ circularReferenceBis = new CircularReference ();
147
+ $ circularReferenceBis ->parent = $ circularReference ;
148
+
149
+ $ circularReference ->children ->add ($ circularReference );
150
+ $ circularReference ->children ->add ($ circularReferenceBis );
151
+
152
+ $ this ->manager ->persist ($ circularReference );
153
+ $ this ->manager ->persist ($ circularReferenceBis );
186
154
$ this ->manager ->flush ();
187
155
}
156
+
157
+ private function getValueOfNode ($ node )
158
+ {
159
+ return $ this ->inspector ->evaluate ($ this ->getJson (), $ node );
160
+ }
161
+
162
+ private function getJson ()
163
+ {
164
+ return new Json ($ this ->getContent ());
165
+ }
166
+
167
+ private function getContent ()
168
+ {
169
+ return $ this ->restContext ->getMink ()->getSession ()->getDriver ()->getContent ();
170
+ }
188
171
}
0 commit comments