Skip to content

Commit 3433fdb

Browse files
committed
Create assertion and type-class for xs:unsignedByte
1 parent 480a925 commit 3433fdb

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* @method static void validString(mixed $value, string $message = '', string $exception = '')
4646
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
4747
* @method static void validToken(mixed $value, string $message = '', string $exception = '')
48+
* @method static void validUnsignedByte(mixed $value, string $message = '', string $exception = '')
4849
* @method static void validUnsignedInt(mixed $value, string $message = '', string $exception = '')
4950
* @method static void validUnsignedLong(mixed $value, string $message = '', string $exception = '')
5051
* @method static void validUnsignedShort(mixed $value, string $message = '', string $exception = '')
@@ -85,6 +86,7 @@
8586
* @method static void nullOrValidString(mixed $value, string $message = '', string $exception = '')
8687
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
8788
* @method static void nullOrValidToken(mixed $value, string $message = '', string $exception = '')
89+
* @method static void nullOrValidUnsignedByte(mixed $value, string $message = '', string $exception = '')
8890
* @method static void nullOrValidUnsignedInt(mixed $value, string $message = '', string $exception = '')
8991
* @method static void nullOrValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
9092
* @method static void nullOrValidUnsignedShort(mixed $value, string $message = '', string $exception = '')
@@ -125,6 +127,7 @@
125127
* @method static void allValidString(mixed $value, string $message = '', string $exception = '')
126128
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
127129
* @method static void allValidToken(mixed $value, string $message = '', string $exception = '')
130+
* @method static void allValidUnsignedByte(mixed $value, string $message = '', string $exception = '')
128131
* @method static void allValidUnsignedInt(mixed $value, string $message = '', string $exception = '')
129132
* @method static void allValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
130133
* @method static void allValidUnsignedShort(mixed $value, string $message = '', string $exception = '')
@@ -168,6 +171,7 @@ class Assert extends BaseAssert
168171
use StringTrait;
169172
use TimeTrait;
170173
use TokenTrait;
174+
use UnsignedByteTrait;
171175
use UnsignedIntTrait;
172176
use UnsignedLongTrait;
173177
use UnsignedShortTrait;

src/Assert/UnsignedByteTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait UnsignedByteTrait
13+
{
14+
/** @var string */
15+
private static string $unsignedByte_regex = '/^(([+]?0*)(?:[1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])|0)$/D';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validUnsignedByte(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$unsignedByte_regex,
27+
$message ?: '%s is not a valid xs:unsignedByte',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/UnsignedByteValue.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
10+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class UnsignedByteValue extends IntegerValue
14+
{
15+
/**
16+
* Validate the value.
17+
*
18+
* @param string $value
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateValue(string $value): void
23+
{
24+
Assert::validUnsignedByte($this->sanitizeValue($value), SchemaViolationException::class);
25+
}
26+
}

tests/Assert/UnsignedByteTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Assert;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Assert\AssertionFailedException;
11+
use SimpleSAML\XML\Assert\Assert;
12+
13+
/**
14+
* Class \SimpleSAML\Test\XML\Assert\UnsignedByteTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class UnsignedByteTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $unsignedByte
24+
*/
25+
#[DataProvider('provideUnsignedByte')]
26+
public function testValidUnsignedByte(bool $shouldPass, string $unsignedByte): void
27+
{
28+
try {
29+
Assert::validUnsignedByte($unsignedByte);
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 provideUnsignedByte(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive Byte' => [true, '255'],
45+
'invalid positive out-of-bounds' => [false, '256'],
46+
'valid signed positive Byte' => [true, '+255'],
47+
'valid zero' => [true, '0'],
48+
'valid negative leading zeros' => [true, '0000000000000000000005'],
49+
'invalid with fractional' => [false, '1.'],
50+
'invalid with space' => [false, '12 3'],
51+
'invalid negative' => [false, '-1'],
52+
'invalid with thousands-delimiter' => [false, '1,23'],
53+
];
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Type\UnsignedByteValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\UnsignedByteValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(UnsignedByteValue::class)]
19+
final class UnsignedByteValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $unsignedByte
24+
*/
25+
#[DataProvider('provideUnsignedByte')]
26+
public function testUnsignedByte(bool $shouldPass, string $unsignedByte): void
27+
{
28+
try {
29+
UnsignedByteValue::fromString($unsignedByte);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideUnsignedByte(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive Byte' => [true, '255'],
45+
'invalid positive out-of-bounds' => [false, '256'],
46+
'valid signed positive Byte' => [true, '+255'],
47+
'valid zero' => [true, '0'],
48+
'valid negative leading zeros' => [true, '0000000000000000000005'],
49+
'invalid with fractional' => [false, '1.'],
50+
'valid with whitespace collapse' => [true, " 1 24 \n"],
51+
'invalid negative' => [false, '-1'],
52+
'invalid with thousands-delimiter' => [false, '1,23'],
53+
];
54+
}
55+
}

0 commit comments

Comments
 (0)