|
| 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\Parameters; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Chicken; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ChickenCoop; |
| 19 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Owner; |
| 20 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 21 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 22 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 23 | + |
| 24 | +final class ComparisonFilterTest extends ApiTestCase |
| 25 | +{ |
| 26 | + use RecreateSchemaTrait; |
| 27 | + use SetupClassResourcesTrait; |
| 28 | + |
| 29 | + protected static ?bool $alwaysBootKernel = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * @return class-string[] |
| 33 | + */ |
| 34 | + public static function getResources(): array |
| 35 | + { |
| 36 | + return [Chicken::class, ChickenCoop::class, Owner::class]; |
| 37 | + } |
| 38 | + |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + $this->recreateSchema([Chicken::class, ChickenCoop::class, Owner::class]); |
| 42 | + $this->loadFixtures(); |
| 43 | + } |
| 44 | + |
| 45 | + #[DataProvider('comparisonFilterProvider')] |
| 46 | + public function testComparisonFilter(string $url, int $expectedCount, array $expectedNames): void |
| 47 | + { |
| 48 | + $response = self::createClient()->request('GET', $url); |
| 49 | + $this->assertResponseIsSuccessful(); |
| 50 | + |
| 51 | + $responseData = $response->toArray(); |
| 52 | + $filteredItems = $responseData['member']; |
| 53 | + |
| 54 | + $this->assertCount($expectedCount, $filteredItems, \sprintf('Expected %d items for URL %s', $expectedCount, $url)); |
| 55 | + |
| 56 | + $names = array_map(static fn ($chicken) => $chicken['name'], $filteredItems); |
| 57 | + sort($names); |
| 58 | + sort($expectedNames); |
| 59 | + |
| 60 | + $this->assertSame($expectedNames, $names, 'The names do not match the expected values.'); |
| 61 | + } |
| 62 | + |
| 63 | + public static function comparisonFilterProvider(): \Generator |
| 64 | + { |
| 65 | + // We create 4 chickens with IDs 1-4: Alpha, Bravo, Charlie, Delta |
| 66 | + yield 'gt: id > 2 returns chickens 3,4' => [ |
| 67 | + '/chickens?idComparison[gt]=2', |
| 68 | + 2, |
| 69 | + ['Charlie', 'Delta'], |
| 70 | + ]; |
| 71 | + |
| 72 | + yield 'gte: id >= 2 returns chickens 2,3,4' => [ |
| 73 | + '/chickens?idComparison[gte]=2', |
| 74 | + 3, |
| 75 | + ['Bravo', 'Charlie', 'Delta'], |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'lt: id < 3 returns chickens 1,2' => [ |
| 79 | + '/chickens?idComparison[lt]=3', |
| 80 | + 2, |
| 81 | + ['Alpha', 'Bravo'], |
| 82 | + ]; |
| 83 | + |
| 84 | + yield 'lte: id <= 3 returns chickens 1,2,3' => [ |
| 85 | + '/chickens?idComparison[lte]=3', |
| 86 | + 3, |
| 87 | + ['Alpha', 'Bravo', 'Charlie'], |
| 88 | + ]; |
| 89 | + |
| 90 | + yield 'between: id between 2..3 returns chickens 2,3' => [ |
| 91 | + '/chickens?idComparison[between]=2..3', |
| 92 | + 2, |
| 93 | + ['Bravo', 'Charlie'], |
| 94 | + ]; |
| 95 | + |
| 96 | + yield 'between equal values: id between 2..2 returns chicken 2 (equality)' => [ |
| 97 | + '/chickens?idComparison[between]=2..2', |
| 98 | + 1, |
| 99 | + ['Bravo'], |
| 100 | + ]; |
| 101 | + |
| 102 | + yield 'combined gt and lt: id > 1 AND id < 4 returns chickens 2,3' => [ |
| 103 | + '/chickens?idComparison[gt]=1&idComparison[lt]=4', |
| 104 | + 2, |
| 105 | + ['Bravo', 'Charlie'], |
| 106 | + ]; |
| 107 | + |
| 108 | + yield 'gt with no results: id > 100 returns nothing' => [ |
| 109 | + '/chickens?idComparison[gt]=100', |
| 110 | + 0, |
| 111 | + [], |
| 112 | + ]; |
| 113 | + |
| 114 | + yield 'gte with large range returns all' => [ |
| 115 | + '/chickens?idComparison[gte]=1&itemsPerPage=10', |
| 116 | + 4, |
| 117 | + ['Alpha', 'Bravo', 'Charlie', 'Delta'], |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + private function loadFixtures(): void |
| 122 | + { |
| 123 | + $manager = $this->getManager(); |
| 124 | + |
| 125 | + $owner = new Owner(); |
| 126 | + $owner->setName('TestOwner'); |
| 127 | + $manager->persist($owner); |
| 128 | + |
| 129 | + $coop = new ChickenCoop(); |
| 130 | + $manager->persist($coop); |
| 131 | + |
| 132 | + foreach (['Alpha', 'Bravo', 'Charlie', 'Delta'] as $name) { |
| 133 | + $chicken = new Chicken(); |
| 134 | + $chicken->setName($name); |
| 135 | + $chicken->setEan('000000000000'); |
| 136 | + $chicken->setChickenCoop($coop); |
| 137 | + $chicken->setOwner($owner); |
| 138 | + $coop->addChicken($chicken); |
| 139 | + $manager->persist($chicken); |
| 140 | + } |
| 141 | + |
| 142 | + $manager->flush(); |
| 143 | + } |
| 144 | +} |
0 commit comments