|
| 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\Doctrine\Orm\Tests\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
| 17 | +use ApiPlatform\Doctrine\Orm\Tests\DoctrineOrmFilterTestCase; |
| 18 | +use ApiPlatform\Doctrine\Orm\Tests\Fixtures\CustomConverter; |
| 19 | +use ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\UuidIdentifierDummy; |
| 20 | +use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
| 21 | +use ApiPlatform\Metadata\IriConverterInterface; |
| 22 | +use Doctrine\Persistence\ManagerRegistry; |
| 23 | +use Prophecy\Argument; |
| 24 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Oleksii Polyvanyi <[email protected]> |
| 28 | + */ |
| 29 | +class SearchFilterWithUuidTest extends DoctrineOrmFilterTestCase |
| 30 | +{ |
| 31 | + use ProphecyTrait; |
| 32 | + |
| 33 | + protected string $resourceClass = UuidIdentifierDummy::class; |
| 34 | + protected string $filterClass = SearchFilter::class; |
| 35 | + |
| 36 | + public static function provideApplyTestData(): array |
| 37 | + { |
| 38 | + $filterFactory = self::buildSearchFilter(...); |
| 39 | + $validUuid = '9584fbef-e849-41e3-912b-f2c509874a70'; |
| 40 | + |
| 41 | + return [ |
| 42 | + 'invalid uuid for id' => [ |
| 43 | + [ |
| 44 | + 'id' => 'exact', |
| 45 | + ], |
| 46 | + [ |
| 47 | + 'id' => 'some-invalid-uuid', |
| 48 | + ], |
| 49 | + 'SELECT o FROM ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\UuidIdentifierDummy o', |
| 50 | + [], |
| 51 | + $filterFactory, |
| 52 | + ], |
| 53 | + |
| 54 | + 'valid uuid for id' => [ |
| 55 | + [ |
| 56 | + 'id' => 'exact', |
| 57 | + ], |
| 58 | + [ |
| 59 | + 'id' => $validUuid, |
| 60 | + ], |
| 61 | + 'SELECT o FROM ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\UuidIdentifierDummy o WHERE o.id = :id_p1', |
| 62 | + ['id_p1' => $validUuid], |
| 63 | + $filterFactory, |
| 64 | + ], |
| 65 | + |
| 66 | + 'invalid uuid for uuidField' => [ |
| 67 | + [ |
| 68 | + 'uuidField' => 'exact', |
| 69 | + ], |
| 70 | + [ |
| 71 | + 'uuidField' => 'some-invalid-uuid', |
| 72 | + ], |
| 73 | + 'SELECT o FROM ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\UuidIdentifierDummy o', |
| 74 | + [], |
| 75 | + $filterFactory, |
| 76 | + ], |
| 77 | + |
| 78 | + 'valid uuid for uuidField' => [ |
| 79 | + [ |
| 80 | + 'uuidField' => 'exact', |
| 81 | + ], |
| 82 | + [ |
| 83 | + 'uuidField' => $validUuid, |
| 84 | + ], |
| 85 | + 'SELECT o FROM ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\UuidIdentifierDummy o WHERE o.uuidField = :uuidField_p1', |
| 86 | + ['uuidField_p1' => $validUuid], |
| 87 | + $filterFactory, |
| 88 | + ], |
| 89 | + |
| 90 | + 'invalid uuid for relatedUuidIdentifierDummy' => [ |
| 91 | + [ |
| 92 | + 'relatedUuidIdentifierDummy' => 'exact', |
| 93 | + ], |
| 94 | + [ |
| 95 | + 'relatedUuidIdentifierDummy' => 'some-invalid-uuid', |
| 96 | + ], |
| 97 | + 'SELECT o FROM ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\UuidIdentifierDummy o', |
| 98 | + [], |
| 99 | + $filterFactory, |
| 100 | + ], |
| 101 | + |
| 102 | + 'valid uuid for relatedUuidIdentifierDummy' => [ |
| 103 | + [ |
| 104 | + 'relatedUuidIdentifierDummy' => 'exact', |
| 105 | + ], |
| 106 | + [ |
| 107 | + 'relatedUuidIdentifierDummy' => $validUuid, |
| 108 | + ], |
| 109 | + 'SELECT o FROM ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\UuidIdentifierDummy o WHERE o.relatedUuidIdentifierDummy = :relatedUuidIdentifierDummy_p1', |
| 110 | + ['relatedUuidIdentifierDummy_p1' => $validUuid], |
| 111 | + $filterFactory, |
| 112 | + ], |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
| 116 | + protected static function buildSearchFilter(self $that, ManagerRegistry $managerRegistry, ?array $properties = null): SearchFilter |
| 117 | + { |
| 118 | + $iriConverterProphecy = $that->prophesize(IriConverterInterface::class); |
| 119 | + |
| 120 | + $iriConverterProphecy->getResourceFromIri(Argument::type('string'), ['fetch_data' => false])->will(function () { |
| 121 | + throw new InvalidArgumentException(); |
| 122 | + }); |
| 123 | + |
| 124 | + $iriConverter = $iriConverterProphecy->reveal(); |
| 125 | + $propertyAccessor = static::$kernel->getContainer()->get('test.property_accessor'); |
| 126 | + |
| 127 | + return new SearchFilter($managerRegistry, $iriConverter, $propertyAccessor, null, $properties, null, new CustomConverter()); |
| 128 | + } |
| 129 | +} |
0 commit comments