|
| 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; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7126\DummyForBackedEnumFilter; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7126\IntegerBackedEnum; |
| 19 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7126\StringBackedEnum; |
| 20 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 21 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 22 | + |
| 23 | +final class BackedEnumFilterTest extends ApiTestCase |
| 24 | +{ |
| 25 | + use RecreateSchemaTrait; |
| 26 | + use SetupClassResourcesTrait; |
| 27 | + |
| 28 | + protected static ?bool $alwaysBootKernel = false; |
| 29 | + |
| 30 | + /** |
| 31 | + * @return class-string[] |
| 32 | + */ |
| 33 | + public static function getResources(): array |
| 34 | + { |
| 35 | + return [DummyForBackedEnumFilter::class]; |
| 36 | + } |
| 37 | + |
| 38 | + public function testFilterStringBackedEnum(): void |
| 39 | + { |
| 40 | + if ($this->isMongoDB()) { |
| 41 | + $this->markTestSkipped(); |
| 42 | + } |
| 43 | + |
| 44 | + $this->recreateSchema($this->getResources()); |
| 45 | + $this->loadFixtures(); |
| 46 | + $route = 'backed_enum_filter'; |
| 47 | + $response = self::createClient()->request('GET', $route.'?stringBackedEnum='.StringBackedEnum::One->value); |
| 48 | + $a = $response->toArray(); |
| 49 | + $this->assertCount(1, $a['hydra:member']); |
| 50 | + $this->assertEquals(StringBackedEnum::One->value, $a['hydra:member'][0]['stringBackedEnum']); |
| 51 | + } |
| 52 | + |
| 53 | + public function testFilterIntegerBackedEnum(): void |
| 54 | + { |
| 55 | + if ($this->isMongoDB()) { |
| 56 | + $this->markTestSkipped(); |
| 57 | + } |
| 58 | + |
| 59 | + $this->recreateSchema($this->getResources()); |
| 60 | + $this->loadFixtures(); |
| 61 | + $route = 'backed_enum_filter'; |
| 62 | + $response = self::createClient()->request('GET', $route.'?integerBackedEnum='.IntegerBackedEnum::Two->value); |
| 63 | + $a = $response->toArray(); |
| 64 | + $this->assertCount(1, $a['hydra:member']); |
| 65 | + $this->assertEquals(IntegerBackedEnum::Two->value, $a['hydra:member'][0]['integerBackedEnum']); |
| 66 | + } |
| 67 | + |
| 68 | + public function loadFixtures(): void |
| 69 | + { |
| 70 | + $container = static::$kernel->getContainer(); |
| 71 | + $registry = $container->get('doctrine'); |
| 72 | + $manager = $registry->getManager(); |
| 73 | + |
| 74 | + $dummyOne = new DummyForBackedEnumFilter(); |
| 75 | + $dummyOne->setStringBackedEnum(StringBackedEnum::One); |
| 76 | + $dummyOne->setIntegerBackedEnum(IntegerBackedEnum::One); |
| 77 | + $manager->persist($dummyOne); |
| 78 | + |
| 79 | + $dummyTwo = new DummyForBackedEnumFilter(); |
| 80 | + $dummyTwo->setStringBackedEnum(StringBackedEnum::Two); |
| 81 | + $dummyTwo->setIntegerBackedEnum(IntegerBackedEnum::Two); |
| 82 | + $manager->persist($dummyTwo); |
| 83 | + |
| 84 | + $manager->flush(); |
| 85 | + } |
| 86 | +} |
0 commit comments