|
| 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 Symfony\Component\Validator\Constraints\Slug; |
| 15 | +use Symfony\Component\Validator\Constraints\SlugValidator; |
| 16 | +use Symfony\Component\Validator\Exception\UnexpectedValueException; |
| 17 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 18 | + |
| 19 | +class SlugValidatorTest extends ConstraintValidatorTestCase |
| 20 | +{ |
| 21 | + protected function createValidator(): SlugValidator |
| 22 | + { |
| 23 | + return new SlugValidator(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testNullIsValid() |
| 27 | + { |
| 28 | + $this->validator->validate(null, new Slug()); |
| 29 | + |
| 30 | + $this->assertNoViolation(); |
| 31 | + } |
| 32 | + |
| 33 | + public function testEmptyStringIsValid() |
| 34 | + { |
| 35 | + $this->validator->validate('', new Slug()); |
| 36 | + |
| 37 | + $this->assertNoViolation(); |
| 38 | + } |
| 39 | + |
| 40 | + public function testExpectsStringCompatibleType() |
| 41 | + { |
| 42 | + $this->expectException(UnexpectedValueException::class); |
| 43 | + $this->validator->validate(new \stdClass(), new Slug()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @testWith ["test-slug"] |
| 48 | + * ["slug-123-test"] |
| 49 | + * ["slug"] |
| 50 | + */ |
| 51 | + public function testValidSlugs($slug) |
| 52 | + { |
| 53 | + $this->validator->validate($slug, new Slug()); |
| 54 | + |
| 55 | + $this->assertNoViolation(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @testWith ["NotASlug"] |
| 60 | + * ["Not a slug"] |
| 61 | + * ["not-á-slug"] |
| 62 | + * ["not-@-slug"] |
| 63 | + */ |
| 64 | + public function testInvalidSlugs($slug) |
| 65 | + { |
| 66 | + $constraint = new Slug([ |
| 67 | + 'message' => 'myMessage', |
| 68 | + ]); |
| 69 | + |
| 70 | + $this->validator->validate($slug, $constraint); |
| 71 | + |
| 72 | + $this->buildViolation('myMessage') |
| 73 | + ->setParameter('{{ value }}', '"'.$slug.'"') |
| 74 | + ->setCode(Slug::NOT_SLUG_ERROR) |
| 75 | + ->assertRaised(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @testWith ["test-slug", true] |
| 80 | + * ["slug-123-test", true] |
| 81 | + */ |
| 82 | + public function testCustomRegexInvalidSlugs($slug) |
| 83 | + { |
| 84 | + $constraint = new Slug(regex: '/^[a-z0-9]+$/i'); |
| 85 | + |
| 86 | + $this->validator->validate($slug, $constraint); |
| 87 | + |
| 88 | + $this->buildViolation($constraint->message) |
| 89 | + ->setParameter('{{ value }}', '"'.$slug.'"') |
| 90 | + ->setCode(Slug::NOT_SLUG_ERROR) |
| 91 | + ->assertRaised(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @testWith ["slug"] |
| 96 | + * @testWith ["test1234"] |
| 97 | + */ |
| 98 | + public function testCustomRegexValidSlugs($slug) |
| 99 | + { |
| 100 | + $constraint = new Slug(regex: '/^[a-z0-9]+$/i'); |
| 101 | + |
| 102 | + $this->validator->validate($slug, $constraint); |
| 103 | + |
| 104 | + $this->assertNoViolation(); |
| 105 | + } |
| 106 | +} |
0 commit comments