|
| 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\Symfony\Validator\Metadata\Property\Restriction; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiProperty; |
| 17 | +use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaCssColorRestriction; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Symfony\Component\Validator\Constraints\CssColor; |
| 21 | +use Symfony\Component\Validator\Constraints\PositiveOrZero; |
| 22 | + |
| 23 | +final class PropertySchemaCssColorRestrictionTest extends TestCase |
| 24 | +{ |
| 25 | + private PropertySchemaCssColorRestriction $restriction; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + $this->restriction = new PropertySchemaCssColorRestriction(); |
| 30 | + } |
| 31 | + |
| 32 | + #[DataProvider('supportsProvider')] |
| 33 | + public function testSupports(mixed $constraint, ApiProperty $propertyMetadata, bool $expected): void |
| 34 | + { |
| 35 | + self::assertSame($expected, $this->restriction->supports($constraint, $propertyMetadata)); |
| 36 | + } |
| 37 | + |
| 38 | + public static function supportsProvider(): \Generator |
| 39 | + { |
| 40 | + yield 'supported CssColor' => [new CssColor(), new ApiProperty(), true]; |
| 41 | + yield 'unsupported other constraint' => [new PositiveOrZero(), new ApiProperty(), false]; |
| 42 | + } |
| 43 | + |
| 44 | + #[DataProvider('createProvider')] |
| 45 | + public function testCreate(CssColor $constraint, string $expectedPattern): void |
| 46 | + { |
| 47 | + $property = new ApiProperty(); |
| 48 | + $result = $this->restriction->create($constraint, $property); |
| 49 | + |
| 50 | + self::assertArrayHasKey('pattern', $result); |
| 51 | + self::assertSame($expectedPattern, $result['pattern']); |
| 52 | + } |
| 53 | + |
| 54 | + public static function createProvider(): \Generator |
| 55 | + { |
| 56 | + yield 'HEX_LONG only' => [ |
| 57 | + new CssColor(formats: [CssColor::HEX_LONG]), |
| 58 | + '^(#[0-9a-f]{6})$', |
| 59 | + ]; |
| 60 | + |
| 61 | + yield 'HEX_SHORT and KEYWORDS' => [ |
| 62 | + new CssColor(formats: [CssColor::HEX_SHORT, CssColor::KEYWORDS]), |
| 63 | + '^(#[0-9a-f]{3}|(transparent|currentColor))$', |
| 64 | + ]; |
| 65 | + |
| 66 | + yield 'RGB + NAMED COLORS' => [ |
| 67 | + new CssColor(formats: [CssColor::RGB, CssColor::BASIC_NAMED_COLORS]), |
| 68 | + '^(rgb\\(\\s*(0|255|25[0-4]|2[0-4]\\d|1\\d\\d|0?\\d?\\d),\\s*(0|255|25[0-4]|2[0-4]\\d|1\\d\\d|0?\\d?\\d),\\s*(0|255|25[0-4]|2[0-4]\\d|1\\d\\d|0?\\d?\\d)\\s*\\)|(black|silver|gray|white|maroon|red|purple|fuchsia|green|lime|olive|yellow|navy|blue|teal|aqua))$', |
| 69 | + ]; |
| 70 | + } |
| 71 | +} |
0 commit comments