Skip to content

Commit 83d4dfa

Browse files
committed
test: add Behat tests
1 parent dada34a commit 83d4dfa

File tree

8 files changed

+181
-36
lines changed

8 files changed

+181
-36
lines changed

features/doctrine/search_filter.feature

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,44 @@ Feature: Search filter on collections
539539
}
540540
"""
541541

542+
@!mongodb
543+
Scenario: Search collection by binary UUID (Ramsey)
544+
Given there is a ramsey identified resource with binary uuid "c19900a9-d2b2-45bf-b040-05c72d321282"
545+
And there is a ramsey identified resource with binary uuid "a96cb2ed-e3dc-4449-9842-830e770cdecc"
546+
When I send a "GET" request to "/ramsey_uuid_binary_dummies?id=c19900a9-d2b2-45bf-b040-05c72d321282"
547+
Then the response status code should be 200
548+
And the response should be in JSON
549+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
550+
And the JSON node "hydra:totalItems" should be equal to "1"
551+
552+
@!mongodb
553+
Scenario: Search collection by binary UUID (Ramsey) (multiple values)
554+
Given there is a ramsey identified resource with binary uuid "f71a6469-1bfc-4945-bad1-d6092f09a8c3"
555+
When I send a "GET" request to "/ramsey_uuid_binary_dummies?id[]=c19900a9-d2b2-45bf-b040-05c72d321282&id[]=f71a6469-1bfc-4945-bad1-d6092f09a8c3"
556+
Then the response status code should be 200
557+
And the response should be in JSON
558+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
559+
And the JSON node "hydra:totalItems" should be equal to "2"
560+
561+
@!mongodb
562+
Scenario: Search collection by related binary UUID (Ramsey)
563+
Given there is a ramsey identified resource with binary uuid "56fa36c3-2b5e-4813-9e3a-b0bbe2ab5553" having a related resource with binary uuid "02227dc6-a371-4b8b-a34c-bbbf921b8ebd"
564+
And there is a ramsey identified resource with binary uuid "4d796212-4b26-4e19-b092-a32d990b1e7e" having a related resource with binary uuid "31f64c33-6061-4fc1-b0e8-f4711b607c7d"
565+
When I send a "GET" request to "/ramsey_uuid_binary_dummies?relateds=02227dc6-a371-4b8b-a34c-bbbf921b8ebd"
566+
Then the response status code should be 200
567+
And the response should be in JSON
568+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
569+
And the JSON node "hydra:totalItems" should be equal to "1"
570+
571+
@!mongodb
572+
Scenario: Search collection by related binary UUID (Ramsey) (multiple values)
573+
Given there is a ramsey identified resource with binary uuid "3248c908-a89d-483a-b75f-25888730d391" having a related resource with binary uuid "d7b2e909-37b0-411e-814c-74e044afbccb"
574+
When I send a "GET" request to "/ramsey_uuid_binary_dummies?relateds[]=02227dc6-a371-4b8b-a34c-bbbf921b8ebd&relateds[]=d7b2e909-37b0-411e-814c-74e044afbccb"
575+
Then the response status code should be 200
576+
And the response should be in JSON
577+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
578+
And the JSON node "hydra:totalItems" should be equal to "2"
579+
542580
Scenario: Search for entities within an impossible range
543581
When I send a "GET" request to "/dummies?name=MuYm"
544582
Then the response status code should be 200

src/Bridge/Doctrine/Orm/Filter/SearchFilter.php

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -151,85 +151,69 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
151151
$associationField = $associationFieldIdentifier;
152152
}
153153

154-
/*
155-
* If field type is string/float, Doctrine does not call convertToDatabaseValueSQL() because it
156-
* does not know it needs conversion.
157-
* This would lead to incorrect values for Ramsey\Uuid\Doctrine\UuidBinaryType for example.
158-
* The only fix is to provide field type to doctrine ...
159-
* It's easy if setParameter() sets only one value BUT impossible if multiple values are provided.
160-
* The only way to do this is to rewrite the IN() statement to multiple values
161-
* and map a global setParameters()
162-
*/
163154
$type = $metadata->getTypeOfField($associationField);
164-
$nbValues = \count($values);
165155

166-
if (1 === $nbValues) {
156+
if (1 === \count($values)) {
167157
$queryBuilder
168158
->andWhere($queryBuilder->expr()->eq($associationAlias.'.'.$associationField, ':'.$valueParameter))
169159
->setParameter($valueParameter, $values[0], $type);
170-
} else {
171-
// get current parameters, because QueryBuilder->setParameters() erase previous parameters set
172-
$parameters = $queryBuilder->getParameters();
173-
$inQuery = [];
174160

175-
// convertToDatabaseValue() can only convert one value at a time ... We can no longer pass an array of values, we should use multiple values
161+
return;
162+
}
176163

177-
for ($i = 0; $i < $nbValues; ++$i) {
178-
$inQuery[] = ':'.$valueParameter;
179-
$parameters->add(new Parameter($valueParameter, $values[$i], $type));
180-
$valueParameter = $queryNameGenerator->generateParameterName($associationField);
181-
}
164+
$parameters = $queryBuilder->getParameters();
165+
$inQuery = [];
182166

183-
// we cannot use expr->in() here because it considers $inQuery parameters as strings.
184-
$queryBuilder
185-
->andWhere($associationAlias.'.'.$associationField.' IN ('.implode(', ', $inQuery).')')
186-
->setParameters($parameters);
167+
foreach ($values as $val) {
168+
$inQuery[] = ':'.$valueParameter;
169+
$parameters->add(new Parameter($valueParameter, $val, $type));
170+
$valueParameter = $queryNameGenerator->generateParameterName($associationField);
187171
}
172+
173+
$queryBuilder
174+
->andWhere($associationAlias.'.'.$associationField.' IN ('.implode(', ', $inQuery).')')
175+
->setParameters($parameters);
188176
}
189177

190178
/**
191179
* Adds where clause according to the strategy.
192180
*
193181
* @throws InvalidArgumentException If strategy does not exist
194182
*/
195-
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $values, bool $caseSensitive/*, ClassMetadata $metadata = null*/)
183+
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $values, bool $caseSensitive/*, ClassMetadata $metadata*/)
196184
{
197-
// check if we have metadata
198185
if (\func_num_args() > 7 && ($metadata = func_get_arg(7)) instanceof ClassMetadata) {
199186
$type = $metadata->getTypeOfField($field);
200187
} else {
201188
@trigger_error(sprintf('Method %s() will have a 8th argument `$metadata` in version API Platform 3.0.', __FUNCTION__), \E_USER_DEPRECATED);
202-
$type = null; // default setParameter() value
189+
$type = null;
203190
}
204191

205192
if (!\is_array($values)) {
206193
$values = [$values];
207194
}
208195

209-
$nbValues = \count($values);
210196
$wrapCase = $this->createWrapCase($caseSensitive);
211197
$valueParameter = ':'.$queryNameGenerator->generateParameterName($field);
212198
$aliasedField = sprintf('%s.%s', $alias, $field);
213199

214-
if (null == $strategy || self::STRATEGY_EXACT == $strategy) {
215-
if (1 == $nbValues) {
200+
if (self::STRATEGY_EXACT === $strategy) {
201+
if (1 === \count($values)) {
216202
$queryBuilder
217203
->andWhere($queryBuilder->expr()->eq($wrapCase($aliasedField), $wrapCase($valueParameter)))
218204
->setParameter($valueParameter, $values[0], $type);
219205

220206
return;
221207
}
222208

223-
// get current parameters, because QueryBuilder->setParameters() erase previous parameters set
224209
$parameters = $queryBuilder->getParameters();
225210
$inQuery = [];
226-
for ($i = 0; $i < $nbValues; ++$i) {
211+
foreach ($values as $value) {
227212
$inQuery[] = $valueParameter;
228-
$parameters->add(new Parameter($valueParameter, $caseSensitive ? $values[$i] : strtolower($values[$i]), $type));
213+
$parameters->add(new Parameter($valueParameter, $caseSensitive ? $value : strtolower($value), $type));
229214
$valueParameter = ':'.$queryNameGenerator->generateParameterName($field);
230215
}
231216

232-
// we cannot use expr->in() here because it considers $inQuery parameters as strings.
233217
$queryBuilder
234218
->andWhere($wrapCase($aliasedField).' IN ('.implode(', ', $inQuery).')')
235219
->setParameters($parameters);

tests/Behat/DoctrineContext.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Pet;
140140
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Product;
141141
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Question;
142+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RamseyUuidBinaryDummy;
142143
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RamseyUuidDummy;
143144
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
144145
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy;
@@ -1306,6 +1307,35 @@ public function thereIsARamseyIdentifiedResource(string $uuid)
13061307
$this->manager->flush();
13071308
}
13081309

1310+
/**
1311+
* @Given there is a ramsey identified resource with binary uuid :uuid
1312+
*/
1313+
public function thereIsARamseyIdentifiedResourceWithBinaryUuid(string $uuid)
1314+
{
1315+
$dummy = new RamseyUuidBinaryDummy();
1316+
$dummy->setId($uuid);
1317+
1318+
$this->manager->persist($dummy);
1319+
$this->manager->flush();
1320+
}
1321+
1322+
/**
1323+
* @Given there is a ramsey identified resource with binary uuid :uuid having a related resource with binary uuid :uuid_related
1324+
*/
1325+
public function thereIsARamseyIdentifiedResourceWithBinaryUuidHavingARelatedResourceWithBinaryUuid(string $uuid, string $uuidRelated)
1326+
{
1327+
$related = new RamseyUuidBinaryDummy();
1328+
$related->setId($uuidRelated);
1329+
1330+
$dummy = new RamseyUuidBinaryDummy();
1331+
$dummy->setId($uuid);
1332+
$dummy->addRelated($related);
1333+
1334+
$this->manager->persist($related);
1335+
$this->manager->persist($dummy);
1336+
$this->manager->flush();
1337+
}
1338+
13091339
/**
13101340
* @Given there is a dummy object with a fourth level relation
13111341
*/
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiFilter;
17+
use ApiPlatform\Core\Annotation\ApiResource;
18+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
19+
use Doctrine\Common\Collections\ArrayCollection;
20+
use Doctrine\Common\Collections\Collection;
21+
use Doctrine\ORM\Mapping as ORM;
22+
use Ramsey\Uuid\Uuid;
23+
use Ramsey\Uuid\UuidInterface;
24+
25+
/**
26+
* @ORM\Entity
27+
* @ApiResource
28+
* @ApiFilter(SearchFilter::class, properties={"id": "exact", "relateds": "exact"})
29+
*/
30+
class RamseyUuidBinaryDummy
31+
{
32+
/**
33+
* @var UuidInterface
34+
*
35+
* @ORM\Id
36+
* @ORM\Column(type="uuid_binary", unique=true)
37+
*/
38+
private $id;
39+
40+
/**
41+
* @var Collection<RamseyUuidBinaryDummy>
42+
*
43+
* @ORM\OneToMany(targetEntity="RamseyUuidBinaryDummy", mappedBy="relatedParent")
44+
*/
45+
private $relateds;
46+
47+
/**
48+
* @var ?RamseyUuidBinaryDummy
49+
*
50+
* @ORM\ManyToOne(targetEntity="RamseyUuidBinaryDummy", inversedBy="relateds")
51+
*/
52+
private $relatedParent;
53+
54+
public function __construct()
55+
{
56+
$this->relateds = new ArrayCollection();
57+
}
58+
59+
public function getId(): UuidInterface
60+
{
61+
return $this->id;
62+
}
63+
64+
public function setId(string $uuid): void
65+
{
66+
$this->id = Uuid::fromString($uuid);
67+
}
68+
69+
public function getRelateds(): Collection
70+
{
71+
return $this->relateds;
72+
}
73+
74+
public function addRelated(RamseyUuidBinaryDummy $dummy): void
75+
{
76+
$this->relateds->add($dummy);
77+
$dummy->setRelatedParent($this);
78+
}
79+
80+
public function getRelatedParent(): ?RamseyUuidBinaryDummy
81+
{
82+
return $this->relatedParent;
83+
}
84+
85+
public function setRelatedParent(RamseyUuidBinaryDummy $dummy): void
86+
{
87+
$this->relatedParent = $dummy;
88+
}
89+
}

tests/Fixtures/app/config/config_common.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ doctrine:
88
path: '%kernel.cache_dir%/db.sqlite'
99
charset: 'UTF8'
1010
types:
11-
uuid: Ramsey\Uuid\Doctrine\UuidType
11+
uuid: Ramsey\Uuid\Doctrine\UuidType
12+
uuid_binary: Ramsey\Uuid\Doctrine\UuidBinaryType
1213

1314
orm:
1415
auto_generate_proxy_classes: '%kernel.debug%'

tests/Fixtures/app/config/config_mysql.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ doctrine:
1313
server_version: '%env(MYSQL_VERSION)%'
1414
types:
1515
uuid: Ramsey\Uuid\Doctrine\UuidType
16+
uuid_binary: Ramsey\Uuid\Doctrine\UuidBinaryType

tests/Fixtures/app/config/config_postgres.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ doctrine:
1313
server_version: '%env(POSTGRES_VERSION)%'
1414
types:
1515
uuid: Ramsey\Uuid\Doctrine\UuidType
16+
uuid_binary: Ramsey\Uuid\Doctrine\UuidBinaryType

tests/Fixtures/app/config/config_sqlite.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ doctrine:
1010
url: '%env(resolve:DATABASE_URL)%'
1111
types:
1212
uuid: Ramsey\Uuid\Doctrine\UuidType
13+
uuid_binary: Ramsey\Uuid\Doctrine\UuidBinaryType

0 commit comments

Comments
 (0)