|
| 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\Finite; |
| 15 | +use Symfony\Component\Validator\Constraints\FiniteValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | + |
| 18 | +class FiniteValidatorTest extends ConstraintValidatorTestCase |
| 19 | +{ |
| 20 | + protected function createValidator(): FiniteValidator |
| 21 | + { |
| 22 | + return new FiniteValidator(); |
| 23 | + } |
| 24 | + |
| 25 | + public function testNullIsValid() |
| 26 | + { |
| 27 | + $constraint = new Finite(); |
| 28 | + |
| 29 | + $this->validator->validate(null, $constraint); |
| 30 | + |
| 31 | + $this->assertNoViolation(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @dataProvider getValidValues |
| 36 | + */ |
| 37 | + public function testValidValues(mixed $value) |
| 38 | + { |
| 39 | + $constraint = new Finite(); |
| 40 | + |
| 41 | + $this->validator->validate($value, $constraint); |
| 42 | + |
| 43 | + $this->assertNoViolation(); |
| 44 | + } |
| 45 | + |
| 46 | + public static function getValidValues(): array |
| 47 | + { |
| 48 | + return [ |
| 49 | + [0], |
| 50 | + ['0'], |
| 51 | + [1.5], |
| 52 | + ['1.5'], |
| 53 | + ['12345'], |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @dataProvider getInvalidValues |
| 59 | + */ |
| 60 | + public function testInvalidValues(mixed $value, string $valueAsString) |
| 61 | + { |
| 62 | + $constraint = new Finite([ |
| 63 | + 'message' => 'myMessage', |
| 64 | + ]); |
| 65 | + |
| 66 | + $this->validator->validate($value, $constraint); |
| 67 | + |
| 68 | + $this->buildViolation('myMessage') |
| 69 | + ->setParameter('{{ value }}', $valueAsString) |
| 70 | + ->setCode(Finite::NOT_FINITE_ERROR) |
| 71 | + ->assertRaised(); |
| 72 | + } |
| 73 | + |
| 74 | + public static function getInvalidValues(): array |
| 75 | + { |
| 76 | + return [ |
| 77 | + [\NAN, 'NAN'], |
| 78 | + [\INF, 'INF'], |
| 79 | + ['', '""'], |
| 80 | + ['foo', '"foo"'], |
| 81 | + ]; |
| 82 | + } |
| 83 | +} |
0 commit comments