Skip to content

Commit

Permalink
Create assertion and type-class for xs:duration
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 11, 2025
1 parent 1a55d95 commit 922d680
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/Assert/DurationTest.php
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'],
];
}
}
56 changes: 56 additions & 0 deletions tests/Type/DurationValueTest.php
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'],
];
}
}

0 comments on commit 922d680

Please sign in to comment.