|
9 | 9 | use CuyZ\Valinor\Tests\Fixture\Enum\BackedStringEnum; |
10 | 10 | use CuyZ\Valinor\Tests\Fixture\Object\StringableObject; |
11 | 11 | use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; |
| 12 | +use PHPUnit\Framework\Attributes\DataProvider; |
12 | 13 | use PHPUnit\Framework\Attributes\TestWith; |
13 | 14 |
|
14 | 15 | final class ScalarValueCastingMappingTest extends IntegrationTestCase |
@@ -50,4 +51,53 @@ public function test_scalar_values_are_casted_properly(string $type, mixed $valu |
50 | 51 | $this->mappingFail($error); |
51 | 52 | } |
52 | 53 | } |
| 54 | + |
| 55 | + #[DataProvider('integer_values_are_casted_properly_data_provider')] |
| 56 | + public function test_integer_values_are_casted_properly(string $type, mixed $value, mixed $expected): void |
| 57 | + { |
| 58 | + try { |
| 59 | + $result = $this |
| 60 | + ->mapperBuilder() |
| 61 | + ->allowScalarValueCasting() |
| 62 | + ->mapper() |
| 63 | + ->map($type, $value); |
| 64 | + |
| 65 | + self::assertSame($expected, $result); |
| 66 | + } catch (MappingError $error) { |
| 67 | + $this->mappingFail($error); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static function integer_values_are_casted_properly_data_provider(): iterable |
| 72 | + { |
| 73 | + yield 'int with very large integer from string' => [ |
| 74 | + 'type' => 'int', |
| 75 | + 'value' => (string)(PHP_INT_MAX - 1), |
| 76 | + 'expected' => PHP_INT_MAX - 1, |
| 77 | + ]; |
| 78 | + |
| 79 | + yield 'non negative int with very large integer from string' => [ |
| 80 | + 'type' => 'non-negative-int', |
| 81 | + 'value' => (string)(PHP_INT_MAX - 1), |
| 82 | + 'expected' => PHP_INT_MAX - 1, |
| 83 | + ]; |
| 84 | + |
| 85 | + yield 'positive int with very large integer from string' => [ |
| 86 | + 'type' => 'positive-int', |
| 87 | + 'value' => (string)(PHP_INT_MAX - 1), |
| 88 | + 'expected' => PHP_INT_MAX - 1, |
| 89 | + ]; |
| 90 | + |
| 91 | + yield 'integer range with very large integer from string' => [ |
| 92 | + 'type' => 'int<0, max>', |
| 93 | + 'value' => (string)(PHP_INT_MAX - 1), |
| 94 | + 'expected' => PHP_INT_MAX - 1, |
| 95 | + ]; |
| 96 | + |
| 97 | + yield 'integer value with very large integer from string' => [ |
| 98 | + 'type' => (string)(PHP_INT_MAX - 1), |
| 99 | + 'value' => (string)(PHP_INT_MAX - 1), |
| 100 | + 'expected' => PHP_INT_MAX - 1, |
| 101 | + ]; |
| 102 | + } |
53 | 103 | } |
0 commit comments