Skip to content

Commit 2ab4df7

Browse files
committed
Add assertion to test xs:hexBinary
1 parent 8f41359 commit 2ab4df7

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/Assert.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
* @method static void nullOrThrows(Closure|null $expression, string $class, string $message = '', string $exception = '')
312312
* @method static void allThrows(Closure[] $expression, string $class, string $message = '', string $exception = '')
313313
*
314+
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
314315
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
315316
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
316317
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
@@ -322,6 +323,7 @@
322323
* @method static void validURL(mixed $value, string $message = '', string $exception = '')
323324
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
324325
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
326+
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
325327
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
326328
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
327329
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
@@ -333,6 +335,7 @@
333335
* @method static void nullOrValidURL(mixed $value, string $message = '', string $exception = '')
334336
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
335337
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
338+
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
336339
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
337340
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
338341
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')

src/CustomAssertionTrait.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
trait CustomAssertionTrait
2424
{
25+
/** @var string */
26+
private static string $hexbin_regex = '/^([0-9a-fA-F]{2})+$/D';
27+
2528
/** @var string */
2629
private static string $nmtoken_regex = '/^[\w.:-]+$/Du';
2730

@@ -130,6 +133,27 @@ private static function stringPlausibleBase64(string $value, string $message = '
130133
}
131134

132135

136+
/**
137+
* @param string $value
138+
* @param string $message
139+
*/
140+
private static function validHexBinary(string $value, string $message = ''): void
141+
{
142+
$result = true;
143+
144+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$hexbin_regex]]) === false) {
145+
$result = false;
146+
}
147+
148+
if ($result === false) {
149+
throw new InvalidArgumentException(sprintf(
150+
$message ?: '\'%s\' is not a valid hexBinary string',
151+
$value,
152+
));
153+
}
154+
}
155+
156+
133157
/**
134158
* @param string $value
135159
* @param string $message

tests/Assert/HexBinaryTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\Assert;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Assert\Assert;
11+
use SimpleSAML\Assert\AssertionFailedException;
12+
13+
/**
14+
* Class \SimpleSAML\Assert\HexBinaryTest
15+
*
16+
* @package simplesamlphp/assert
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class HexBinaryTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $name
24+
*/
25+
#[DataProvider('provideHexBinary')]
26+
public function testHexBinary(bool $shouldPass, string $name): void
27+
{
28+
try {
29+
Assert::validHexBinary($name);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideHexBinary(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'base64' => [false, 'U2ltcGxlU0FNTHBocA=='],
45+
'valid' => [true, '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f'],
46+
'invalid' => [false, '3f3r'],
47+
'bogus' => [false, '&*$(#&^@!(^%$'],
48+
'length not dividable by 4' => [false, '3f3'],
49+
];
50+
}
51+
}

0 commit comments

Comments
 (0)