|
| 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\Polyfill\Tests\Php86; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +class Php86Test extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @dataProvider provideValidClampInput |
| 20 | + */ |
| 21 | + public function testClampSuccess(array $arguments, $result): void |
| 22 | + { |
| 23 | + [$value, $min, $max] = $arguments; |
| 24 | + |
| 25 | + $actual = clamp($value, $min, $max); |
| 26 | + |
| 27 | + if ($value instanceof \DateTimeImmutable) { |
| 28 | + $this->assertInstanceOf(\DateTimeImmutable::class, $actual); |
| 29 | + |
| 30 | + $actual = $actual->format('Y-m-d'); |
| 31 | + } |
| 32 | + |
| 33 | + $this->assertSame($result, $actual); |
| 34 | + } |
| 35 | + |
| 36 | + public function testClampNanReturn(): void |
| 37 | + { |
| 38 | + $this->assertTrue(is_nan(clamp(NAN, 4, 6))); |
| 39 | + } |
| 40 | + |
| 41 | + public static function provideValidClampInput(): array |
| 42 | + { |
| 43 | + $a = new \InvalidArgumentException('a'); |
| 44 | + $b = new \RuntimeException('b'); |
| 45 | + $c = new \LogicException('c'); |
| 46 | + |
| 47 | + return [ |
| 48 | + [ |
| 49 | + [2, 1, 3], |
| 50 | + 2, |
| 51 | + ], |
| 52 | + [ |
| 53 | + [0, 1, 3], |
| 54 | + 1, |
| 55 | + ], |
| 56 | + [ |
| 57 | + [6, 1, 3], |
| 58 | + 3, |
| 59 | + ], |
| 60 | + [ |
| 61 | + [2, 1.3, 3.4], |
| 62 | + 2, |
| 63 | + ], |
| 64 | + [ |
| 65 | + [2.5, 1, 3], |
| 66 | + 2.5, |
| 67 | + ], |
| 68 | + [ |
| 69 | + [2.5, 1.3, 3.4], |
| 70 | + 2.5, |
| 71 | + ], |
| 72 | + [ |
| 73 | + [0, 1.3, 3.4], |
| 74 | + 1.3, |
| 75 | + ], |
| 76 | + [ |
| 77 | + [M_PI, -INF, INF], |
| 78 | + M_PI, |
| 79 | + ], |
| 80 | + [ |
| 81 | + ['a', 'c', 'g'], |
| 82 | + 'c', |
| 83 | + ], |
| 84 | + [ |
| 85 | + ['d', 'c', 'g'], |
| 86 | + 'd', |
| 87 | + ], |
| 88 | + [ |
| 89 | + ['2025-08-01', '2025-08-15', '2025-09-15'], |
| 90 | + '2025-08-15', |
| 91 | + ], |
| 92 | + [ |
| 93 | + ['2025-08-20', '2025-08-15', '2025-09-15'], |
| 94 | + '2025-08-20', |
| 95 | + ], |
| 96 | + [ |
| 97 | + [new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15')], |
| 98 | + '2025-08-15', |
| 99 | + ], |
| 100 | + [ |
| 101 | + [new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15')], |
| 102 | + '2025-08-20', |
| 103 | + ], |
| 104 | + [ |
| 105 | + [null, -1, 1], |
| 106 | + -1, |
| 107 | + ], |
| 108 | + [ |
| 109 | + [null, 1, 3], |
| 110 | + 1, |
| 111 | + ], |
| 112 | + [ |
| 113 | + [null, -3, -1], |
| 114 | + -3, |
| 115 | + ], |
| 116 | + [ |
| 117 | + [-9999, null, 10], |
| 118 | + -9999, |
| 119 | + ], |
| 120 | + [ |
| 121 | + [12, null, 10], |
| 122 | + 10, |
| 123 | + ], |
| 124 | + [ |
| 125 | + [$a, $b, $c], |
| 126 | + $a, |
| 127 | + ], |
| 128 | + [ |
| 129 | + [$b, $a, $c], |
| 130 | + $b, |
| 131 | + ], |
| 132 | + [ |
| 133 | + [$c, $a, $b], |
| 134 | + $c, |
| 135 | + ], |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @dataProvider provideInvalidClampInput |
| 141 | + */ |
| 142 | + public function testClampFailure(array $arguments, string $error): void |
| 143 | + { |
| 144 | + $this->expectException(\ValueError::class); |
| 145 | + $this->expectExceptionMessage($error); |
| 146 | + |
| 147 | + [$value, $min, $max] = $arguments; |
| 148 | + clamp($value, $min, $max); |
| 149 | + } |
| 150 | + |
| 151 | + public static function provideInvalidClampInput(): array |
| 152 | + { |
| 153 | + return [ |
| 154 | + [ |
| 155 | + [4, NAN, 6], |
| 156 | + 'clamp(): Argument #2 ($min) must not be NAN', |
| 157 | + ], |
| 158 | + [ |
| 159 | + [7, 6, NAN], |
| 160 | + 'clamp(): Argument #3 ($max) must not be NAN', |
| 161 | + ], |
| 162 | + [ |
| 163 | + [1, 3, 2], |
| 164 | + 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', |
| 165 | + ], |
| 166 | + [ |
| 167 | + [-9999, 5, null], |
| 168 | + 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', |
| 169 | + ], |
| 170 | + [ |
| 171 | + [12, -5, null], |
| 172 | + 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', |
| 173 | + ], |
| 174 | + ]; |
| 175 | + } |
| 176 | +} |
0 commit comments