Skip to content

Commit 8a62339

Browse files
committed
Merge pull request #317 from dunglas/fix-hydra-doc
Fix Hydra doc when the ID is readonly + Fix cache
2 parents b1234a9 + c3e1d15 commit 8a62339

File tree

3 files changed

+205
-1280
lines changed

3 files changed

+205
-1280
lines changed

Mapping/Loader/DoctrineIdentifierLoader.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public function loadClassMetadata(
4848
return true;
4949
}
5050

51-
$doctrineClassMetaData = $manager->getClassMetadata($className);
52-
if (!$doctrineClassMetaData) {
51+
$doctrineClassMetadata = $manager->getClassMetadata($className);
52+
if (!$doctrineClassMetadata) {
5353
return true;
5454
}
5555

56-
$identifiers = $doctrineClassMetaData->getIdentifier();
56+
$identifiers = $doctrineClassMetadata->getIdentifier();
5757
if (1 !== count($identifiers)) {
5858
return true;
5959
}
@@ -63,6 +63,10 @@ public function loadClassMetadata(
6363
if ($attribute->getName() === $identifierName) {
6464
$attribute->setIdentifier(true);
6565

66+
if (!$doctrineClassMetadata->isIdentifierNatural()) {
67+
$attribute->setWritable(false);
68+
}
69+
6670
return true;
6771
}
6872
}

features/bootstrap/HydraContext.php

Lines changed: 132 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@
1212
use Behat\Behat\Context\Context;
1313
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
1414
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
15+
use Symfony\Component\PropertyAccess\PropertyAccess;
1516

1617
class HydraContext implements Context
1718
{
1819
/**
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+
*
1932
* @param BeforeScenarioScope $scope
2033
*
2134
* @BeforeScenario
@@ -28,84 +41,138 @@ public function gatherContexts(BeforeScenarioScope $scope)
2841
}
2942

3043
/**
31-
* @Then the hydra class ":class" exist
44+
* @Then the Hydra class ":class" exist
3245
*/
3346
public function assertTheHydraClassExist($className)
3447
{
3548
$this->getClassInfos($className);
3649
}
3750

3851
/**
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"
4097
*/
4198
public function assertNbOperationsExist($nb, $className)
4299
{
43100
$operations = $this->getOperations($className);
44101

45-
\PHPUnit_Framework_Assert::assertEquals(
46-
$nb,
47-
count($operations)
48-
);
102+
\PHPUnit_Framework_Assert::assertEquals($nb, count($operations));
49103
}
50104

51105
/**
52-
* @Then :nb properties are available for hydra class ":class"
106+
* @Then :nb properties are available for Hydra class ":class"
53107
*/
54108
public function assertNbPropertiesExist($nb, $className)
55109
{
56110
$properties = $this->getProperties($className);
57111

58-
\PHPUnit_Framework_Assert::assertEquals(
59-
$nb,
60-
count($properties)
61-
);
112+
\PHPUnit_Framework_Assert::assertEquals($nb, count($properties));
62113
}
63114

64115
/**
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"
66133
*/
67134
public function assertPropertyIsReadable($propertyName, $className)
68135
{
69-
$propertyInfos = $this->getProperty($propertyName, $className);
136+
$properties = $this->getProperty($propertyName, $className);
70137

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));
73140
}
74141
}
75142

76143
/**
77-
* @Then ":prop" property is not readable for hydra class ":class"
144+
* @Then ":prop" property is not readable for Hydra class ":class"
78145
*/
79146
public function assertPropertyIsNotReadable($propertyName, $className)
80147
{
81-
$propertyInfos = $this->getProperty($propertyName, $className);
148+
$properties = $this->getProperty($propertyName, $className);
82149

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));
85152
}
86153
}
87154

88155
/**
89-
* @Then ":prop" property is writable for hydra class ":class"
156+
* @Then ":prop" property is writable for Hydra class ":class"
90157
*/
91158
public function assertPropertyIsWritable($propertyName, $className)
92159
{
93-
$propertyInfos = $this->getProperty($propertyName, $className);
160+
$properties = $this->getProperty($propertyName, $className);
94161

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));
97164
}
98165
}
99166

100167
/**
101-
* @Then ":prop" property is required for hydra class ":class"
168+
* @Then ":prop" property is required for Hydra class ":class"
102169
*/
103170
public function assertPropertyIsRequired($propertyName, $className)
104171
{
105-
$propertyInfos = $this->getProperty($propertyName, $className);
172+
$properties = $this->getProperty($propertyName, $className);
106173

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));
109176
}
110177
}
111178

@@ -122,19 +189,42 @@ private function getProperty($propertyName, $className)
122189
$properties = $this->getProperties($className);
123190
$propertyInfos = null;
124191
foreach ($properties as $property) {
125-
if ($property['hydra:title'] == $propertyName) {
192+
if ($property->{'hydra:title'} === $propertyName) {
126193
$propertyInfos = $property;
127194
}
128195
}
129196

130197
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));
132199
}
133200

134201
return $propertyInfos;
135202
}
136203

137204
/**
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+
*
138228
* @param string $className
139229
*
140230
* @return array
@@ -145,10 +235,12 @@ private function getOperations($className)
145235
{
146236
$classInfos = $this->getClassInfos($className);
147237

148-
return isset($classInfos['hydra:supportedOperation']) ? $classInfos['hydra:supportedOperation'] : [];
238+
return isset($classInfos->{'hydra:supportedOperation'}) ? $classInfos->{'hydra:supportedOperation'} : [];
149239
}
150240

151241
/**
242+
* Gets all properties of a given class.
243+
*
152244
* @param string $className
153245
*
154246
* @return array
@@ -159,10 +251,12 @@ private function getProperties($className)
159251
{
160252
$classInfos = $this->getClassInfos($className);
161253

162-
return isset($classInfos['hydra:supportedProperty']) ? $classInfos['hydra:supportedProperty'] : [];
254+
return isset($classInfos->{'hydra:supportedProperty'}) ? $classInfos->{'hydra:supportedProperty'} : [];
163255
}
164256

165257
/**
258+
* Gets information about a class.
259+
*
166260
* @param string $className
167261
*
168262
* @return array
@@ -174,28 +268,30 @@ private function getClassInfos($className)
174268
$json = $this->getLastJsonResponse();
175269
$classInfos = null;
176270

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) {
180274
$classInfos = $classData;
181275
}
182276
}
183277
}
184278

185279
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));
187281
}
188282

189283
return $classInfos;
190284
}
191285

192286
/**
287+
* Gets the last JSON response.
288+
*
193289
* @return array
194290
*/
195291
private function getLastJsonResponse()
196292
{
197293
$content = $this->restContext->getMink()->getSession()->getDriver()->getContent();
198-
if (null === ($decoded = json_decode($content, true))) {
294+
if (null === ($decoded = json_decode($content))) {
199295
throw new \RuntimeException('JSON response seems to be invalid');
200296
}
201297

0 commit comments

Comments
 (0)