Skip to content

Commit 2a98f36

Browse files
committed
Create assertion and type-class for xs:token
1 parent e8db6c7 commit 2a98f36

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
2929
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
3030
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
31+
* @method static void validToken(mixed $value, string $message = '', string $exception = '')
3132
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
3233
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
3334
* @method static void nullOrValidDate(mixed $value, string $message = '', string $exception = '')
@@ -48,6 +49,7 @@
4849
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
4950
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
5051
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
52+
* @method static void nullOrValidToken(mixed $value, string $message = '', string $exception = '')
5153
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
5254
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
5355
* @method static void allValidDate(mixed $value, string $message = '', string $exception = '')
@@ -68,6 +70,7 @@
6870
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
6971
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
7072
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
73+
* @method static void allValidToken(mixed $value, string $message = '', string $exception = '')
7174
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
7275
*/
7376
class Assert extends BaseAssert
@@ -91,5 +94,6 @@ class Assert extends BaseAssert
9194
use NMTokensTrait;
9295
use QNameTrait;
9396
use TimeTrait;
97+
use TokenTrait;
9498
use YearMonthTrait;
9599
}

src/Assert/TokenTrait.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 TokenTrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validToken(string $value, string $message = ''): void
19+
{
20+
}
21+
}

src/Type/TokenValue.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 function preg_replace;
8+
use function trim;
9+
10+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class TokenValue extends NormalizedStringValue
14+
{
15+
/**
16+
* Sanitize the value.
17+
*
18+
* @param string $value The unsanitized value
19+
* @return string
20+
*/
21+
protected function sanitizeValue(string $value): string
22+
{
23+
$content = parent::sanitizeValue($value);
24+
return trim(preg_replace('/\s+/', ' ', $value));
25+
}
26+
}

tests/Assert/TokenTest.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\TokenTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class TokenTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $token
24+
*/
25+
#[DataProvider('provideToken')]
26+
public function testValidToken(bool $shouldPass, string $token): void
27+
{
28+
try {
29+
Assert::validToken($token);
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 provideToken(): array
41+
{
42+
return [
43+
[true, 'Snoopy'],
44+
[true, ':CMS'],
45+
[true, 'fööbár'],
46+
[true, '-1950-10-04'],
47+
[true, ''],
48+
[true, '0836217462'],
49+
[true, 'foo bar'],
50+
[true, 'foo,bar'],
51+
[true, "foobar\n"],
52+
];
53+
}
54+
}

tests/Type/TokenValueTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Type\TokenValue;
11+
12+
/**
13+
* Class \SimpleSAML\Test\Type\TokenValueTest
14+
*
15+
* @package simplesamlphp/xml-common
16+
*/
17+
#[CoversClass(TokenValue::class)]
18+
final class TokenValueTest extends TestCase
19+
{
20+
/**
21+
* @param string $str
22+
* @param string $normalizedString
23+
*/
24+
#[DataProvider('provideString')]
25+
public function testToken(string $str, string $normalizedString): void
26+
{
27+
$value = TokenValue::fromString($str);
28+
$this->assertEquals($normalizedString, $value->getValue());
29+
$this->assertEquals($str, $value->getRawValue());
30+
}
31+
32+
33+
/**
34+
* @return array<string, array{0: string, 1: string}>
35+
*/
36+
public static function provideString(): array
37+
{
38+
return [
39+
'empty string' => ['', ''],
40+
'trim' => [' Snoopy ', 'Snoopy'],
41+
'replace whitespace' => ["Snoopy\trulez", 'Snoopy rulez'],
42+
'collapse' => ["Snoopy\t\n\rrulez", 'Snoopy rulez'],
43+
];
44+
}
45+
}

0 commit comments

Comments
 (0)