Skip to content

Commit bd4254d

Browse files
committed
Create assertion and type-class for xs:gDay
1 parent 7c0751b commit bd4254d

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

src/Assert/DayTrait.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 DayTrait
13+
{
14+
/**
15+
* Format: ---DD with optional timezone representation
16+
*
17+
* @var string
18+
*/
19+
private static string $day_regex = '/^---(0[1-9]|1[1-9]|2[1-9]|3[01])((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?$/Di';
20+
21+
22+
/**
23+
* @param string $value
24+
* @param string $message
25+
*/
26+
protected static function validDay(string $value, string $message = ''): void
27+
{
28+
parent::regex(
29+
$value,
30+
self::$day_regex,
31+
$message ?: '%s is not a valid xs:gDay',
32+
InvalidArgumentException::class,
33+
);
34+
}
35+
}

src/Type/DayValue.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 DayValue extends AbstractValueType
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::validDay($value, SchemaViolationException::class);
25+
}
26+
}

tests/Assert/DayTest.php

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\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\DayTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class DayTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $day
24+
*/
25+
#[DataProvider('provideDay')]
26+
public function testValidDay(bool $shouldPass, string $day): void
27+
{
28+
try {
29+
Assert::validDay($day);
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 provideDay(): array
41+
{
42+
return [
43+
'valid' => [true, '---01'],
44+
'valid numeric timezone' => [true, '---01+02:00'],
45+
'valid Zulu timezone' => [true, '---01Z'],
46+
'valid 00:00 timezone' => [true, '---01+00:00'],
47+
'day 15' => [true, '---15'],
48+
'day 31' => [true, '---31'],
49+
'invalid format' => [false, '--30-'],
50+
'day out of range' => [false, '---35'],
51+
'missing leading dashes' => [false, '15'],
52+
];
53+
}
54+
}

tests/Type/DayValueTest.php

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\DayValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\DayValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(DayValue::class)]
19+
final class DayValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $day
24+
*/
25+
#[DataProvider('provideDay')]
26+
public function testDay(bool $shouldPass, string $day): void
27+
{
28+
try {
29+
DayValue::fromString($day);
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 provideDay(): array
41+
{
42+
return [
43+
'valid' => [true, '---01'],
44+
'valid numeric timezone' => [true, '---01+02:00'],
45+
'valid Zulu timezone' => [true, '---01Z'],
46+
'valid 00:00 timezone' => [true, '---01+00:00'],
47+
'day 15' => [true, '---15'],
48+
'day 31' => [true, '---31'],
49+
'invalid format' => [false, '--30-'],
50+
'day out of range' => [false, '---35'],
51+
'missing leading dashes' => [false, '15'],
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)