Skip to content

Commit 8f3c1fb

Browse files
committed
Create assertion and type-class for xs:byte
1 parent 4689490 commit 8f3c1fb

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
@@ -12,6 +12,7 @@
1212
* @method static void validAnyURI(mixed $value, string $message = '', string $exception = '')
1313
* @method static void validBase64Binary(mixed $value, string $message = '', string $exception = '')
1414
* @method static void validBoolean(mixed $value, string $message = '', string $exception = '')
15+
* @method static void validByte(mixed $value, string $message = '', string $exception = '')
1516
* @method static void validDate(mixed $value, string $message = '', string $exception = '')
1617
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
1718
* @method static void validDay(mixed $value, string $message = '', string $exception = '')
@@ -46,6 +47,7 @@
4647
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
4748
* @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '')
4849
* @method static void nullOrValidBoolean(mixed $value, string $message = '', string $exception = '')
50+
* @method static void nullOrValidByte(mixed $value, string $message = '', string $exception = '')
4951
* @method static void nullOrValidDate(mixed $value, string $message = '', string $exception = '')
5052
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
5153
* @method static void nullOrValidDay(mixed $value, string $message = '', string $exception = '')
@@ -80,6 +82,7 @@
8082
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
8183
* @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '')
8284
* @method static void allValidBoolean(mixed $value, string $message = '', string $exception = '')
85+
* @method static void allValidByte(mixed $value, string $message = '', string $exception = '')
8386
* @method static void allValidDate(mixed $value, string $message = '', string $exception = '')
8487
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
8588
* @method static void allValidDay(mixed $value, string $message = '', string $exception = '')
@@ -117,6 +120,7 @@ class Assert extends BaseAssert
117120
use AnyURITrait;
118121
use Base64BinaryTrait;
119122
use BooleanTrait;
123+
use ByteTrait;
120124
use DateTrait;
121125
use DateTimeTrait;
122126
use DayTrait;

src/Assert/ByteTrait.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 ByteTrait
13+
{
14+
/** @var string */
15+
private static string $byte_regex = '/^(((-[0]*([0-9]|[1-8][0-9]|9[0-9]|1[01][0-9]|12[0-8]))|([+]?[0]*([0-9]|[1-8][0-9]|9[0-9]|1[01][0-9]|12[0-7]))|0))$/D';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validByte(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$byte_regex,
27+
$message ?: '%s is not a valid xs:byte',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/ByteValue.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 ByteValue extends LongValue
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::validByte($this->sanitizeValue($value), SchemaViolationException::class);
25+
}
26+
}

tests/Assert/ByteTest.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\ByteTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class ByteTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $byte
24+
*/
25+
#[DataProvider('provideByte')]
26+
public function testValidByte(bool $shouldPass, string $byte): void
27+
{
28+
try {
29+
Assert::validByte($byte);
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 provideByte(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+127'],
45+
'valid negative signed' => [true, '-128'],
46+
'valid non-signed' => [true, '123'],
47+
'valid leading zeros' => [true, '-0001'],
48+
'valid zero' => [true, '0'],
49+
'invalid positive signed out-of-bounds' => [false, '+128'],
50+
'invalid negative signed out-of-bounds' => [false, '-129'],
51+
'invalid with space' => [false, '1 23'],
52+
'invalid with fractional' => [false, '123.'],
53+
];
54+
}
55+
}

tests/Type/ByteValueTest.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\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\ByteValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\ByteValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(ByteValue::class)]
19+
final class ByteValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $byte
24+
*/
25+
#[DataProvider('provideByte')]
26+
public function testByte(bool $shouldPass, string $byte): void
27+
{
28+
try {
29+
ByteValue::fromString($byte);
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 provideByte(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+127'],
45+
'valid negative signed' => [true, '-128'],
46+
'valid non-signed' => [true, '123'],
47+
'valid leading zeros' => [true, '-0001'],
48+
'valid zero' => [true, '0'],
49+
'invalid positive signed out-of-bounds' => [false, '+128'],
50+
'invalid negative signed out-of-bounds' => [false, '-129'],
51+
'valid with whitespace collapse' => [true, " 1 23 \n"],
52+
'invalid with fractional' => [false, '123.'],
53+
];
54+
}
55+
}

0 commit comments

Comments
 (0)