12
12
use Behat \Behat \Context \Context ;
13
13
use Behat \Behat \Context \Environment \InitializedContextEnvironment ;
14
14
use Behat \Behat \Hook \Scope \BeforeScenarioScope ;
15
+ use Symfony \Component \PropertyAccess \PropertyAccess ;
15
16
16
17
class HydraContext implements Context
17
18
{
18
19
/**
20
+ * @var PropertyAccessor
21
+ */
22
+ private $ propertyAccessor ;
23
+
24
+ public function __construct ()
25
+ {
26
+ $ this ->propertyAccessor = PropertyAccess::createPropertyAccessor ();
27
+ }
28
+
29
+ /**
30
+ * Gives access to the Behatch context.
31
+ *
19
32
* @param BeforeScenarioScope $scope
20
33
*
21
34
* @BeforeScenario
@@ -28,84 +41,138 @@ public function gatherContexts(BeforeScenarioScope $scope)
28
41
}
29
42
30
43
/**
31
- * @Then the hydra class ":class" exist
44
+ * @Then the Hydra class ":class" exist
32
45
*/
33
46
public function assertTheHydraClassExist ($ className )
34
47
{
35
48
$ this ->getClassInfos ($ className );
36
49
}
37
50
38
51
/**
39
- * @Then :nb operations are available for hydra class ":class"
52
+ * @Then the Hydra class ":class" not exist
53
+ */
54
+ public function assertTheHydraClassNotExist ($ className )
55
+ {
56
+ try {
57
+ $ this ->getClassInfos ($ className );
58
+
59
+ throw new \PHPUnit_Framework_ExpectationFailedException (sprintf ('The class "%s" exist. ' , $ className ));
60
+ } catch (\Exception $ exception ) {
61
+ // an exception must be catched
62
+ }
63
+ }
64
+
65
+ /**
66
+ * @Then the value of the node ":node" of the Hydra class ":class" is ":value"
67
+ */
68
+ public function assertNodeValueIs ($ nodeName , $ className , $ value )
69
+ {
70
+ $ classInfos = $ this ->getClassInfos ($ className );
71
+
72
+ \PHPUnit_Framework_Assert::assertEquals ($ this ->propertyAccessor ->getValue ($ classInfos , $ nodeName ), $ value );
73
+ }
74
+
75
+ /**
76
+ * @Then the value of the node ":node" of the property ":prop" of the Hydra class ":class" is ":value"
77
+ */
78
+ public function assertPropertyNodeValueIs ($ nodeName , $ propertyName , $ className , $ value )
79
+ {
80
+ $ property = $ this ->getProperty ($ propertyName , $ className );
81
+
82
+ \PHPUnit_Framework_Assert::assertEquals ($ this ->propertyAccessor ->getValue ($ property , $ nodeName ), $ value );
83
+ }
84
+
85
+ /**
86
+ * @Then the value of the node ":node" of the operation ":operation" of the Hydra class ":class" is ":value"
87
+ */
88
+ public function assertOperationNodeValueIs ($ nodeName , $ operationMethod , $ className , $ value )
89
+ {
90
+ $ property = $ this ->getOperation ($ operationMethod , $ className );
91
+
92
+ \PHPUnit_Framework_Assert::assertEquals ($ this ->propertyAccessor ->getValue ($ property , $ nodeName ), $ value );
93
+ }
94
+
95
+ /**
96
+ * @Then :nb operations are available for Hydra class ":class"
40
97
*/
41
98
public function assertNbOperationsExist ($ nb , $ className )
42
99
{
43
100
$ operations = $ this ->getOperations ($ className );
44
101
45
- \PHPUnit_Framework_Assert::assertEquals (
46
- $ nb ,
47
- count ($ operations )
48
- );
102
+ \PHPUnit_Framework_Assert::assertEquals ($ nb , count ($ operations ));
49
103
}
50
104
51
105
/**
52
- * @Then :nb properties are available for hydra class ":class"
106
+ * @Then :nb properties are available for Hydra class ":class"
53
107
*/
54
108
public function assertNbPropertiesExist ($ nb , $ className )
55
109
{
56
110
$ properties = $ this ->getProperties ($ className );
57
111
58
- \PHPUnit_Framework_Assert::assertEquals (
59
- $ nb ,
60
- count ($ properties )
61
- );
112
+ \PHPUnit_Framework_Assert::assertEquals ($ nb , count ($ properties ));
62
113
}
63
114
64
115
/**
65
- * @Then ":prop" property is readable for hydra class ":class"
116
+ * @Then ":prop" property doesn't exist for the Hydra class ":class"
117
+ */
118
+ public function assertPropertyNotExist ($ propertyName , $ className )
119
+ {
120
+ try {
121
+ $ this ->getProperty ($ propertyName , $ className );
122
+
123
+ throw new \PHPUnit_Framework_ExpectationFailedException (
124
+ sprintf ('The property "%s" for the class "%s" exist. ' , $ propertyName , $ className )
125
+ );
126
+ } catch (\Exception $ exception ) {
127
+ // an exception must be catched
128
+ }
129
+ }
130
+
131
+ /**
132
+ * @Then ":prop" property is readable for Hydra class ":class"
66
133
*/
67
134
public function assertPropertyIsReadable ($ propertyName , $ className )
68
135
{
69
- $ propertyInfos = $ this ->getProperty ($ propertyName , $ className );
136
+ $ properties = $ this ->getProperty ($ propertyName , $ className );
70
137
71
- if (empty ($ propertyInfos [ 'hydra:readable ' ] )) {
72
- throw new Exception (sprintf ('Property "%s" of class "%s" is not readable ' , $ propertyName , $ className ));
138
+ if (empty ($ properties ->{ 'hydra:readable ' } )) {
139
+ throw new \ Exception (sprintf ('Property "%s" of class "%s" is not readable ' , $ propertyName , $ className ));
73
140
}
74
141
}
75
142
76
143
/**
77
- * @Then ":prop" property is not readable for hydra class ":class"
144
+ * @Then ":prop" property is not readable for Hydra class ":class"
78
145
*/
79
146
public function assertPropertyIsNotReadable ($ propertyName , $ className )
80
147
{
81
- $ propertyInfos = $ this ->getProperty ($ propertyName , $ className );
148
+ $ properties = $ this ->getProperty ($ propertyName , $ className );
82
149
83
- if (!empty ($ propertyInfos [ 'hydra:readable ' ] )) {
84
- throw new Exception (sprintf ('Property "%s" of class "%s" is readable ' , $ propertyName , $ className ));
150
+ if (!empty ($ properties ->{ 'hydra:readable ' } )) {
151
+ throw new \ Exception (sprintf ('Property "%s" of class "%s" is readable ' , $ propertyName , $ className ));
85
152
}
86
153
}
87
154
88
155
/**
89
- * @Then ":prop" property is writable for hydra class ":class"
156
+ * @Then ":prop" property is writable for Hydra class ":class"
90
157
*/
91
158
public function assertPropertyIsWritable ($ propertyName , $ className )
92
159
{
93
- $ propertyInfos = $ this ->getProperty ($ propertyName , $ className );
160
+ $ properties = $ this ->getProperty ($ propertyName , $ className );
94
161
95
- if (empty ($ propertyInfos [ 'hydra:writable ' ] )) {
96
- throw new Exception (sprintf ('Property "%s" of class "%s" is not writable ' , $ propertyName , $ className ));
162
+ if (empty ($ properties ->{ 'hydra:writable ' } )) {
163
+ throw new \ Exception (sprintf ('Property "%s" of class "%s" is not writable ' , $ propertyName , $ className ));
97
164
}
98
165
}
99
166
100
167
/**
101
- * @Then ":prop" property is required for hydra class ":class"
168
+ * @Then ":prop" property is required for Hydra class ":class"
102
169
*/
103
170
public function assertPropertyIsRequired ($ propertyName , $ className )
104
171
{
105
- $ propertyInfos = $ this ->getProperty ($ propertyName , $ className );
172
+ $ properties = $ this ->getProperty ($ propertyName , $ className );
106
173
107
- if (empty ($ propertyInfos [ 'hydra:required ' ] )) {
108
- throw new Exception (sprintf ('Property "%s" of class "%s" is not required ' , $ propertyName , $ className ));
174
+ if (empty ($ properties ->{ 'hydra:required ' } )) {
175
+ throw new \ Exception (sprintf ('Property "%s" of class "%s" is not required ' , $ propertyName , $ className ));
109
176
}
110
177
}
111
178
@@ -122,19 +189,42 @@ private function getProperty($propertyName, $className)
122
189
$ properties = $ this ->getProperties ($ className );
123
190
$ propertyInfos = null ;
124
191
foreach ($ properties as $ property ) {
125
- if ($ property[ 'hydra:title ' ] == $ propertyName ) {
192
+ if ($ property->{ 'hydra:title ' } = == $ propertyName ) {
126
193
$ propertyInfos = $ property ;
127
194
}
128
195
}
129
196
130
197
if (empty ($ propertyInfos )) {
131
- throw new Exception (sprintf ('Property "%s" of class "%s" not exist ' , $ propertyName , $ className ));
198
+ throw new \ Exception (sprintf ('Property "%s" of class "%s" does \' nt exist ' , $ propertyName , $ className ));
132
199
}
133
200
134
201
return $ propertyInfos ;
135
202
}
136
203
137
204
/**
205
+ * Gets an operation by its method name.
206
+ *
207
+ * @param string $className
208
+ * @param string $method
209
+ *
210
+ * @return array
211
+ *
212
+ * @throws Exception
213
+ */
214
+ private function getOperation ($ method , $ className )
215
+ {
216
+ foreach ($ this ->getOperations ($ className ) as $ operation ) {
217
+ if ($ operation ->{'hydra:method ' } === $ method ) {
218
+ return $ operation ;
219
+ }
220
+ }
221
+
222
+ throw new \Exception (sprintf ('Operation "%s" of class "%s" doesn \'t exist. ' , $ method , $ className ));
223
+ }
224
+
225
+ /**
226
+ * Gets all operations of a given class.
227
+ *
138
228
* @param string $className
139
229
*
140
230
* @return array
@@ -145,10 +235,12 @@ private function getOperations($className)
145
235
{
146
236
$ classInfos = $ this ->getClassInfos ($ className );
147
237
148
- return isset ($ classInfos[ 'hydra:supportedOperation ' ] ) ? $ classInfos[ 'hydra:supportedOperation ' ] : [];
238
+ return isset ($ classInfos->{ 'hydra:supportedOperation ' } ) ? $ classInfos->{ 'hydra:supportedOperation ' } : [];
149
239
}
150
240
151
241
/**
242
+ * Gets all properties of a given class.
243
+ *
152
244
* @param string $className
153
245
*
154
246
* @return array
@@ -159,10 +251,12 @@ private function getProperties($className)
159
251
{
160
252
$ classInfos = $ this ->getClassInfos ($ className );
161
253
162
- return isset ($ classInfos[ 'hydra:supportedProperty ' ] ) ? $ classInfos[ 'hydra:supportedProperty ' ] : [];
254
+ return isset ($ classInfos->{ 'hydra:supportedProperty ' } ) ? $ classInfos->{ 'hydra:supportedProperty ' } : [];
163
255
}
164
256
165
257
/**
258
+ * Gets information about a class.
259
+ *
166
260
* @param string $className
167
261
*
168
262
* @return array
@@ -174,28 +268,30 @@ private function getClassInfos($className)
174
268
$ json = $ this ->getLastJsonResponse ();
175
269
$ classInfos = null ;
176
270
177
- if (isset ($ json[ 'hydra:supportedClass ' ] )) {
178
- foreach ($ json[ 'hydra:supportedClass ' ] as $ classData ) {
179
- if ($ classData[ 'hydra:title ' ] == $ className ) {
271
+ if (isset ($ json->{ 'hydra:supportedClass ' } )) {
272
+ foreach ($ json->{ 'hydra:supportedClass ' } as $ classData ) {
273
+ if ($ classData->{ 'hydra:title ' } = == $ className ) {
180
274
$ classInfos = $ classData ;
181
275
}
182
276
}
183
277
}
184
278
185
279
if (empty ($ classInfos )) {
186
- throw new Exception (sprintf ('Class %s cannot be found in the vocabulary ' , $ className ));
280
+ throw new \ Exception (sprintf ('Class %s cannot be found in the vocabulary ' , $ className ));
187
281
}
188
282
189
283
return $ classInfos ;
190
284
}
191
285
192
286
/**
287
+ * Gets the last JSON response.
288
+ *
193
289
* @return array
194
290
*/
195
291
private function getLastJsonResponse ()
196
292
{
197
293
$ content = $ this ->restContext ->getMink ()->getSession ()->getDriver ()->getContent ();
198
- if (null === ($ decoded = json_decode ($ content, true ))) {
294
+ if (null === ($ decoded = json_decode ($ content ))) {
199
295
throw new \RuntimeException ('JSON response seems to be invalid ' );
200
296
}
201
297
0 commit comments