|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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\Tests\Functional\Uuid; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Uuid\SymfonyUuidDevice; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Uuid\SymfonyUuidDeviceEndpoint; |
| 19 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 20 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 21 | + |
| 22 | +final class UuidComparisonFilterTest extends ApiTestCase |
| 23 | +{ |
| 24 | + use RecreateSchemaTrait; |
| 25 | + use SetupClassResourcesTrait; |
| 26 | + |
| 27 | + protected static ?bool $alwaysBootKernel = false; |
| 28 | + |
| 29 | + /** |
| 30 | + * @return class-string[] |
| 31 | + */ |
| 32 | + public static function getResources(): array |
| 33 | + { |
| 34 | + return [SymfonyUuidDevice::class, SymfonyUuidDeviceEndpoint::class]; |
| 35 | + } |
| 36 | + |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + |
| 41 | + if ($this->isMongoDB()) { |
| 42 | + $this->markTestSkipped('UuidFilter is ORM only.'); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function testGtWithUuid(): void |
| 47 | + { |
| 48 | + $this->recreateSchema(static::getResources()); |
| 49 | + |
| 50 | + $manager = $this->getManager(); |
| 51 | + $manager->persist($device1 = new SymfonyUuidDevice()); |
| 52 | + usleep(1000); |
| 53 | + $manager->persist($device2 = new SymfonyUuidDevice()); |
| 54 | + usleep(1000); |
| 55 | + $manager->persist($device3 = new SymfonyUuidDevice()); |
| 56 | + $manager->flush(); |
| 57 | + |
| 58 | + $uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id]; |
| 59 | + sort($uuids); |
| 60 | + |
| 61 | + $response = self::createClient()->request('GET', '/symfony_uuid_devices', [ |
| 62 | + 'query' => [ |
| 63 | + 'idComparison' => ['gt' => $uuids[1]], |
| 64 | + ], |
| 65 | + ]); |
| 66 | + |
| 67 | + self::assertResponseIsSuccessful(); |
| 68 | + $json = $response->toArray(); |
| 69 | + self::assertSame(1, $json['hydra:totalItems']); |
| 70 | + self::assertSame($uuids[2], $json['hydra:member'][0]['id']); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGteWithUuid(): void |
| 74 | + { |
| 75 | + $this->recreateSchema(static::getResources()); |
| 76 | + |
| 77 | + $manager = $this->getManager(); |
| 78 | + $manager->persist($device1 = new SymfonyUuidDevice()); |
| 79 | + usleep(1000); |
| 80 | + $manager->persist($device2 = new SymfonyUuidDevice()); |
| 81 | + usleep(1000); |
| 82 | + $manager->persist($device3 = new SymfonyUuidDevice()); |
| 83 | + $manager->flush(); |
| 84 | + |
| 85 | + $uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id]; |
| 86 | + sort($uuids); |
| 87 | + |
| 88 | + $response = self::createClient()->request('GET', '/symfony_uuid_devices', [ |
| 89 | + 'query' => [ |
| 90 | + 'idComparison' => ['gte' => $uuids[1]], |
| 91 | + ], |
| 92 | + ]); |
| 93 | + |
| 94 | + self::assertResponseIsSuccessful(); |
| 95 | + $json = $response->toArray(); |
| 96 | + self::assertSame(2, $json['hydra:totalItems']); |
| 97 | + } |
| 98 | + |
| 99 | + public function testLtWithUuid(): void |
| 100 | + { |
| 101 | + $this->recreateSchema(static::getResources()); |
| 102 | + |
| 103 | + $manager = $this->getManager(); |
| 104 | + $manager->persist($device1 = new SymfonyUuidDevice()); |
| 105 | + usleep(1000); |
| 106 | + $manager->persist($device2 = new SymfonyUuidDevice()); |
| 107 | + usleep(1000); |
| 108 | + $manager->persist($device3 = new SymfonyUuidDevice()); |
| 109 | + $manager->flush(); |
| 110 | + |
| 111 | + $uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id]; |
| 112 | + sort($uuids); |
| 113 | + |
| 114 | + $response = self::createClient()->request('GET', '/symfony_uuid_devices', [ |
| 115 | + 'query' => [ |
| 116 | + 'idComparison' => ['lt' => $uuids[1]], |
| 117 | + ], |
| 118 | + ]); |
| 119 | + |
| 120 | + self::assertResponseIsSuccessful(); |
| 121 | + $json = $response->toArray(); |
| 122 | + self::assertSame(1, $json['hydra:totalItems']); |
| 123 | + self::assertSame($uuids[0], $json['hydra:member'][0]['id']); |
| 124 | + } |
| 125 | + |
| 126 | + public function testCombinedGteAndLteWithUuid(): void |
| 127 | + { |
| 128 | + $this->recreateSchema(static::getResources()); |
| 129 | + |
| 130 | + $manager = $this->getManager(); |
| 131 | + $manager->persist($device1 = new SymfonyUuidDevice()); |
| 132 | + usleep(1000); |
| 133 | + $manager->persist($device2 = new SymfonyUuidDevice()); |
| 134 | + usleep(1000); |
| 135 | + $manager->persist($device3 = new SymfonyUuidDevice()); |
| 136 | + usleep(1000); |
| 137 | + $manager->persist($device4 = new SymfonyUuidDevice()); |
| 138 | + $manager->flush(); |
| 139 | + |
| 140 | + $uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id, (string) $device4->id]; |
| 141 | + sort($uuids); |
| 142 | + |
| 143 | + $response = self::createClient()->request('GET', '/symfony_uuid_devices', [ |
| 144 | + 'query' => [ |
| 145 | + 'idComparison' => ['gte' => $uuids[1], 'lte' => $uuids[2]], |
| 146 | + ], |
| 147 | + ]); |
| 148 | + |
| 149 | + self::assertResponseIsSuccessful(); |
| 150 | + $json = $response->toArray(); |
| 151 | + self::assertSame(2, $json['hydra:totalItems']); |
| 152 | + } |
| 153 | + |
| 154 | + public function testNeWithUuid(): void |
| 155 | + { |
| 156 | + $this->recreateSchema(static::getResources()); |
| 157 | + |
| 158 | + $manager = $this->getManager(); |
| 159 | + $manager->persist($device1 = new SymfonyUuidDevice()); |
| 160 | + $manager->persist($device2 = new SymfonyUuidDevice()); |
| 161 | + $manager->persist($device3 = new SymfonyUuidDevice()); |
| 162 | + $manager->flush(); |
| 163 | + |
| 164 | + $response = self::createClient()->request('GET', '/symfony_uuid_devices', [ |
| 165 | + 'query' => [ |
| 166 | + 'idComparison' => ['ne' => (string) $device2->id], |
| 167 | + ], |
| 168 | + ]); |
| 169 | + |
| 170 | + self::assertResponseIsSuccessful(); |
| 171 | + $json = $response->toArray(); |
| 172 | + self::assertSame(2, $json['hydra:totalItems']); |
| 173 | + |
| 174 | + $returnedIds = array_map(static fn ($m) => $m['id'], $json['hydra:member']); |
| 175 | + self::assertNotContains((string) $device2->id, $returnedIds); |
| 176 | + } |
| 177 | + |
| 178 | + protected function tearDown(): void |
| 179 | + { |
| 180 | + if ($this->isMongoDB()) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + $this->recreateSchema(static::getResources()); |
| 185 | + parent::tearDown(); |
| 186 | + } |
| 187 | +} |
0 commit comments