|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 | + public function testClamp(): void |
| 19 | + { |
| 20 | + $this->assertSame(2, clamp(2, 1, 3)); |
| 21 | + $this->assertSame(1, clamp(0, 1, 3)); |
| 22 | + $this->assertSame(3, clamp(6, 1, 3)); |
| 23 | + $this->assertSame(2, clamp(2, 1.3, 3.4)); |
| 24 | + $this->assertSame(2.5, clamp(2.5, 1, 3)); |
| 25 | + $this->assertSame(2.5, clamp(2.5, 1.3, 3.4)); |
| 26 | + $this->assertSame(1.3, clamp(0, 1.3, 3.4)); |
| 27 | + $this->assertSame(M_PI, clamp(M_PI, -INF, INF)); |
| 28 | + $this->assertTrue(is_nan(clamp(NAN, 4, 6))); |
| 29 | + $this->assertSame('c', clamp('a', 'c', 'g')); |
| 30 | + $this->assertSame('d', clamp('d', 'c', 'g')); |
| 31 | + $this->assertSame('2025-08-15', clamp('2025-08-01', '2025-08-15', '2025-09-15')); |
| 32 | + $this->assertSame('2025-08-20', clamp('2025-08-20', '2025-08-15', '2025-09-15')); |
| 33 | + $this->assertSame('2025-08-15', clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d')); |
| 34 | + $this->assertSame('2025-08-20', clamp(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d')); |
| 35 | + $this->assertSame(-1, clamp(null, -1, 1)); |
| 36 | + $this->assertSame(1, clamp(null, 1, 3)); |
| 37 | + $this->assertSame(-3, clamp(null, -3, -1)); |
| 38 | + $this->assertSame(-9999, clamp(-9999, null, 10)); |
| 39 | + $this->assertSame(10, clamp(12, null, 10)); |
| 40 | + $a = new \InvalidArgumentException('a'); |
| 41 | + $b = new \RuntimeException('b'); |
| 42 | + $c = new \LogicException('c'); |
| 43 | + $this->assertSame($a, clamp($a, $b, $c)); |
| 44 | + $this->assertSame($b, clamp($b, $a, $c)); |
| 45 | + $this->assertSame($c, clamp($c, $a, $b)); |
| 46 | + |
| 47 | + $errorMessage = null; |
| 48 | + |
| 49 | + try { |
| 50 | + clamp(4, NAN, 6); |
| 51 | + } catch (\ValueError $error) { |
| 52 | + $errorMessage = $error->getMessage(); |
| 53 | + } |
| 54 | + |
| 55 | + $this->assertSame('clamp(): Argument #2 ($min) cannot be NAN', $errorMessage); |
| 56 | + |
| 57 | + $errorMessage = null; |
| 58 | + |
| 59 | + try { |
| 60 | + clamp(7, 6, NAN); |
| 61 | + } catch (\ValueError $error) { |
| 62 | + $errorMessage = $error->getMessage(); |
| 63 | + } |
| 64 | + |
| 65 | + $this->assertSame('clamp(): Argument #3 ($max) cannot be NAN', $errorMessage); |
| 66 | + |
| 67 | + $errorMessage = null; |
| 68 | + |
| 69 | + try { |
| 70 | + clamp(1, 3, 2); |
| 71 | + } catch (\ValueError $error) { |
| 72 | + $errorMessage = $error->getMessage(); |
| 73 | + } |
| 74 | + |
| 75 | + $this->assertSame('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', $errorMessage); |
| 76 | + |
| 77 | + $errorMessage = null; |
| 78 | + |
| 79 | + try { |
| 80 | + clamp(-9999, 5, null); |
| 81 | + } catch (\ValueError $error) { |
| 82 | + $errorMessage = $error->getMessage(); |
| 83 | + } |
| 84 | + |
| 85 | + $this->assertSame('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', $errorMessage); |
| 86 | + |
| 87 | + $errorMessage = null; |
| 88 | + |
| 89 | + try { |
| 90 | + clamp(12, -5, null); |
| 91 | + } catch (\ValueError $error) { |
| 92 | + $errorMessage = $error->getMessage(); |
| 93 | + } |
| 94 | + |
| 95 | + $this->assertSame('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', $errorMessage); |
| 96 | + } |
| 97 | +} |
0 commit comments