From 769a9ffe24e08f619e7cfca2557054ff430cb5f2 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Tue, 14 Jan 2025 00:50:33 +0100 Subject: [PATCH] Create assertion and type-class for xs:unsignedLong --- src/Assert/Assert.php | 4 ++ src/Assert/UnsignedLongTrait.php | 30 +++++++++++++++ src/Type/UnsignedLongValue.php | 26 +++++++++++++ tests/Assert/UnsignedLongTest.php | 55 ++++++++++++++++++++++++++++ tests/Type/UnsignedLongValueTest.php | 55 ++++++++++++++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 src/Assert/UnsignedLongTrait.php create mode 100644 src/Type/UnsignedLongValue.php create mode 100644 tests/Assert/UnsignedLongTest.php create mode 100644 tests/Type/UnsignedLongValueTest.php diff --git a/src/Assert/Assert.php b/src/Assert/Assert.php index b1f8d627..ecc85c8a 100644 --- a/src/Assert/Assert.php +++ b/src/Assert/Assert.php @@ -45,6 +45,7 @@ * @method static void validString(mixed $value, string $message = '', string $exception = '') * @method static void validTime(mixed $value, string $message = '', string $exception = '') * @method static void validToken(mixed $value, string $message = '', string $exception = '') + * @method static void validUnsignedLong(mixed $value, string $message = '', string $exception = '') * @method static void validYearMonth(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '') @@ -82,6 +83,7 @@ * @method static void nullOrValidString(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidToken(mixed $value, string $message = '', string $exception = '') + * @method static void nullOrValidUnsignedLong(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '') * @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '') * @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '') @@ -119,6 +121,7 @@ * @method static void allValidString(mixed $value, string $message = '', string $exception = '') * @method static void allValidTime(mixed $value, string $message = '', string $exception = '') * @method static void allValidToken(mixed $value, string $message = '', string $exception = '') + * @method static void allValidUnsignedLong(mixed $value, string $message = '', string $exception = '') * @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '') */ class Assert extends BaseAssert @@ -159,5 +162,6 @@ class Assert extends BaseAssert use StringTrait; use TimeTrait; use TokenTrait; + use UnsignedLongTrait; use YearMonthTrait; } diff --git a/src/Assert/UnsignedLongTrait.php b/src/Assert/UnsignedLongTrait.php new file mode 100644 index 00000000..b36b9802 --- /dev/null +++ b/src/Assert/UnsignedLongTrait.php @@ -0,0 +1,30 @@ +sanitizeValue($value), SchemaViolationException::class); + } +} diff --git a/tests/Assert/UnsignedLongTest.php b/tests/Assert/UnsignedLongTest.php new file mode 100644 index 00000000..6955e594 --- /dev/null +++ b/tests/Assert/UnsignedLongTest.php @@ -0,0 +1,55 @@ +assertTrue($shouldPass); + } catch (AssertionFailedException $e) { + $this->assertFalse($shouldPass); + } + } + + + /** + * @return array + */ + public static function provideUnsignedLong(): array + { + return [ + 'empty' => [false, ''], + 'valid positive integer' => [true, '18446744073709551615'], + 'invalid positive out-of-bounds' => [true, '18446744073709551616'], + 'valid signed positive integer' => [true, '+18446744073709551615'], + 'valid zero' => [true, '0'], + 'valid negative leading zeros' => [true, '0000000000000000000005'], + 'invalid with fractional' => [false, '1.'], + 'invalid with space' => [false, '12 34'], + 'invalid negative' => [false, '-1'], + 'invalid with thousands-delimiter' => [false, '1,234'], + ]; + } +} diff --git a/tests/Type/UnsignedLongValueTest.php b/tests/Type/UnsignedLongValueTest.php new file mode 100644 index 00000000..d5454a74 --- /dev/null +++ b/tests/Type/UnsignedLongValueTest.php @@ -0,0 +1,55 @@ +assertTrue($shouldPass); + } catch (SchemaViolationException $e) { + $this->assertFalse($shouldPass); + } + } + + + /** + * @return array + */ + public static function provideUnsignedLong(): array + { + return [ + 'empty' => [false, ''], + 'valid positive integer' => [true, '18446744073709551615'], + 'invalid positive out-of-bounds' => [true, '18446744073709551616'], + 'valid signed positive integer' => [true, '+18446744073709551615'], + 'valid zero' => [true, '0'], + 'valid negative leading zeros' => [true, '0000000000000000000005'], + 'invalid with fractional' => [false, '1.'], + 'valid with whitespace collapse' => [true, " 1 234 \n"], + 'invalid negative' => [false, '-1'], + 'invalid with thousands-delimiter' => [false, '1,234'], + ]; + } +}