Skip to content

Commit 8ed3bdc

Browse files
authored
Merge pull request #984 from jordscream/nested-embed-field-filter
Manage embedded fields for API Platform Filter
2 parents c86a607 + 5dc1b08 commit 8ed3bdc

21 files changed

+1271
-61
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyOffer;
2626
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyProduct;
2727
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyProperty;
28+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\EmbeddableDummy;
29+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\EmbeddedDummy;
2830
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy;
2931
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
3032
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Question;
@@ -170,6 +172,25 @@ public function thereIsDummyPropertyObjects($nb)
170172
$this->manager->flush();
171173
}
172174

175+
/**
176+
* @Given there are :nb embedded dummy objects
177+
*/
178+
public function thereIsEmbeddedDummyObjects($nb)
179+
{
180+
for ($i = 1; $i <= $nb; ++$i) {
181+
$dummy = new EmbeddedDummy();
182+
$dummy->setName('Dummy #'.$i);
183+
184+
$embeddableDummy = new EmbeddableDummy();
185+
$embeddableDummy->setDummyName('Dummy #'.$i);
186+
$dummy->setEmbeddedDummy($embeddableDummy);
187+
188+
$this->manager->persist($dummy);
189+
}
190+
191+
$this->manager->flush();
192+
}
193+
173194
/**
174195
* @Given there is :nb dummy objects with relatedDummy
175196
*/
@@ -191,6 +212,25 @@ public function thereIsDummyObjectsWithRelatedDummy($nb)
191212
$this->manager->flush();
192213
}
193214

215+
/**
216+
* @Given there is :nb dummy objects with embeddedDummy
217+
*/
218+
public function thereIsDummyObjectsWithEmbeddedDummy($nb)
219+
{
220+
for ($i = 1; $i <= $nb; ++$i) {
221+
$embeddableDummy = new EmbeddableDummy();
222+
$embeddableDummy->setDummyName('EmbeddedDummy #'.$i);
223+
224+
$dummy = new EmbeddedDummy();
225+
$dummy->setName('Dummy #'.$i);
226+
$dummy->setEmbeddedDummy($embeddableDummy);
227+
228+
$this->manager->persist($dummy);
229+
}
230+
231+
$this->manager->flush();
232+
}
233+
194234
/**
195235
* @Given there is :nb dummy objects having each :nbrelated relatedDummies
196236
*/
@@ -305,6 +345,32 @@ public function thereIsDummyObjectsWithDummyDateAndRelatedDummy($nb)
305345
$this->manager->flush();
306346
}
307347

348+
/**
349+
* @Given there is :nb embedded dummy objects with dummyDate and embeddedDummy
350+
*/
351+
public function thereIsDummyObjectsWithDummyDateAndEmbeddedDummy($nb)
352+
{
353+
for ($i = 1; $i <= $nb; ++$i) {
354+
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
355+
356+
$embeddableDummy = new EmbeddableDummy();
357+
$embeddableDummy->setDummyName('Embeddable #'.$i);
358+
$embeddableDummy->setDummyDate($date);
359+
360+
$dummy = new EmbeddedDummy();
361+
$dummy->setName('Dummy #'.$i);
362+
$dummy->setEmbeddedDummy($embeddableDummy);
363+
// Last Dummy has a null date
364+
if ($nb !== $i) {
365+
$dummy->setDummyDate($date);
366+
}
367+
368+
$this->manager->persist($dummy);
369+
}
370+
371+
$this->manager->flush();
372+
}
373+
308374
/**
309375
* @Given there is :nb dummy objects with dummyPrice
310376
*/
@@ -354,6 +420,66 @@ public function thereIsDummyObjectsWithDummyBoolean($nb, $bool)
354420
$this->manager->flush();
355421
}
356422

423+
/**
424+
* @Given there is :nb embedded dummy objects with embeddedDummy.dummyBoolean :bool
425+
*/
426+
public function thereIsDummyObjectsWithEmbeddedDummyBoolean($nb, $bool)
427+
{
428+
if (in_array($bool, ['true', '1', 1], true)) {
429+
$bool = true;
430+
} elseif (in_array($bool, ['false', '0', 0], true)) {
431+
$bool = false;
432+
} else {
433+
$expected = ['true', 'false', '1', '0'];
434+
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
435+
}
436+
437+
for ($i = 1; $i <= $nb; ++$i) {
438+
$dummy = new EmbeddedDummy();
439+
$dummy->setName('Embedded Dummy #'.$i);
440+
$embeddableDummy = new EmbeddableDummy();
441+
$embeddableDummy->setDummyName('Embedded Dummy #'.$i);
442+
$embeddableDummy->setDummyBoolean($bool);
443+
$dummy->setEmbeddedDummy($embeddableDummy);
444+
$this->manager->persist($dummy);
445+
}
446+
447+
$this->manager->flush();
448+
}
449+
450+
/**
451+
* @Given there is :nb embedded dummy objects with relatedDummy.embeddedDummy.dummyBoolean :bool
452+
*/
453+
public function thereIsDummyObjectsWithRelationEmbeddedDummyBoolean($nb, $bool)
454+
{
455+
if (in_array($bool, ['true', '1', 1], true)) {
456+
$bool = true;
457+
} elseif (in_array($bool, ['false', '0', 0], true)) {
458+
$bool = false;
459+
} else {
460+
$expected = ['true', 'false', '1', '0'];
461+
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
462+
}
463+
464+
for ($i = 1; $i <= $nb; ++$i) {
465+
$dummy = new EmbeddedDummy();
466+
$dummy->setName('Embedded Dummy #'.$i);
467+
$embeddableDummy = new EmbeddableDummy();
468+
$embeddableDummy->setDummyName('Embedded Dummy #'.$i);
469+
$embeddableDummy->setDummyBoolean($bool);
470+
471+
$relationDummy = new RelatedDummy();
472+
$relationDummy->setEmbeddedDummy($embeddableDummy);
473+
474+
$dummy->setRelatedDummy($relationDummy);
475+
476+
$this->manager->persist($relationDummy);
477+
$this->manager->persist($dummy);
478+
}
479+
480+
$this->manager->flush();
481+
}
482+
357483
/**
358484
* @Given there is a RelationEmbedder object
359485
*/

features/doctrine/boolean_filter.feature

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,210 @@ Feature: Boolean filter on collections
166166
"""
167167
And the JSON node "hydra:totalItems" should be equal to 10
168168

169+
Scenario: Get collection by embeddedDummy.dummyBoolean true
170+
Given there is "15" embedded dummy objects with embeddedDummy.dummyBoolean true
171+
And there is "10" embedded dummy objects with embeddedDummy.dummyBoolean false
172+
When I send a "GET" request to "/embedded_dummies?embeddedDummy.dummyBoolean=true"
173+
Then the response status code should be 200
174+
And the response should be in JSON
175+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
176+
And the JSON should be valid according to this schema:
177+
"""
178+
{
179+
"type": "object",
180+
"properties": {
181+
"@context": {"pattern": "^/contexts/EmbeddedDummy$"},
182+
"@id": {"pattern": "^/embedded_dummies$"},
183+
"@type": {"pattern": "^hydra:Collection$"},
184+
"hydra:member": {
185+
"type": "array",
186+
"items": {
187+
"type": "object",
188+
"properties": {
189+
"@id": {
190+
"oneOf": [
191+
{"pattern": "^/embedded_dummies/1$"},
192+
{"pattern": "^/embedded_dummies/2$"},
193+
{"pattern": "^/embedded_dummies/3$"}
194+
]
195+
}
196+
}
197+
}
198+
},
199+
"hydra:view": {
200+
"type": "object",
201+
"properties": {
202+
"@id": {"pattern": "^/embedded_dummies\\?embeddedDummy\\.dummyBoolean=true"},
203+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
204+
}
205+
}
206+
}
207+
}
208+
"""
209+
And the JSON node "hydra:totalItems" should be equal to 15
210+
211+
Scenario: Get collection by embeddedDummy.dummyBoolean true
212+
When I send a "GET" request to "/embedded_dummies?embeddedDummy.dummyBoolean=1"
213+
Then the response status code should be 200
214+
And the response should be in JSON
215+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
216+
And the JSON should be valid according to this schema:
217+
"""
218+
{
219+
"type": "object",
220+
"properties": {
221+
"@context": {"pattern": "^/contexts/EmbeddedDummy$"},
222+
"@id": {"pattern": "^/embedded_dummies$"},
223+
"@type": {"pattern": "^hydra:Collection$"},
224+
"hydra:member": {
225+
"type": "array",
226+
"items": {
227+
"type": "object",
228+
"properties": {
229+
"@id": {
230+
"oneOf": [
231+
{"pattern": "^/embedded_dummies/1$"},
232+
{"pattern": "^/embedded_dummies/2$"},
233+
{"pattern": "^/embedded_dummies/3$"}
234+
]
235+
}
236+
}
237+
}
238+
},
239+
"hydra:view": {
240+
"type": "object",
241+
"properties": {
242+
"@id": {"pattern": "^/embedded_dummies\\?embeddedDummy\\.dummyBoolean=1"},
243+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
244+
}
245+
}
246+
}
247+
}
248+
"""
249+
And the JSON node "hydra:totalItems" should be equal to 15
250+
251+
Scenario: Get collection by embeddedDummy.dummyBoolean false
252+
When I send a "GET" request to "/embedded_dummies?embeddedDummy.dummyBoolean=false"
253+
Then the response status code should be 200
254+
And the response should be in JSON
255+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
256+
And the JSON should be valid according to this schema:
257+
"""
258+
{
259+
"type": "object",
260+
"properties": {
261+
"@context": {"pattern": "^/contexts/EmbeddedDummy$"},
262+
"@id": {"pattern": "^/embedded_dummies$"},
263+
"@type": {"pattern": "^hydra:Collection$"},
264+
"hydra:member": {
265+
"type": "array",
266+
"items": {
267+
"type": "object",
268+
"properties": {
269+
"@id": {
270+
"oneOf": [
271+
{"pattern": "^/embedded_dummies/16$"},
272+
{"pattern": "^/embedded_dummies/17$"},
273+
{"pattern": "^/embedded_dummies/18$"}
274+
]
275+
}
276+
}
277+
}
278+
},
279+
"hydra:view": {
280+
"type": "object",
281+
"properties": {
282+
"@id": {"pattern": "^/embedded_dummies\\?embeddedDummy\\.dummyBoolean=false"},
283+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
284+
}
285+
}
286+
}
287+
}
288+
"""
289+
And the JSON node "hydra:totalItems" should be equal to 10
290+
291+
Scenario: Get collection by embeddedDummy.dummyBoolean false
292+
When I send a "GET" request to "/embedded_dummies?embeddedDummy.dummyBoolean=0"
293+
Then the response status code should be 200
294+
And the response should be in JSON
295+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
296+
And the JSON should be valid according to this schema:
297+
"""
298+
{
299+
"type": "object",
300+
"properties": {
301+
"@context": {"pattern": "^/contexts/EmbeddedDummy$"},
302+
"@id": {"pattern": "^/embedded_dummies$"},
303+
"@type": {"pattern": "^hydra:Collection$"},
304+
"hydra:member": {
305+
"type": "array",
306+
"items": {
307+
"type": "object",
308+
"properties": {
309+
"@id": {
310+
"oneOf": [
311+
{"pattern": "^/embedded_dummies/16$"},
312+
{"pattern": "^/embedded_dummies/17$"},
313+
{"pattern": "^/embedded_dummies/18$"}
314+
]
315+
}
316+
}
317+
}
318+
},
319+
"hydra:view": {
320+
"type": "object",
321+
"properties": {
322+
"@id": {"pattern": "^/embedded_dummies\\?embeddedDummy\\.dummyBoolean=0"},
323+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
324+
}
325+
}
326+
}
327+
}
328+
"""
329+
And the JSON node "hydra:totalItems" should be equal to 10
330+
331+
Scenario: Get collection by association with embed relatedDummy.embeddedDummy.dummyBoolean true
332+
Given there is "15" embedded dummy objects with relatedDummy.embeddedDummy.dummyBoolean true
333+
And there is "10" embedded dummy objects with relatedDummy.embeddedDummy.dummyBoolean false
334+
When I send a "GET" request to "/embedded_dummies?relatedDummy.embeddedDummy.dummyBoolean=true"
335+
Then the response status code should be 200
336+
And the response should be in JSON
337+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
338+
And the JSON should be valid according to this schema:
339+
"""
340+
{
341+
"type": "object",
342+
"properties": {
343+
"@context": {"pattern": "^/contexts/EmbeddedDummy$"},
344+
"@id": {"pattern": "^/embedded_dummies$"},
345+
"@type": {"pattern": "^hydra:Collection$"},
346+
"hydra:member": {
347+
"type": "array",
348+
"items": {
349+
"type": "object",
350+
"properties": {
351+
"@id": {
352+
"oneOf": [
353+
{"pattern": "^/embedded_dummies/26$"},
354+
{"pattern": "^/embedded_dummies/27$"},
355+
{"pattern": "^/embedded_dummies/28$"}
356+
]
357+
}
358+
}
359+
}
360+
},
361+
"hydra:view": {
362+
"type": "object",
363+
"properties": {
364+
"@id": {"pattern": "^/embedded_dummies\\?relatedDummy.embeddedDummy\\.dummyBoolean=true"},
365+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
366+
}
367+
}
368+
}
369+
}
370+
"""
371+
And the JSON node "hydra:totalItems" should be equal to 15
372+
169373
@dropSchema
170374
Scenario: Get collection ordered by a non valid properties
171375
When I send a "GET" request to "/dummies?unknown=0"

0 commit comments

Comments
 (0)