Skip to content

Commit 39e113f

Browse files
committed
Create assertion and type-class for xs:ENTITY
1 parent dd38060 commit 39e113f

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

src/Assert/EntityTrait.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 EntityTrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validEntity(string $value, string $message = ''): void
19+
{
20+
Assert::validNCName(
21+
$value,
22+
$message ?: '%s is not a valid xs:Entity',
23+
InvalidArgumentException::class,
24+
);
25+
}
26+
}

tests/Assert/EntityTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\EntityTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class EntityTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $entity
24+
*/
25+
#[DataProvider('provideEntity')]
26+
public function testValidEntity(bool $shouldPass, string $entity): void
27+
{
28+
try {
29+
Assert::validEntity($entity);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<int, array{0: bool, 1: string}>
39+
*/
40+
public static function provideEntity(): array
41+
{
42+
return [
43+
[true, 'Test'],
44+
// May start with an underscore
45+
[true, '_Test'],
46+
// May contain dashes
47+
[true, '_1950-10-04_10-00'],
48+
// May contain dots
49+
[true, 'Te.st'],
50+
// May contain diacriticals
51+
[true, 'fööbár'],
52+
// Prefixed v4 UUID
53+
[true, '_5425e58e-e799-4884-92cc-ca64ecede32f'],
54+
// An empty value is not valid, unless xsi:nil is used
55+
[false, ''],
56+
// Wildcards are not allowed
57+
[false, 'Te*st'],
58+
// May not start with a digit
59+
[false, '1Test'],
60+
// May not contain a colon
61+
[false, 'Te:st'],
62+
// Trailing newlines are forbidden
63+
[false, "Test\n"],
64+
];
65+
}
66+
}

tests/Type/EntityValueTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\EntityValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\EntityValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(EntityValue::class)]
19+
final class EntityValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $entity
24+
*/
25+
#[DataProvider('provideEntity')]
26+
public function testIDRef(bool $shouldPass, string $entity): void
27+
{
28+
try {
29+
EntityValue::fromString($entity);
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 provideEntity(): array
41+
{
42+
return [
43+
'valid' => [true, 'Test'],
44+
'valid starts with underscore' => [true, '_Test'],
45+
'valid contains dashes' => [true, '_1950-10-04_10-00'],
46+
'valid contains dots' => [true, 'Te.st'],
47+
'valid contains diacriticals' => [true, 'fööbár'],
48+
'valid prefixed v4 UUID' => [true, '_5425e58e-e799-4884-92cc-ca64ecede32f'],
49+
'invalid empty string' => [false, ''],
50+
'invalid contains wildcard' => [false, 'Te*st'],
51+
'invalid starts with digit' => [false, '1Test'],
52+
'invalid contains colon' => [false, 'Te:st'],
53+
'whitespace collapse' => [true, "foobar\n"],
54+
'normalization' => [true, ' foobar '],
55+
];
56+
}
57+
}

0 commit comments

Comments
 (0)