Skip to content

Commit 769a9ff

Browse files
committed
Create assertion and type-class for xs:unsignedLong
1 parent 0c10520 commit 769a9ff

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-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 validUnsignedLong(mixed $value, string $message = '', string $exception = '')
4849
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
4950
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
5051
* @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '')
@@ -82,6 +83,7 @@
8283
* @method static void nullOrValidString(mixed $value, string $message = '', string $exception = '')
8384
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
8485
* @method static void nullOrValidToken(mixed $value, string $message = '', string $exception = '')
86+
* @method static void nullOrValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
8587
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
8688
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
8789
* @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '')
@@ -119,6 +121,7 @@
119121
* @method static void allValidString(mixed $value, string $message = '', string $exception = '')
120122
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
121123
* @method static void allValidToken(mixed $value, string $message = '', string $exception = '')
124+
* @method static void allValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
122125
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
123126
*/
124127
class Assert extends BaseAssert
@@ -159,5 +162,6 @@ class Assert extends BaseAssert
159162
use StringTrait;
160163
use TimeTrait;
161164
use TokenTrait;
165+
use UnsignedLongTrait;
162166
use YearMonthTrait;
163167
}

src/Assert/UnsignedLongTrait.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 UnsignedLongTrait
13+
{
14+
/** @var string */
15+
private static string $unsignedLong_regex = '/^([+]?[0]*)(?:[0-9]|[1-9][0-9]{1,19}|1000000000000000|10000000000000000|100000000000000000|1000000000000000000|1[0-8]000000000000000000|18[0-4]00000000000000000|184[0-4]0000000000000000|1844[0-6]000000000000000|18446[0-7]00000000000000|184467[0-4]0000000000000|1844674[0-4]000000000000|184467440[0-7]0000000000|1844674407[0-3]000000000|18446744073[0-7]00000000|1844674407370000000[0-9]|18446744073709[0-5]00000|184467440737095[0-5]0000|1844674407370955[0-2]000)$/D';
16+
17+
/**
18+
* @param string $value
19+
* @param string $message
20+
*/
21+
protected static function validUnsignedLong(string $value, string $message = ''): void
22+
{
23+
parent::regex(
24+
$value,
25+
self::$unsignedLong_regex,
26+
$message ?: '%s is not a valid xs:unsignedLong',
27+
InvalidArgumentException::class,
28+
);
29+
}
30+
}

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

tests/Assert/UnsignedLongTest.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\UnsignedLongTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class UnsignedLongTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $unsignedLong
24+
*/
25+
#[DataProvider('provideUnsignedLong')]
26+
public function testValidUnsignedLong(bool $shouldPass, string $unsignedLong): void
27+
{
28+
try {
29+
Assert::validUnsignedLong($unsignedLong);
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 provideUnsignedLong(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive integer' => [true, '18446744073709551615'],
45+
'invalid positive out-of-bounds' => [true, '18446744073709551616'],
46+
'valid signed positive integer' => [true, '+18446744073709551615'],
47+
'valid zero' => [true, '0'],
48+
'valid negative leading zeros' => [true, '0000000000000000000005'],
49+
'invalid with fractional' => [false, '1.'],
50+
'invalid with space' => [false, '12 34'],
51+
'invalid negative' => [false, '-1'],
52+
'invalid with thousands-delimiter' => [false, '1,234'],
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\UnsignedLongValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\UnsignedLongValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(UnsignedLongValue::class)]
19+
final class UnsignedLongValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $unsignedLong
24+
*/
25+
#[DataProvider('provideUnsignedLong')]
26+
public function testUnsignedLong(bool $shouldPass, string $unsignedLong): void
27+
{
28+
try {
29+
UnsignedLongValue::fromString($unsignedLong);
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 provideUnsignedLong(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive integer' => [true, '18446744073709551615'],
45+
'invalid positive out-of-bounds' => [true, '18446744073709551616'],
46+
'valid signed positive integer' => [true, '+18446744073709551615'],
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 234 \n"],
51+
'invalid negative' => [false, '-1'],
52+
'invalid with thousands-delimiter' => [false, '1,234'],
53+
];
54+
}
55+
}

0 commit comments

Comments
 (0)