Skip to content

Commit 40f38e0

Browse files
committed
Create assertion and type-class for xs:duration
1 parent a6101d8 commit 40f38e0

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

tests/Assert/DurationTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\DurationTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class DurationTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $duration
24+
*/
25+
#[DataProvider('provideDuration')]
26+
public function testValidDuration(bool $shouldPass, string $duration): void
27+
{
28+
try {
29+
Assert::validDuration($duration);
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 provideDuration(): array
41+
{
42+
return [
43+
'valid long seconds' => [true, 'PT1004199059S'],
44+
'valid short seconds' => [true, 'PT130S'],
45+
'valid minutes and seconds' => [true, 'PT2M10S'],
46+
'valid one day and two seconds' => [true, 'P1DT2S'],
47+
'valid minus one year' => [true, '-P1Y'],
48+
'valid complex sub-second' => [true, 'P1Y2M3DT5H20M30.123S'],
49+
'invalid missing P' => [false, '1Y'],
50+
'invalid missing T' => [false, 'P1S'],
51+
'invalid all parts must be positive' => [false, 'P-1Y'],
52+
'invalid order Y must precede M' => [false, 'P1M2Y'],
53+
'invalid all parts must me positive' => [false, 'P1Y-1M'],
54+
];
55+
}
56+
}

tests/Type/DurationValueTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\DurationValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\DurationValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(DurationValue::class)]
19+
final class DurationValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $duration
24+
*/
25+
#[DataProvider('provideDuration')]
26+
public function testDuration(bool $shouldPass, string $duration): void
27+
{
28+
try {
29+
DurationValue::fromString($duration);
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 provideDuration(): array
41+
{
42+
return [
43+
'valid long seconds' => [true, 'PT1004199059S'],
44+
'valid short seconds' => [true, 'PT130S'],
45+
'valid minutes and seconds' => [true, 'PT2M10S'],
46+
'valid one day and two seconds' => [true, 'P1DT2S'],
47+
'valid minus one year' => [true, '-P1Y'],
48+
'valid complex sub-second' => [true, 'P1Y2M3DT5H20M30.123S'],
49+
'invalid missing P' => [false, '1Y'],
50+
'invalid missing T' => [false, 'P1S'],
51+
'invalid all parts must be positive' => [false, 'P-1Y'],
52+
'invalid order Y must precede M' => [false, 'P1M2Y'],
53+
'invalid all parts must me positive' => [false, 'P1Y-1M'],
54+
];
55+
}
56+
}

0 commit comments

Comments
 (0)