Skip to content

Commit 7995457

Browse files
committed
Create assertion and type-class for xs:integer
1 parent 9c26356 commit 7995457

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @method static void validID(mixed $value, string $message = '', string $exception = '')
2424
* @method static void validIDRef(mixed $value, string $message = '', string $exception = '')
2525
* @method static void validIDRefs(mixed $value, string $message = '', string $exception = '')
26+
* @method static void validInteger(mixed $value, string $message = '', string $exception = '')
2627
* @method static void validLang(mixed $value, string $message = '', string $exception = '')
2728
* @method static void validMonth(mixed $value, string $message = '', string $exception = '')
2829
* @method static void validName(mixed $value, string $message = '', string $exception = '')
@@ -49,6 +50,7 @@
4950
* @method static void nullOrValidID(mixed $value, string $message = '', string $exception = '')
5051
* @method static void nullOrValidIDRef(mixed $value, string $message = '', string $exception = '')
5152
* @method static void nullOrValidIDRefs(mixed $value, string $message = '', string $exception = '')
53+
* @method static void nullOrValidInteger(mixed $value, string $message = '', string $exception = '')
5254
* @method static void nullOrValidLang(mixed $value, string $message = '', string $exception = '')
5355
* @method static void nullOrValidMonth(mixed $value, string $message = '', string $exception = '')
5456
* @method static void nullOrValidName(mixed $value, string $message = '', string $exception = '')
@@ -75,6 +77,7 @@
7577
* @method static void allValidID(mixed $value, string $message = '', string $exception = '')
7678
* @method static void allValidIDRef(mixed $value, string $message = '', string $exception = '')
7779
* @method static void allValidIDRefs(mixed $value, string $message = '', string $exception = '')
80+
* @method static void allValidInteger(mixed $value, string $message = '', string $exception = '')
7881
* @method static void allValidLang(mixed $value, string $message = '', string $exception = '')
7982
* @method static void allValidMonth(mixed $value, string $message = '', string $exception = '')
8083
* @method static void allValidName(mixed $value, string $message = '', string $exception = '')
@@ -104,6 +107,7 @@ class Assert extends BaseAssert
104107
use IDTrait;
105108
use IDRefTrait;
106109
use IDRefsTrait;
110+
use IntegerTrait;
107111
use LangTrait;
108112
use MonthTrait;
109113
use NameTrait;

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

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

tests/Assert/IntegerTest.php

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\IntegerTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class IntegerTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $integer
24+
*/
25+
#[DataProvider('provideInteger')]
26+
public function testValidInteger(bool $shouldPass, string $integer): void
27+
{
28+
try {
29+
Assert::validInteger($integer);
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 provideInteger(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid integer' => [true, '123456'],
45+
'valid positive signed' => [true, '+00000012'],
46+
'valid negative signed' => [true, '-1'],
47+
'invalid with space' => [false, '1 234'],
48+
'invalid with fractional' => [false, '1234.'],
49+
'invalid with thousands-delimiter' => [false, '+1,234'],
50+
];
51+
}
52+
}

tests/Type/IntegerValueTest.php

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\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\IntegerValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\IntegerValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(IntegerValue::class)]
19+
final class IntegerValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $integer
24+
*/
25+
#[DataProvider('provideInteger')]
26+
public function testInteger(bool $shouldPass, string $integer): void
27+
{
28+
try {
29+
IntegerValue::fromString($integer);
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 provideInteger(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid integer' => [true, '123456'],
45+
'valid positive signed' => [true, '+00000012'],
46+
'valid negative signed' => [true, '-1'],
47+
'valid with whitespace collapse' => [true, ' 1 234 '],
48+
'invalid with fractional' => [false, '1234.'],
49+
'invalid with thousands-delimiter' => [false, '+1,234'],
50+
];
51+
}
52+
}

0 commit comments

Comments
 (0)