From c2ee7d7813cbf06c8bf96118a5e177c4acea33a5 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Tue, 19 Nov 2024 18:21:36 +0100 Subject: [PATCH 1/3] Add support for enums in error message --- src/Assert.php | 7 +++++++ tests/Assert/AssertTest.php | 4 ++++ tests/Utils/TestEnum.php | 14 ++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/Utils/TestEnum.php diff --git a/src/Assert.php b/src/Assert.php index 59de7b1..2ce4587 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -15,6 +15,9 @@ use function array_unshift; use function call_user_func_array; use function end; +use function enum_exists; +use function function_exists; +use function get_class; use function is_object; use function is_resource; use function is_string; @@ -465,6 +468,10 @@ protected static function valueToString(mixed $value): string return $value::class . ': ' . self::valueToString($value->format('c')); } + if (function_exists('enum_exists') && enum_exists(get_class($value))) { + return get_class($value) . '::' . $value->name; + } + return $value::class; } diff --git a/tests/Assert/AssertTest.php b/tests/Assert/AssertTest.php index 0aa21fb..322d894 100644 --- a/tests/Assert/AssertTest.php +++ b/tests/Assert/AssertTest.php @@ -15,6 +15,7 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\Assert\AssertionFailedException; use SimpleSAML\Test\Utils\TestClass; +use SimpleSAML\Test\Utils\TestEnum; use stdClass; use function opendir; @@ -143,6 +144,8 @@ public static function provideValue(): array $resource = opendir(sys_get_temp_dir()); + $enum = TestEnum::PHPUnit; + return [ 'null' => [null, 'null'], 'true' => [true, 'true'], @@ -150,6 +153,7 @@ public static function provideValue(): array 'array' => [[], 'array'], 'Stringable' => [$stringable, 'SimpleSAML\Test\Utils\TestClass: "phpunit"'], 'DateTime' => [$dateTime, 'DateTimeImmutable: "2000-01-01T00:00:00+00:00"'], + 'Enum' => [$enum, 'SimpleSAML\Test\Utils\TestEnum::PHPUnit'], 'object' => [$otherObject, 'stdClass'], 'resource' => [$resource, 'resource'], 'string' => ['string', '"string"'], diff --git a/tests/Utils/TestEnum.php b/tests/Utils/TestEnum.php new file mode 100644 index 0000000..5c059a2 --- /dev/null +++ b/tests/Utils/TestEnum.php @@ -0,0 +1,14 @@ + Date: Thu, 21 Nov 2024 20:21:34 +0100 Subject: [PATCH 2/3] Start testing PHP 8.4 --- .github/workflows/php.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index b41369c..681cfdd 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -51,7 +51,7 @@ jobs: uses: shivammathur/setup-php@v2 with: # Should be the higest supported version, so we can use the newest tools - php-version: '8.3' + php-version: '8.4' tools: composer, phpcs, composer-require-checker, composer-unused extensions: ctype, date, filter, pcre, spl coverage: none @@ -143,7 +143,7 @@ jobs: fail-fast: false matrix: operating-system: [ubuntu-latest] - php-versions: ['8.1', '8.2', '8.3'] + php-versions: ['8.1', '8.2', '8.3', '8.4'] steps: - name: Setup PHP, with composer and extensions @@ -183,15 +183,15 @@ jobs: run: composer install --no-progress --prefer-dist --optimize-autoloader - name: Run unit tests with coverage - if: ${{ matrix.php-versions == '8.3' }} + if: ${{ matrix.php-versions == '8.4' }} run: vendor/bin/phpunit - name: Run unit tests (no coverage) - if: ${{ matrix.php-versions != '8.3' }} + if: ${{ matrix.php-versions != '8.4' }} run: vendor/bin/phpunit --no-coverage - name: Save coverage data - if: ${{ matrix.php-versions == '8.3' }} + if: ${{ matrix.php-versions == '8.4' }} uses: actions/upload-artifact@v4 with: name: coverage-data @@ -205,7 +205,7 @@ jobs: fail-fast: true matrix: operating-system: [windows-latest] - php-versions: ['8.1', '8.2', '8.3'] + php-versions: ['8.1', '8.2', '8.3', '8.4'] steps: - name: Setup PHP, with composer and extensions From 2ab4df783e0cf029065b3d59bec6583fb7c510d4 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 5 Dec 2024 00:28:18 +0100 Subject: [PATCH 3/3] Add assertion to test xs:hexBinary --- src/Assert.php | 3 ++ src/CustomAssertionTrait.php | 24 ++++++++++++++++ tests/Assert/HexBinaryTest.php | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 tests/Assert/HexBinaryTest.php diff --git a/src/Assert.php b/src/Assert.php index 2ce4587..6577e74 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -311,6 +311,7 @@ * @method static void nullOrThrows(Closure|null $expression, string $class, string $message = '', string $exception = '') * @method static void allThrows(Closure[] $expression, string $class, string $message = '', string $exception = '') * + * @method static void validHexBinary(mixed $value, string $message = '', string $exception = '') * @method static void validNMToken(mixed $value, string $message = '', string $exception = '') * @method static void validNMTokens(mixed $value, string $message = '', string $exception = '') * @method static void validDuration(mixed $value, string $message = '', string $exception = '') @@ -322,6 +323,7 @@ * @method static void validURL(mixed $value, string $message = '', string $exception = '') * @method static void validNCName(mixed $value, string $message = '', string $exception = '') * @method static void validQName(mixed $value, string $message = '', string $exception = '') + * @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '') @@ -333,6 +335,7 @@ * @method static void nullOrValidURL(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '') + * @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '') * @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '') * @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '') * @method static void allValidDuration(mixed $value, string $message = '', string $exception = '') diff --git a/src/CustomAssertionTrait.php b/src/CustomAssertionTrait.php index 7884861..0dbed1e 100644 --- a/src/CustomAssertionTrait.php +++ b/src/CustomAssertionTrait.php @@ -22,6 +22,9 @@ */ trait CustomAssertionTrait { + /** @var string */ + private static string $hexbin_regex = '/^([0-9a-fA-F]{2})+$/D'; + /** @var string */ private static string $nmtoken_regex = '/^[\w.:-]+$/Du'; @@ -130,6 +133,27 @@ private static function stringPlausibleBase64(string $value, string $message = ' } + /** + * @param string $value + * @param string $message + */ + private static function validHexBinary(string $value, string $message = ''): void + { + $result = true; + + if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$hexbin_regex]]) === false) { + $result = false; + } + + if ($result === false) { + throw new InvalidArgumentException(sprintf( + $message ?: '\'%s\' is not a valid hexBinary string', + $value, + )); + } + } + + /** * @param string $value * @param string $message diff --git a/tests/Assert/HexBinaryTest.php b/tests/Assert/HexBinaryTest.php new file mode 100644 index 0000000..75cca9f --- /dev/null +++ b/tests/Assert/HexBinaryTest.php @@ -0,0 +1,51 @@ +assertTrue($shouldPass); + } catch (AssertionFailedException $e) { + $this->assertFalse($shouldPass); + } + } + + + /** + * @return array + */ + public static function provideHexBinary(): array + { + return [ + 'empty' => [false, ''], + 'base64' => [false, 'U2ltcGxlU0FNTHBocA=='], + 'valid' => [true, '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f'], + 'invalid' => [false, '3f3r'], + 'bogus' => [false, '&*$(#&^@!(^%$'], + 'length not dividable by 4' => [false, '3f3'], + ]; + } +}