|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\XMLSchema\Type; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\Attributes\DataProviderExternal; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use SimpleSAML\Test\XML\Assert\IntegerTest; |
| 12 | +use SimpleSAML\XMLSchema\Exception\RuntimeException; |
| 13 | +use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
| 14 | +use SimpleSAML\XMLSchema\Type\IntegerValue; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class \SimpleSAML\Test\XMLSchema\Type\IntegerOverflowTest |
| 18 | + * |
| 19 | + * @package simplesamlphp/xml-common |
| 20 | + */ |
| 21 | +#[CoversClass(IntegerValue::class)] |
| 22 | +final class IntegerOverflowTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @param boolean|class-string<\Throwable> $shouldPass |
| 26 | + * @param string $integer |
| 27 | + * @param string|null $message |
| 28 | + */ |
| 29 | + #[DataProvider('provideInvalidInteger')] |
| 30 | + #[DataProvider('provideValidInteger')] |
| 31 | + #[DataProviderExternal(IntegerTest::class, 'provideValidInteger')] |
| 32 | + public function testInteger(bool|string $shouldPass, string $integer, ?string $message = null): void |
| 33 | + { |
| 34 | + try { |
| 35 | + IntegerValue::fromString($integer)->toInteger(); |
| 36 | + $this->assertTrue($shouldPass); |
| 37 | + } catch (RuntimeException | SchemaViolationException $e) { |
| 38 | + $this->assertSame($shouldPass, $e::class); |
| 39 | + if ($message !== null) { |
| 40 | + $this->assertSame($message, $e->getMessage()); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * @return array<string, array{0: true, 1: string}> |
| 48 | + */ |
| 49 | + public static function provideValidInteger(): array |
| 50 | + { |
| 51 | + return [ |
| 52 | + 'valid with whitespace collapse' => [true, " 1234 \n "], |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * @return array<string, array{0: class-string<\Throwable>, 1: string, 2?: string}> |
| 59 | + */ |
| 60 | + public static function provideInvalidInteger(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + 'empty' => [SchemaViolationException::class, ''], |
| 64 | + 'invalid positive signed out-of-bounds' => [ |
| 65 | + RuntimeException::class, |
| 66 | + '+9223372036854775808', |
| 67 | + 'Cannot convert to integer: out of bounds.', |
| 68 | + ], |
| 69 | + 'invalid negative signed out-of-bounds' => [ |
| 70 | + RuntimeException::class, |
| 71 | + '-9223372036854775809', |
| 72 | + 'Cannot convert to integer: out of bounds.', |
| 73 | + ], |
| 74 | + 'invalid' => [SchemaViolationException::class, '0x123'], |
| 75 | + 'invalid with fractional' => [SchemaViolationException::class, '1234.'], |
| 76 | + 'invalid with thousands-delimiter' => [SchemaViolationException::class, '+1,234'], |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + public function testToIntegerWithNonWellFormedIntegerStringThrowsException(): void |
| 82 | + { |
| 83 | + $this->expectException(SchemaViolationException::class); |
| 84 | + $this->expectExceptionMessageMatches('/^Not a well-formed integer string\.$/'); |
| 85 | + |
| 86 | + /* mock the internal rawValue to test the exception handling within IntegerValue::toInteger() */ |
| 87 | + $value = $this->createPartialMock(IntegerValue::class, ['getRawValue']); |
| 88 | + $value->expects($this->once())->method('getRawValue')->willReturn('0x42'); |
| 89 | + $value->toInteger(); |
| 90 | + } |
| 91 | +} |
0 commit comments