Skip to content

Commit be05fdf

Browse files
committed
Create assertion and type-class for xs:normalizedString
1 parent da5a410 commit be05fdf

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

src/Assert/Assert.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
2727
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
2828
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
29+
* @method static void validNormalizedString(mixed $value, string $message = '', string $exception = '')
2930
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
3031
* @method static void validString(mixed $value, string $message = '', string $exception = '')
3132
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
@@ -48,6 +49,7 @@
4849
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
4950
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
5051
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
52+
* @method static void nullOrValidNormalizedString(mixed $value, string $message = '', string $exception = '')
5153
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
5254
* @method static void nullOrValidString(mixed $value, string $message = '', string $exception = '')
5355
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
@@ -70,6 +72,7 @@
7072
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
7173
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
7274
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
75+
* @method static void allValidNormalizedString(mixed $value, string $message = '', string $exception = '')
7376
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
7477
* @method static void allValidString(mixed $value, string $message = '', string $exception = '')
7578
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')

src/Assert/NormalizedString.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
/**
8+
* @package simplesamlphp/xml-common
9+
*/
10+
trait NormalizedStringTrait
11+
{
12+
/**
13+
* @param string $value
14+
* @param string $message
15+
*/
16+
protected static function validNormalizedString(string $value, string $message = ''): void
17+
{
18+
}
19+
}

src/Type/NormalizedStringValue.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use function str_replace;
8+
9+
/**
10+
* @package simplesaml/xml-common
11+
*/
12+
class NormalizedStringValue extends StringValue
13+
{
14+
/**
15+
* Sanitize the value.
16+
*
17+
* @param string $value The unsanitized value
18+
* @return string
19+
*/
20+
protected function sanitizeValue(string $value): string
21+
{
22+
return str_replace(["\f", "\r", "\n", "\t", "\v"], ' ', $value);
23+
}
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\NormalizedStringTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class NormalizedStringTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $str
24+
*/
25+
#[DataProvider('provideString')]
26+
public function testNormalizedString(bool $shouldPass, string $str): void
27+
{
28+
try {
29+
Assert::validNormalizedString($str);
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 provideString(): array
41+
{
42+
return [
43+
'preserve spaces' => [' Snoopy ', ' Snoopy '],
44+
'replace whitespace' => [" Snoopy\t\n\rrulez ", ' Snoopy rulez '],
45+
];
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\NormalizedStringValue;
11+
12+
/**
13+
* Class \SimpleSAML\Test\Type\NormalizedStringValueTest
14+
*
15+
* @package simplesamlphp/xml-common
16+
*/
17+
#[CoversClass(NormalizedStringValue::class)]
18+
final class NormalizedStringValueTest extends TestCase
19+
{
20+
/**
21+
* @param string $str
22+
* @param string $normalizedString
23+
*/
24+
#[DataProvider('provideString')]
25+
public function testNormalizedString(string $str, string $normalizedString): void
26+
{
27+
$value = NormalizedStringValue::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+
'preserve spaces' => [' Snoopy ', ' Snoopy '],
40+
'replace whitespace' => [" Snoopy\t\n\rrulez ", ' Snoopy rulez '],
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)