|
| 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\Tests\Functional\Parameters; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Document\Chicken as DocumentChicken; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Document\ChickenCoop as DocumentChickenCoop; |
| 19 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Chicken; |
| 20 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ChickenCoop; |
| 21 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 22 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 23 | +use Doctrine\ODM\MongoDB\MongoDBException; |
| 24 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 25 | +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; |
| 26 | +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; |
| 27 | +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
| 28 | +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
| 29 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 30 | + |
| 31 | +final class OrFilterTest extends ApiTestCase |
| 32 | +{ |
| 33 | + use RecreateSchemaTrait; |
| 34 | + use SetupClassResourcesTrait; |
| 35 | + |
| 36 | + public static function getResources(): array |
| 37 | + { |
| 38 | + return [Chicken::class, ChickenCoop::class]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @throws \Throwable |
| 43 | + * @throws MongoDBException |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $entities = $this->isMongoDB() |
| 48 | + ? [DocumentChicken::class, DocumentChickenCoop::class] |
| 49 | + : [Chicken::class, ChickenCoop::class]; |
| 50 | + |
| 51 | + $this->recreateSchema($entities); |
| 52 | + $this->loadFixtures(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @throws RedirectionExceptionInterface |
| 57 | + * @throws DecodingExceptionInterface |
| 58 | + * @throws ClientExceptionInterface |
| 59 | + * @throws TransportExceptionInterface |
| 60 | + * @throws ServerExceptionInterface |
| 61 | + */ |
| 62 | + #[DataProvider('filterDataProvider')] |
| 63 | + public function testOrFilter(string $url, int $expectedCount, array $expectedNames): void |
| 64 | + { |
| 65 | + $client = self::createClient(); |
| 66 | + $client->request('GET', $url); |
| 67 | + |
| 68 | + $this->assertResponseIsSuccessful(); |
| 69 | + $this->assertJsonContains(['hydra:totalItems' => $expectedCount]); |
| 70 | + |
| 71 | + if ($expectedCount > 0) { |
| 72 | + $names = array_column($client->getResponse()->toArray()['hydra:member'], 'name'); |
| 73 | + sort($names); |
| 74 | + sort($expectedNames); |
| 75 | + $this->assertSame($expectedNames, $names); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public static function filterDataProvider(): \Generator |
| 80 | + { |
| 81 | + yield 'filtre par ID du poulailler de Gertrude' => [ |
| 82 | + 'url' => '/chickens?relation=1', |
| 83 | + 'expectedCount' => 1, |
| 84 | + 'expectedNames' => ['Gertrude'], |
| 85 | + ]; |
| 86 | + |
| 87 | + yield 'filtre par IRI du poulailler de Gertrude' => [ |
| 88 | + 'url' => '/chickens?relation=/chicken_coops/1', |
| 89 | + 'expectedCount' => 1, |
| 90 | + 'expectedNames' => ['Gertrude'], |
| 91 | + ]; |
| 92 | + |
| 93 | + yield 'filtre par ID du poulailler de Henriette' => [ |
| 94 | + 'url' => '/chickens?relation=2', |
| 95 | + 'expectedCount' => 1, |
| 96 | + 'expectedNames' => ['Henriette'], |
| 97 | + ]; |
| 98 | + |
| 99 | + yield 'filtre par IRI du poulailler de Henriette' => [ |
| 100 | + 'url' => '/chickens?relation=/chicken_coops/2', |
| 101 | + 'expectedCount' => 1, |
| 102 | + 'expectedNames' => ['Henriette'], |
| 103 | + ]; |
| 104 | + |
| 105 | + yield 'filtre avec un ID inexistant' => [ |
| 106 | + 'url' => '/chickens?relation=999', |
| 107 | + 'expectedCount' => 0, |
| 108 | + 'expectedNames' => [], |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @throws \Throwable |
| 114 | + * @throws MongoDBException |
| 115 | + */ |
| 116 | + private function loadFixtures(): void |
| 117 | + { |
| 118 | + $manager = $this->getManager(); |
| 119 | + $chickenClass = $this->isMongoDB() ? DocumentChicken::class : Chicken::class; |
| 120 | + $coopClass = $this->isMongoDB() ? DocumentChickenCoop::class : ChickenCoop::class; |
| 121 | + |
| 122 | + $chickenCoop1 = new $coopClass(); |
| 123 | + $chickenCoop2 = new $coopClass(); |
| 124 | + $manager->persist($chickenCoop1); |
| 125 | + $manager->persist($chickenCoop2); |
| 126 | + |
| 127 | + $chicken1 = new $chickenClass(); |
| 128 | + $chicken1->setName('Gertrude'); |
| 129 | + $chicken1->setChickenCoop($chickenCoop1); |
| 130 | + $manager->persist($chicken1); |
| 131 | + |
| 132 | + $chicken2 = new $chickenClass(); |
| 133 | + $chicken2->setName('Henriette'); |
| 134 | + $chicken2->setChickenCoop($chickenCoop2); |
| 135 | + $manager->persist($chicken2); |
| 136 | + |
| 137 | + $manager->flush(); |
| 138 | + } |
| 139 | +} |
0 commit comments