-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create assertion and type-class for xs:duration
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML\Assert; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Assert\AssertionFailedException; | ||
use SimpleSAML\XML\Assert\Assert; | ||
|
||
/** | ||
* Class \SimpleSAML\Test\XML\Assert\DurationTest | ||
* | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
#[CoversClass(Assert::class)] | ||
final class DurationTest extends TestCase | ||
{ | ||
/** | ||
* @param boolean $shouldPass | ||
* @param string $duration | ||
*/ | ||
#[DataProvider('provideDuration')] | ||
public function testValidDuration(bool $shouldPass, string $duration): void | ||
{ | ||
try { | ||
Assert::validDuration($duration); | ||
$this->assertTrue($shouldPass); | ||
} catch (AssertionFailedException $e) { | ||
$this->assertFalse($shouldPass); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, array{0: bool, 1: string}> | ||
*/ | ||
public static function provideDuration(): array | ||
{ | ||
return [ | ||
'valid long seconds' => [true, 'PT1004199059S'], | ||
'valid short seconds' => [true, 'PT130S'], | ||
'valid minutes and seconds' => [true, 'PT2M10S'], | ||
'valid one day and two seconds' => [true, 'P1DT2S'], | ||
'valid minus one year' => [true, '-P1Y'], | ||
'valid complex sub-second' => [true, 'P1Y2M3DT5H20M30.123S'], | ||
'invalid missing P' => [false, '1Y'], | ||
'invalid missing T' => [false, 'P1S'], | ||
'invalid all parts must be positive' => [false, 'P-1Y'], | ||
'invalid order Y must precede M' => [false, 'P1M2Y'], | ||
'invalid all parts must me positive' => [false, 'P1Y-1M'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML\Type; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\Type\DurationValue; | ||
|
||
/** | ||
* Class \SimpleSAML\Test\Type\DurationValueTest | ||
* | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
#[CoversClass(DurationValue::class)] | ||
final class DurationValueTest extends TestCase | ||
{ | ||
/** | ||
* @param boolean $shouldPass | ||
* @param string $duration | ||
*/ | ||
#[DataProvider('provideDuration')] | ||
public function testDuration(bool $shouldPass, string $duration): void | ||
{ | ||
try { | ||
DurationValue::fromString($duration); | ||
$this->assertTrue($shouldPass); | ||
} catch (SchemaViolationException $e) { | ||
$this->assertFalse($shouldPass); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, array{0: bool, 1: string}> | ||
*/ | ||
public static function provideDuration(): array | ||
{ | ||
return [ | ||
'valid long seconds' => [true, 'PT1004199059S'], | ||
'valid short seconds' => [true, 'PT130S'], | ||
'valid minutes and seconds' => [true, 'PT2M10S'], | ||
'valid one day and two seconds' => [true, 'P1DT2S'], | ||
'valid minus one year' => [true, '-P1Y'], | ||
'valid complex sub-second' => [true, 'P1Y2M3DT5H20M30.123S'], | ||
'invalid missing P' => [false, '1Y'], | ||
'invalid missing T' => [false, 'P1S'], | ||
'invalid all parts must be positive' => [false, 'P-1Y'], | ||
'invalid order Y must precede M' => [false, 'P1M2Y'], | ||
'invalid all parts must me positive' => [false, 'P1Y-1M'], | ||
]; | ||
} | ||
} |