Skip to content

Commit 7e58429

Browse files
committed
Add support for name converter in filters
1 parent 0888bf3 commit 7e58429

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1886
-412
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 123 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeLabel as CompositeLabelDocument;
1818
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositePrimitiveItem as CompositePrimitiveItemDocument;
1919
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeRelation as CompositeRelationDocument;
20+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ConvertedBoolean as ConvertedBoolDocument;
21+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ConvertedDate as ConvertedDateDocument;
22+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ConvertedInteger as ConvertedIntegerDocument;
23+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ConvertedString as ConvertedStringDocument;
2024
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Customer as CustomerDocument;
2125
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Dummy as DummyDocument;
2226
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyAggregateOffer as DummyAggregateOfferDocument;
@@ -62,6 +66,10 @@
6266
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositePrimitiveItem;
6367
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
6468
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Container;
69+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ConvertedBoolean;
70+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ConvertedDate;
71+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ConvertedInteger;
72+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ConvertedString;
6573
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Customer;
6674
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
6775
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
@@ -164,6 +172,7 @@ public function thereAreDummyObjects(int $nb)
164172
$dummy->setAlias('Alias #'.($nb - $i));
165173
$dummy->setDummy('SomeDummyTest'.$i);
166174
$dummy->setDescription($descriptions[($i - 1) % 2]);
175+
$dummy->nameConverted = 'Converted '.$i;
167176

168177
$this->manager->persist($dummy);
169178
}
@@ -257,6 +266,7 @@ public function thereAreDummyPropertyObjects(int $nb)
257266
foreach (['foo', 'bar', 'baz'] as $property) {
258267
$dummyProperty->{$property} = $dummyGroup->{$property} = ucfirst($property).' #'.$i;
259268
}
269+
$dummyProperty->nameConverted = "NameConverted #$i";
260270

261271
$dummyProperty->group = $dummyGroup;
262272

@@ -657,6 +667,66 @@ public function thereAreDummyObjectsWithDummyDateAndEmbeddedDummy(int $nb)
657667
$this->manager->flush();
658668
}
659669

670+
/**
671+
* @Given there are :nb convertedDate objects
672+
*/
673+
public function thereAreconvertedDateObjectsWith(int $nb)
674+
{
675+
for ($i = 1; $i <= $nb; ++$i) {
676+
$convertedDate = $this->buildConvertedDate();
677+
$convertedDate->nameConverted = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
678+
679+
$this->manager->persist($convertedDate);
680+
}
681+
682+
$this->manager->flush();
683+
}
684+
685+
/**
686+
* @Given there are :nb convertedString objects
687+
*/
688+
public function thereAreconvertedStringObjectsWith(int $nb)
689+
{
690+
for ($i = 1; $i <= $nb; ++$i) {
691+
$convertedString = $this->buildConvertedString();
692+
$convertedString->nameConverted = ($i % 2) ? "name#$i" : null;
693+
694+
$this->manager->persist($convertedString);
695+
}
696+
697+
$this->manager->flush();
698+
}
699+
700+
/**
701+
* @Given there are :nb convertedBoolean objects
702+
*/
703+
public function thereAreconvertedBooleanObjectsWith(int $nb)
704+
{
705+
for ($i = 1; $i <= $nb; ++$i) {
706+
$convertedBoolean = $this->buildConvertedBoolean();
707+
$convertedBoolean->nameConverted = (bool) ($i % 2);
708+
709+
$this->manager->persist($convertedBoolean);
710+
}
711+
712+
$this->manager->flush();
713+
}
714+
715+
/**
716+
* @Given there are :nb convertedInteger objects
717+
*/
718+
public function thereAreconvertedIntegerObjectsWith(int $nb)
719+
{
720+
for ($i = 1; $i <= $nb; ++$i) {
721+
$convertedInteger = $this->buildConvertedInteger();
722+
$convertedInteger->nameConverted = $i;
723+
724+
$this->manager->persist($convertedInteger);
725+
}
726+
727+
$this->manager->flush();
728+
}
729+
660730
/**
661731
* @Given there are :nb dummy objects with dummyPrice
662732
*/
@@ -1264,6 +1334,35 @@ public function thereAreNbDummyDtoCustom($nb)
12641334
$this->manager->clear();
12651335
}
12661336

1337+
/**
1338+
* @Given there is a order with same customer and receiver
1339+
*/
1340+
public function testEagerLoadingNotDuplicateRelation()
1341+
{
1342+
$customer = $this->isOrm() ? new Customer() : new CustomerDocument();
1343+
$customer->name = 'customer_name';
1344+
1345+
$address1 = $this->isOrm() ? new Address() : new AddressDocument();
1346+
$address1->name = 'foo';
1347+
$address2 = $this->isOrm() ? new Address() : new AddressDocument();
1348+
$address2->name = 'bar';
1349+
1350+
$order = $this->isOrm() ? new Order() : new OrderDocument();
1351+
$order->recipient = $customer;
1352+
$order->customer = $customer;
1353+
1354+
$customer->addresses->add($address1);
1355+
$customer->addresses->add($address2);
1356+
1357+
$this->manager->persist($address1);
1358+
$this->manager->persist($address2);
1359+
$this->manager->persist($customer);
1360+
$this->manager->persist($order);
1361+
1362+
$this->manager->flush();
1363+
$this->manager->clear();
1364+
}
1365+
12671366
private function isOrm(): bool
12681367
{
12691368
return null !== $this->schemaTool;
@@ -1579,31 +1678,34 @@ private function buildThirdLevel()
15791678
}
15801679

15811680
/**
1582-
* @Given there is a order with same customer and receiver
1681+
* @return ConvertedDate|ConvertedDateDocument
15831682
*/
1584-
public function testEagerLoadingNotDuplicateRelation()
1683+
private function buildConvertedDate()
15851684
{
1586-
$customer = $this->isOrm() ? new Customer() : new CustomerDocument();
1587-
$customer->name = 'customer_name';
1588-
1589-
$address1 = $this->isOrm() ? new Address() : new AddressDocument();
1590-
$address1->name = 'foo';
1591-
$address2 = $this->isOrm() ? new Address() : new AddressDocument();
1592-
$address2->name = 'bar';
1593-
1594-
$order = $this->isOrm() ? new Order() : new OrderDocument();
1595-
$order->recipient = $customer;
1596-
$order->customer = $customer;
1685+
return $this->isOrm() ? new ConvertedDate() : new ConvertedDateDocument();
1686+
}
15971687

1598-
$customer->addresses->add($address1);
1599-
$customer->addresses->add($address2);
1688+
/**
1689+
* @return ConvertedBoolean|ConvertedBoolDocument
1690+
*/
1691+
private function buildConvertedBoolean()
1692+
{
1693+
return $this->isOrm() ? new ConvertedBoolean() : new ConvertedBoolDocument();
1694+
}
16001695

1601-
$this->manager->persist($address1);
1602-
$this->manager->persist($address2);
1603-
$this->manager->persist($customer);
1604-
$this->manager->persist($order);
1696+
/**
1697+
* @return ConvertedInteger|ConvertedIntegerDocument
1698+
*/
1699+
private function buildConvertedInteger()
1700+
{
1701+
return $this->isOrm() ? new ConvertedInteger() : new ConvertedIntegerDocument();
1702+
}
16051703

1606-
$this->manager->flush();
1607-
$this->manager->clear();
1704+
/**
1705+
* @return ConvertedString|ConvertedStringDocument
1706+
*/
1707+
private function buildConvertedString()
1708+
{
1709+
return $this->isOrm() ? new ConvertedString() : new ConvertedStringDocument();
16081710
}
16091711
}

features/doctrine/boolean_filter.feature

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,118 @@ Feature: Boolean filter on collections
448448
}
449449
"""
450450
And the JSON node "hydra:totalItems" should be equal to 25
451+
452+
@createSchema
453+
Scenario: Get collection filtered using a name converter
454+
Given there are 5 convertedBoolean objects
455+
When I send a "GET" request to "/converted_booleans?name_converted=false"
456+
Then the response status code should be 200
457+
And the response should be in JSON
458+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
459+
And the JSON should be equal to:
460+
"""
461+
{
462+
"@context": "/contexts/ConvertedBoolean",
463+
"@id": "/converted_booleans",
464+
"@type": "hydra:Collection",
465+
"hydra:member": [
466+
{
467+
"@id": "/converted_booleans/2",
468+
"@type": "ConvertedBoolean",
469+
"name_converted": false,
470+
"id": 2
471+
},
472+
{
473+
"@id": "/converted_booleans/4",
474+
"@type": "ConvertedBoolean",
475+
"name_converted": false,
476+
"id": 4
477+
}
478+
],
479+
"hydra:totalItems": 2,
480+
"hydra:view": {
481+
"@id": "/converted_booleans?name_converted=false",
482+
"@type": "hydra:PartialCollectionView"
483+
},
484+
"hydra:search": {
485+
"@type": "hydra:IriTemplate",
486+
"hydra:template": "/converted_booleans{?name_converted}",
487+
"hydra:variableRepresentation": "BasicRepresentation",
488+
"hydra:mapping": [
489+
{
490+
"@type": "IriTemplateMapping",
491+
"variable": "name_converted",
492+
"property": "name_converted",
493+
"required": false
494+
}
495+
]
496+
}
497+
}
498+
"""
499+
And the JSON should be valid according to this schema:
500+
"""
501+
{
502+
"type": "object",
503+
"properties": {
504+
"@context": {"pattern": "^/contexts/ConvertedBoolean"},
505+
"@id": {"pattern": "^/converted_booleans"},
506+
"@type": {"pattern": "^hydra:Collection$"},
507+
"hydra:member": {
508+
"type": "array",
509+
"items": {
510+
"type": "object",
511+
"properties": {
512+
"@id": {"pattern": "^/converted_booleans/(2|4)$"},
513+
"@type": {"pattern": "^ConvertedBoolean"},
514+
"name_converted": {"type": "boolean"},
515+
"id": {"type": "integer", "minimum":2, "maximum": 4}
516+
},
517+
"required": ["@id", "@type", "name_converted", "id"],
518+
"additionalProperties": false
519+
},
520+
"minItems": 2,
521+
"maxItems": 2,
522+
"uniqueItems": true
523+
},
524+
"hydra:totalItems": {"type": "integer", "minimum": 2, "maximum": 2},
525+
"hydra:view": {
526+
"type": "object",
527+
"properties": {
528+
"@id": {"pattern": "^/converted_booleans\\?name_converted=false"},
529+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
530+
},
531+
"required": ["@id", "@type"],
532+
"additionalProperties": false
533+
},
534+
"hydra:search": {
535+
"type": "object",
536+
"properties": {
537+
"@type": {"pattern": "^hydra:IriTemplate$"},
538+
"hydra:template": {"pattern": "^/converted_booleans\\{\\?name_converted\\}$"},
539+
"hydra:variableRepresentation": {"pattern": "^BasicRepresentation$"},
540+
"hydra:mapping": {
541+
"type": "array",
542+
"items": {
543+
"type": "object",
544+
"properties": {
545+
"@type": {"pattern": "^IriTemplateMapping$"},
546+
"variable": {"pattern": "^name_converted$"},
547+
"property": {"pattern": "^name_converted$"},
548+
"required": {"type": "boolean"}
549+
},
550+
"required": ["@type", "variable", "property", "required"],
551+
"additionalProperties": false
552+
},
553+
"minItems": 1,
554+
"maxItems": 1,
555+
"uniqueItems": true
556+
}
557+
},
558+
"additionalProperties": false,
559+
"required": ["@type", "hydra:template", "hydra:variableRepresentation", "hydra:mapping"]
560+
},
561+
"additionalProperties": false,
562+
"required": ["@context", "@id", "@type", "hydra:member", "hydra:totalItems", "hydra:view", "hydra:search"]
563+
}
564+
}
565+
"""

0 commit comments

Comments
 (0)