Skip to content

Commit 0d6585d

Browse files
committed
Create assertion and type-class for xs:nonNegativeInteger
1 parent 7315250 commit 0d6585d

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
3232
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
3333
* @method static void validNegativeInteger(mixed $value, string $message = '', string $exception = '')
34+
* @method static void validNonNegativeInteger(mixed $value, string $message = '', string $exception = '')
3435
* @method static void validNonPositiveInteger(mixed $value, string $message = '', string $exception = '')
3536
* @method static void validNormalizedString(mixed $value, string $message = '', string $exception = '')
3637
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
@@ -60,6 +61,7 @@
6061
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
6162
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
6263
* @method static void nullOrValidNegativeInteger(mixed $value, string $message = '', string $exception = '')
64+
* @method static void nullOrValidNonNegativeInteger(mixed $value, string $message = '', string $exception = '')
6365
* @method static void nullOrValidNonPositiveInteger(mixed $value, string $message = '', string $exception = '')
6466
* @method static void nullOrValidNormalizedString(mixed $value, string $message = '', string $exception = '')
6567
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
@@ -89,6 +91,7 @@
8991
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
9092
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
9193
* @method static void allValidNegativeInteger(mixed $value, string $message = '', string $exception = '')
94+
* @method static void allValidNonNegativeInteger(mixed $value, string $message = '', string $exception = '')
9295
* @method static void allValidNonPositiveInteger(mixed $value, string $message = '', string $exception = '')
9396
* @method static void allValidNormalizedString(mixed $value, string $message = '', string $exception = '')
9497
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
@@ -121,6 +124,7 @@ class Assert extends BaseAssert
121124
use NMTokenTrait;
122125
use NMTokensTrait;
123126
use NegativeIntegerTrait;
127+
use NonNegativeIntegerTrait;
124128
use NonPositiveIntegerTrait;
125129
use NormalizedStringTrait;
126130
use QNameTrait;
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 NonNegativeIntegerTrait
13+
{
14+
/** @var string */
15+
private static string $nonNegativeInteger_regex = '/^([+]?\d+)$/D';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validNonNegativeInteger(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$nonNegativeInteger_regex,
27+
$message ?: '%s is not a valid xs:nonNegativeInteger',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}
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 NonNegativeIntegerValue 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::validNonNegativeInteger($this->sanitizeValue($value), SchemaViolationException::class);
25+
}
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\NonNegativeIntegerTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class NonNegativeIntegerTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $nonNegativeInteger
24+
*/
25+
#[DataProvider('provideNonNegativeInteger')]
26+
public function testValidNonNegativeInteger(bool $shouldPass, string $nonNegativeInteger): void
27+
{
28+
try {
29+
Assert::validNonNegativeInteger($nonNegativeInteger);
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 provideNonNegativeInteger(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive integer' => [true, '123456'],
45+
'valid signed positive integer' => [true, '+123456'],
46+
'valid zero' => [true, '0'],
47+
'valid negative leading zeros' => [true, '0000000000000000000005'],
48+
'invalid with fractional' => [false, '1.'],
49+
'invalid negative' => [false, '-1234'],
50+
'invalid with thousands-delimiter' => [false, '1,234'],
51+
];
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\NonNegativeIntegerValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\NonNegativeIntegerValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(NonNegativeIntegerValue::class)]
19+
final class NonNegativeIntegerValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $nonNegativeInteger
24+
*/
25+
#[DataProvider('provideNonNegativeInteger')]
26+
public function testNonNegativeInteger(bool $shouldPass, string $nonNegativeInteger): void
27+
{
28+
try {
29+
NonNegativeIntegerValue::fromString($nonNegativeInteger);
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 provideNonNegativeInteger(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive integer' => [true, '123456'],
45+
'valid signed positive integer' => [true, '+123456'],
46+
'valid zero' => [true, '0'],
47+
'valid negative leading zeros' => [true, '0000000000000000000005'],
48+
'valid with whitespace collapse' => [true, " 1 234 \n"],
49+
'invalid with fractional' => [false, '1.'],
50+
'invalid negative' => [false, '-1234'],
51+
'invalid with thousands-delimiter' => [false, '1,234'],
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)