File: DivisibleByValidatorTest.php

package info (click to toggle)
symfony 7.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,424 kB
  • sloc: php: 1,510,651; xml: 7,039; javascript: 979; sh: 586; makefile: 242; pascal: 70
file content (111 lines) | stat: -rw-r--r-- 3,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\DivisibleBy;
use Symfony\Component\Validator\Constraints\DivisibleByValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
 * @author Colin O'Dell <colinodell@gmail.com>
 */
class DivisibleByValidatorTest extends AbstractComparisonValidatorTestCase
{
    use InvalidComparisonToValueTestTrait;
    use ThrowsOnInvalidStringDatesTestTrait;
    use ValidComparisonToValueTrait;

    protected function createValidator(): DivisibleByValidator
    {
        return new DivisibleByValidator();
    }

    protected static function createConstraint(?array $options = null): Constraint
    {
        if (null !== $options) {
            return new DivisibleBy(...$options);
        }

        return new DivisibleBy();
    }

    protected function getErrorCode(): ?string
    {
        return DivisibleBy::NOT_DIVISIBLE_BY;
    }

    public static function provideValidComparisons(): array
    {
        return [
            [-7, 1],
            [0, 3.1415],
            [42, 42],
            [42, 21],
            [10.12, 0.01],
            [10.12, 0.001],
            [1.133, 0.001],
            [1.1331, 0.0001],
            [1.13331, 0.00001],
            [1.13331, 0.000001],
            [1, 0.1],
            [1, 0.01],
            [1, 0.001],
            [1, 0.0001],
            [1, 0.00001],
            [1, 0.000001],
            [3.25, 0.25],
            ['100', '10'],
            [4.1, 0.1],
            [-4.1, 0.1],
        ];
    }

    public static function provideValidComparisonsToPropertyPath(): array
    {
        return [
            [25],
        ];
    }

    public static function provideInvalidComparisons(): array
    {
        return [
            [1, '1', 2, '2', 'int'],
            [10, '10', 3, '3', 'int'],
            [10, '10', 0, '0', 'int'],
            [42, '42', \INF, 'INF', 'float'],
            [4.15, '4.15', 0.1, '0.1', 'float'],
            [10.123, '10.123', 0.01, '0.01', 'float'],
            ['22', '"22"', '10', '"10"', 'string'],
        ];
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('throwsOnNonNumericValuesProvider')]
    public function testThrowsOnNonNumericValues(string $expectedGivenType, $value, $comparedValue)
    {
        $this->expectException(UnexpectedValueException::class);
        $this->expectExceptionMessage(\sprintf('Expected argument of type "numeric", "%s" given', $expectedGivenType));

        $this->validator->validate($value, $this->createConstraint([
            'value' => $comparedValue,
        ]));
    }

    public static function throwsOnNonNumericValuesProvider()
    {
        return [
            [\stdClass::class, 2, new \stdClass()],
            [\ArrayIterator::class, new \ArrayIterator(), 12],
        ];
    }
}