|
| 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\ConvertedDateParameter; |
| 18 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 19 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests that legacy AbstractFilter subclasses (DateFilter, etc.) work correctly |
| 23 | + * with QueryParameter when a nameConverter is configured. |
| 24 | + * |
| 25 | + * @see https://github.com/api-platform/core/issues/7866 |
| 26 | + */ |
| 27 | +final class NameConverterFilterTest extends ApiTestCase |
| 28 | +{ |
| 29 | + use RecreateSchemaTrait; |
| 30 | + use SetupClassResourcesTrait; |
| 31 | + |
| 32 | + protected static ?bool $alwaysBootKernel = false; |
| 33 | + |
| 34 | + /** |
| 35 | + * @return class-string[] |
| 36 | + */ |
| 37 | + public static function getResources(): array |
| 38 | + { |
| 39 | + return [ConvertedDateParameter::class]; |
| 40 | + } |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $this->recreateSchema([ConvertedDateParameter::class]); |
| 45 | + $this->loadFixtures(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testDateFilterWithNameConverter(): void |
| 49 | + { |
| 50 | + $response = self::createClient()->request('GET', '/converted_date_parameters?nameConverted[after]=2025-01-15'); |
| 51 | + $this->assertResponseIsSuccessful(); |
| 52 | + $members = $response->toArray()['hydra:member']; |
| 53 | + $this->assertCount(2, $members); |
| 54 | + } |
| 55 | + |
| 56 | + public function testDateFilterBeforeWithNameConverter(): void |
| 57 | + { |
| 58 | + $response = self::createClient()->request('GET', '/converted_date_parameters?nameConverted[before]=2025-01-15'); |
| 59 | + $this->assertResponseIsSuccessful(); |
| 60 | + $members = $response->toArray()['hydra:member']; |
| 61 | + $this->assertCount(1, $members); |
| 62 | + } |
| 63 | + |
| 64 | + private function loadFixtures(): void |
| 65 | + { |
| 66 | + $manager = $this->getManager(); |
| 67 | + |
| 68 | + foreach (['2025-01-01', '2025-02-01', '2025-03-01'] as $date) { |
| 69 | + $entity = new ConvertedDateParameter(); |
| 70 | + $entity->nameConverted = new \DateTime($date); |
| 71 | + $manager->persist($entity); |
| 72 | + } |
| 73 | + |
| 74 | + $manager->flush(); |
| 75 | + } |
| 76 | +} |
0 commit comments