Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -308,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 = '')
Expand All @@ -319,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 = '')
Expand All @@ -330,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 = '')
Expand Down Expand Up @@ -465,6 +471,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;
}

Expand Down
24 changes: 24 additions & 0 deletions src/CustomAssertionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/Assert/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,13 +144,16 @@ public static function provideValue(): array

$resource = opendir(sys_get_temp_dir());

$enum = TestEnum::PHPUnit;

return [
'null' => [null, 'null'],
'true' => [true, 'true'],
'false' => [false, 'false'],
'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"'],
Expand Down
51 changes: 51 additions & 0 deletions tests/Assert/HexBinaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Assert;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Assert\AssertionFailedException;

/**
* Class \SimpleSAML\Assert\HexBinaryTest
*
* @package simplesamlphp/assert
*/
#[CoversClass(Assert::class)]
final class HexBinaryTest extends TestCase
{
/**
* @param boolean $shouldPass
* @param string $name
*/
#[DataProvider('provideHexBinary')]
public function testHexBinary(bool $shouldPass, string $name): void
{
try {
Assert::validHexBinary($name);
$this->assertTrue($shouldPass);
} catch (AssertionFailedException $e) {
$this->assertFalse($shouldPass);
}
}


/**
* @return array<string, array{0: bool, 1: string}>
*/
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'],
];
}
}
14 changes: 14 additions & 0 deletions tests/Utils/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Utils;

// Declare a simple enumeration
enum TestEnum
{
case PHPCS;
case PHPStan;
case PHPUnit;
case Psalm;
}
Loading