Skip to content

Commit 7315250

Browse files
committed
Create assertion and type-class for xs:negativeInteger
1 parent 06cde6c commit 7315250

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
3131
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
3232
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
33+
* @method static void validNegativeInteger(mixed $value, string $message = '', string $exception = '')
3334
* @method static void validNonPositiveInteger(mixed $value, string $message = '', string $exception = '')
3435
* @method static void validNormalizedString(mixed $value, string $message = '', string $exception = '')
3536
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
@@ -58,6 +59,7 @@
5859
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
5960
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
6061
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
62+
* @method static void nullOrValidNegativeInteger(mixed $value, string $message = '', string $exception = '')
6163
* @method static void nullOrValidNonPositiveInteger(mixed $value, string $message = '', string $exception = '')
6264
* @method static void nullOrValidNormalizedString(mixed $value, string $message = '', string $exception = '')
6365
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
@@ -86,6 +88,7 @@
8688
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
8789
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
8890
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
91+
* @method static void allValidNegativeInteger(mixed $value, string $message = '', string $exception = '')
8992
* @method static void allValidNonPositiveInteger(mixed $value, string $message = '', string $exception = '')
9093
* @method static void allValidNormalizedString(mixed $value, string $message = '', string $exception = '')
9194
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
@@ -117,6 +120,7 @@ class Assert extends BaseAssert
117120
use NCNameTrait;
118121
use NMTokenTrait;
119122
use NMTokensTrait;
123+
use NegativeIntegerTrait;
120124
use NonPositiveIntegerTrait;
121125
use NormalizedStringTrait;
122126
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 NegativeIntegerTrait
13+
{
14+
/** @var string */
15+
private static string $negativeInteger_regex = '/^(-\d+)$/D';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validNegativeInteger(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$negativeInteger_regex,
27+
$message ?: '%s is not a valid xs:negativeInteger',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/NegativeIntegerValue.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 NegativeIntegerValue 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::validNegativeInteger($this->sanitizeValue($value), SchemaViolationException::class);
25+
}
26+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\NegativeIntegerTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class NegativeIntegerTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $negativeInteger
24+
*/
25+
#[DataProvider('provideNegativeInteger')]
26+
public function testValidNegativeInteger(bool $shouldPass, string $negativeInteger): void
27+
{
28+
try {
29+
Assert::validNegativeInteger($negativeInteger);
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 provideNegativeInteger(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid non-positive integer' => [true, '-123456'],
45+
'invalid zero' => [false, '0'],
46+
'valid negative leading zeros' => [true, '-0000000000000000000005'],
47+
'invalid with fractional' => [false, '-1.'],
48+
'invalid positive' => [false, '1234'],
49+
'invalid with thousands-delimiter' => [false, '-1,234'],
50+
];
51+
}
52+
}
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\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\NegativeIntegerValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\NegativeIntegerValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(NegativeIntegerValue::class)]
19+
final class NegativeIntegerValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $negativeInteger
24+
*/
25+
#[DataProvider('provideNegativeInteger')]
26+
public function testNegativeInteger(bool $shouldPass, string $negativeInteger): void
27+
{
28+
try {
29+
NegativeIntegerValue::fromString($negativeInteger);
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 provideNegativeInteger(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid non-positive integer' => [true, '-123456'],
45+
'invalid zero' => [false, '0'],
46+
'valid negative leading zeros' => [true, '-0000000000000000000005'],
47+
'valid with whitespace collapse' => [true, " -1 234 \n"],
48+
'invalid with fractional' => [false, '-1.'],
49+
'invalid positive' => [false, '1234'],
50+
'invalid with thousands-delimiter' => [false, '-1,234'],
51+
];
52+
}
53+
}

0 commit comments

Comments
 (0)