|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Db\Pgsql\Tests; |
| 6 | + |
| 7 | +use DateTimeImmutable; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use RuntimeException; |
| 11 | +use Yiisoft\Db\Pgsql\Expression\DateRangeValue; |
| 12 | +use Yiisoft\Db\Pgsql\Expression\Int4RangeValue; |
| 13 | +use Yiisoft\Db\Pgsql\Expression\Int8RangeValue; |
| 14 | +use Yiisoft\Db\Pgsql\Expression\NumRangeValue; |
| 15 | +use Yiisoft\Db\Pgsql\Expression\TsRangeValue; |
| 16 | +use Yiisoft\Db\Pgsql\Expression\TsTzRangeValue; |
| 17 | + |
| 18 | +use const PHP_INT_MAX; |
| 19 | +use const PHP_INT_MIN; |
| 20 | + |
| 21 | +final class RangeValueTest extends TestCase |
| 22 | +{ |
| 23 | + public static function boundsProvider(): iterable |
| 24 | + { |
| 25 | + yield 'int4 inclusive' => [ |
| 26 | + new Int4RangeValue(1, 5), |
| 27 | + [1, 5], |
| 28 | + ]; |
| 29 | + yield 'int4 exclusive' => [ |
| 30 | + new Int4RangeValue(1, 5, false, false), |
| 31 | + [2, 4], |
| 32 | + ]; |
| 33 | + yield 'int4 unbounded' => [ |
| 34 | + new Int4RangeValue(null, null, false, false), |
| 35 | + [null, null], |
| 36 | + ]; |
| 37 | + yield 'int8 inclusive string bounds' => [ |
| 38 | + new Int8RangeValue(PHP_INT_MIN, PHP_INT_MAX), |
| 39 | + [PHP_INT_MIN, PHP_INT_MAX], |
| 40 | + ]; |
| 41 | + yield 'int8 exclusive' => [ |
| 42 | + new Int8RangeValue('1', '5', false, false), |
| 43 | + [2, 4], |
| 44 | + ]; |
| 45 | + yield 'int8 unbounded' => [ |
| 46 | + new Int8RangeValue(null, null, false, false), |
| 47 | + [null, null], |
| 48 | + ]; |
| 49 | + yield 'num inclusive' => [ |
| 50 | + new NumRangeValue(1.5, 5.5), |
| 51 | + [1.5, 5.5], |
| 52 | + ]; |
| 53 | + yield 'num unbounded' => [ |
| 54 | + new NumRangeValue(null, null, false, false), |
| 55 | + [null, null], |
| 56 | + ]; |
| 57 | + yield 'date inclusive' => [ |
| 58 | + new DateRangeValue(new DateTimeImmutable('2024-01-01'), new DateTimeImmutable('2024-01-05')), |
| 59 | + [new DateTimeImmutable('2024-01-01'), new DateTimeImmutable('2024-01-05')], |
| 60 | + ]; |
| 61 | + yield 'date exclusive' => [ |
| 62 | + new DateRangeValue( |
| 63 | + new DateTimeImmutable('2024-01-01'), |
| 64 | + new DateTimeImmutable('2024-01-05'), |
| 65 | + false, |
| 66 | + false, |
| 67 | + ), |
| 68 | + [new DateTimeImmutable('2024-01-02'), new DateTimeImmutable('2024-01-04')], |
| 69 | + ]; |
| 70 | + yield 'timestamp exclusive' => [ |
| 71 | + new TsRangeValue( |
| 72 | + new DateTimeImmutable('2024-01-01 10:00:00'), |
| 73 | + new DateTimeImmutable('2024-01-01 15:00:00'), |
| 74 | + false, |
| 75 | + false, |
| 76 | + ), |
| 77 | + [new DateTimeImmutable('2024-01-01 10:00:01'), new DateTimeImmutable('2024-01-01 14:59:59')], |
| 78 | + ]; |
| 79 | + yield 'timestamp with time zone exclusive' => [ |
| 80 | + new TsTzRangeValue( |
| 81 | + new DateTimeImmutable('2024-01-01 10:00:00+02:00'), |
| 82 | + new DateTimeImmutable('2024-01-01 15:00:00+02:00'), |
| 83 | + false, |
| 84 | + false, |
| 85 | + ), |
| 86 | + [new DateTimeImmutable('2024-01-01 10:00:01+02:00'), new DateTimeImmutable('2024-01-01 14:59:59+02:00')], |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + #[DataProvider('boundsProvider')] |
| 91 | + public function testGetBounds(object $rangeValue, array $expected): void |
| 92 | + { |
| 93 | + $this->assertBoundsSame($expected, $rangeValue->getBounds()); |
| 94 | + } |
| 95 | + |
| 96 | + public static function boundsExceptionProvider(): iterable |
| 97 | + { |
| 98 | + yield 'int8 lower below minimum' => [ |
| 99 | + new Int8RangeValue(PHP_INT_MIN, null, false), |
| 100 | + 'Lower bound cannot be determined from the excluded value of a bigint range.', |
| 101 | + ]; |
| 102 | + yield 'int8 lower at maximum' => [ |
| 103 | + new Int8RangeValue(PHP_INT_MAX, null, false), |
| 104 | + 'Lower bound cannot be determined from the excluded value of a bigint range.', |
| 105 | + ]; |
| 106 | + yield 'int8 upper above maximum' => [ |
| 107 | + new Int8RangeValue(null, PHP_INT_MAX, true, false), |
| 108 | + 'Upper bound cannot be determined from the excluded value of a bigint range.', |
| 109 | + ]; |
| 110 | + yield 'int8 upper at minimum' => [ |
| 111 | + new Int8RangeValue(null, PHP_INT_MIN, true, false), |
| 112 | + 'Upper bound cannot be determined from the excluded value of a bigint range.', |
| 113 | + ]; |
| 114 | + yield 'num lower exclusive' => [ |
| 115 | + new NumRangeValue(1.5, null, false), |
| 116 | + 'Lower bound cannot be determined from the excluded value of a numeric range.', |
| 117 | + ]; |
| 118 | + yield 'num upper exclusive' => [ |
| 119 | + new NumRangeValue(null, 5.5, true, false), |
| 120 | + 'Upper bound cannot be determined from the excluded value of a numeric range.', |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + #[DataProvider('boundsExceptionProvider')] |
| 125 | + public function testGetBoundsException(object $rangeValue, string $expectedMessage): void |
| 126 | + { |
| 127 | + $this->expectException(RuntimeException::class); |
| 128 | + $this->expectExceptionMessage($expectedMessage); |
| 129 | + |
| 130 | + $rangeValue->getBounds(); |
| 131 | + } |
| 132 | + |
| 133 | + private function assertBoundsSame(array $expected, array $actual): void |
| 134 | + { |
| 135 | + $this->assertCount(2, $actual); |
| 136 | + |
| 137 | + foreach ($expected as $key => $expectedValue) { |
| 138 | + if ($expectedValue instanceof DateTimeImmutable) { |
| 139 | + $this->assertEquals($expectedValue, $actual[$key]); |
| 140 | + } else { |
| 141 | + $this->assertSame($expectedValue, $actual[$key]); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments