|
| 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 | +/** @group php86 */ |
| 17 | +class Php86Test extends TestCase |
| 18 | +{ |
| 19 | + public function testClamp(): void |
| 20 | + { |
| 21 | + $this->assertSame(2, clamp(2, 1, 3)); |
| 22 | + $this->assertSame(1, clamp(0, 1, 3)); |
| 23 | + $this->assertSame(3, clamp(6, 1, 3)); |
| 24 | + $this->assertSame(2, clamp(2, 1.3, 3.4)); |
| 25 | + $this->assertSame(2.5, clamp(2.5, 1, 3)); |
| 26 | + $this->assertSame(2.5, clamp(2.5, 1.3, 3.4)); |
| 27 | + $this->assertSame(1.3, clamp(0, 1.3, 3.4)); |
| 28 | + $this->assertSame(M_PI, clamp(M_PI, -INF, INF)); |
| 29 | + $this->assertTrue(is_nan(clamp(NAN, 4, 6))); |
| 30 | + $this->assertSame('c', clamp('a', 'c', 'g')); |
| 31 | + $this->assertSame('d', clamp('d', 'c', 'g')); |
| 32 | + $this->assertSame('2025-08-15', clamp('2025-08-01', '2025-08-15', '2025-09-15')); |
| 33 | + $this->assertSame('2025-08-20', clamp('2025-08-20', '2025-08-15', '2025-09-15')); |
| 34 | + $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')); |
| 35 | + $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')); |
| 36 | + |
| 37 | + $errorMessage = null; |
| 38 | + |
| 39 | + try { |
| 40 | + clamp(4, NAN, 6); |
| 41 | + } catch (\ValueError $error) { |
| 42 | + $errorMessage = $error->getMessage(); |
| 43 | + } |
| 44 | + |
| 45 | + $this->assertSame('clamp(): Argument #2 ($min) cannot be NAN', $errorMessage); |
| 46 | + |
| 47 | + $errorMessage = null; |
| 48 | + |
| 49 | + try { |
| 50 | + clamp(7, 6, NAN); |
| 51 | + } catch (\ValueError $error) { |
| 52 | + $errorMessage = $error->getMessage(); |
| 53 | + } |
| 54 | + |
| 55 | + $this->assertSame('clamp(): Argument #3 ($max) cannot be NAN', $errorMessage); |
| 56 | + |
| 57 | + $errorMessage = null; |
| 58 | + |
| 59 | + try { |
| 60 | + clamp(1, 3, 2); |
| 61 | + } catch (\ValueError $error) { |
| 62 | + $errorMessage = $error->getMessage(); |
| 63 | + } |
| 64 | + |
| 65 | + $this->assertSame('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', $errorMessage); |
| 66 | + } |
| 67 | +} |
0 commit comments