Skip to content

Commit 7c0751b

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

File tree

5 files changed

+160
-66
lines changed

5 files changed

+160
-66
lines changed

src/Assert/AnyURITrait.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\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait AnyURITrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validAnyURI(string $value, string $message = ''): void
19+
{
20+
parent::validURI(
21+
$value,
22+
$message ?: '%s is not a valid xs:anyURI',
23+
InvalidArgumentException::class,
24+
);
25+
}
26+
}

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

tests/Assert/AnyURITest.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\AnyURITest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class AnyURITest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $uri
24+
*/
25+
#[DataProvider('provideURI')]
26+
public function testValidURI(bool $shouldPass, string $uri): void
27+
{
28+
try {
29+
Assert::validAnyURI($uri);
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 provideURI(): array
41+
{
42+
return [
43+
'urn' => [true, 'urn:x-simplesamlphp:phpunit'],
44+
'same-doc' => [true, '#_53d830ab1be17291a546c95c7f1cdf8d3d23c959e6'],
45+
'url' => [true, 'https://www.simplesamlphp.org'],
46+
'diacritical' => [true, 'https://aä.com'],
47+
'spn' => [true, 'spn:a4cf592f-a64c-46ff-a788-b260f474525b'],
48+
'typos' => [true, 'https//www.uni.l/en/'],
49+
'spaces' => [true, 'this is silly'],
50+
'empty' => [true, ''],
51+
'azure-common' => [true, 'https://sts.windows.net/{tenantid}/'],
52+
];
53+
}
54+
}

tests/Assert/EntityTest.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

tests/Type/AnyURIValueTest.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\AnyURIValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\AnyURIValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(AnyURIValue::class)]
19+
final class AnyURIValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $uri
24+
*/
25+
#[DataProvider('provideURI')]
26+
public function testAnyURI(bool $shouldPass, string $uri): void
27+
{
28+
try {
29+
AnyURIValue::fromString($uri);
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 provideURI(): array
41+
{
42+
return [
43+
'urn' => [true, 'urn:x-simplesamlphp:phpunit'],
44+
'same-doc' => [true, '#_53d830ab1be17291a546c95c7f1cdf8d3d23c959e6'],
45+
'url' => [true, 'https://www.simplesamlphp.org'],
46+
'diacritical' => [true, 'https://aä.com'],
47+
'spn' => [true, 'spn:a4cf592f-a64c-46ff-a788-b260f474525b'],
48+
'typos' => [true, 'https//www.uni.l/en/'],
49+
'spaces' => [true, 'this is silly'],
50+
'empty' => [true, ''],
51+
'azure-common' => [true, 'https://sts.windows.net/{tenantid}/'],
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)