|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Validator\Constraints\CssColor; |
| 16 | +use Symfony\Component\Validator\Exception\InvalidArgumentException; |
| 17 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 18 | +use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 22 | + */ |
| 23 | +class CssColorTest extends TestCase |
| 24 | +{ |
| 25 | + public function testConstructorStrict() |
| 26 | + { |
| 27 | + $subject = new CssColor(['mode' => CssColor::VALIDATION_MODE_SHORT]); |
| 28 | + |
| 29 | + $this->assertEquals(CssColor::VALIDATION_MODE_SHORT, $subject->mode); |
| 30 | + } |
| 31 | + |
| 32 | + public function testUnknownModesTriggerException() |
| 33 | + { |
| 34 | + $this->expectException(InvalidArgumentException::class); |
| 35 | + $this->expectExceptionMessage('The "mode" parameter value is not valid.'); |
| 36 | + new CssColor(['mode' => 'Unknown Mode']); |
| 37 | + } |
| 38 | + |
| 39 | + public function testNormalizerCanBeSet() |
| 40 | + { |
| 41 | + $hexaColor = new CssColor(['normalizer' => 'trim']); |
| 42 | + |
| 43 | + $this->assertEquals('trim', $hexaColor->normalizer); |
| 44 | + } |
| 45 | + |
| 46 | + public function testInvalidNormalizerThrowsException() |
| 47 | + { |
| 48 | + $this->expectException(InvalidArgumentException::class); |
| 49 | + $this->expectExceptionMessage('The "normalizer" option must be a valid callable ("string" given).'); |
| 50 | + new CssColor(['normalizer' => 'Unknown Callable']); |
| 51 | + } |
| 52 | + |
| 53 | + public function testInvalidNormalizerObjectThrowsException() |
| 54 | + { |
| 55 | + $this->expectException(InvalidArgumentException::class); |
| 56 | + $this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).'); |
| 57 | + new CssColor(['normalizer' => new \stdClass()]); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @requires PHP 8 |
| 62 | + */ |
| 63 | + public function testAttribute() |
| 64 | + { |
| 65 | + $metadata = new ClassMetadata(CssColorDummy::class); |
| 66 | + (new AnnotationLoader())->loadClassMetadata($metadata); |
| 67 | + |
| 68 | + [$aConstraint] = $metadata->properties['a']->constraints; |
| 69 | + self::assertNull($aConstraint->mode); |
| 70 | + self::assertNull($aConstraint->normalizer); |
| 71 | + |
| 72 | + [$bConstraint] = $metadata->properties['b']->constraints; |
| 73 | + self::assertSame('myMessage', $bConstraint->message); |
| 74 | + self::assertSame(CssColor::VALIDATION_MODE_HTML5, $bConstraint->mode); |
| 75 | + self::assertSame('trim', $bConstraint->normalizer); |
| 76 | + |
| 77 | + [$cConstraint] = $metadata->properties['c']->getConstraints(); |
| 78 | + self::assertSame(['my_group'], $cConstraint->groups); |
| 79 | + self::assertSame('some attached data', $cConstraint->payload); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class CssColorDummy |
| 84 | +{ |
| 85 | + #[CssColor] |
| 86 | + private $a; |
| 87 | + |
| 88 | + #[CssColor(message: 'myMessage', mode: CssColor::VALIDATION_MODE_HTML5, normalizer: 'trim')] |
| 89 | + private $b; |
| 90 | + |
| 91 | + #[CssColor(groups: ['my_group'], payload: 'some attached data')] |
| 92 | + private $c; |
| 93 | +} |
0 commit comments