Skip to content

Commit 8a8494c

Browse files
committed
Create assertion and type-class for xs:string
1 parent c0f54b7 commit 8a8494c

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
2828
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
2929
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
30+
* @method static void validString(mixed $value, string $message = '', string $exception = '')
3031
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
3132
* @method static void validToken(mixed $value, string $message = '', string $exception = '')
3233
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
@@ -48,6 +49,7 @@
4849
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
4950
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
5051
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
52+
* @method static void nullOrValidString(mixed $value, string $message = '', string $exception = '')
5153
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
5254
* @method static void nullOrValidToken(mixed $value, string $message = '', string $exception = '')
5355
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
@@ -69,6 +71,7 @@
6971
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
7072
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
7173
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
74+
* @method static void allValidString(mixed $value, string $message = '', string $exception = '')
7275
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
7376
* @method static void allValidToken(mixed $value, string $message = '', string $exception = '')
7477
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
@@ -93,6 +96,7 @@ class Assert extends BaseAssert
9396
use NMTokenTrait;
9497
use NMTokensTrait;
9598
use QNameTrait;
99+
use StringTrait;
96100
use TimeTrait;
97101
use TokenTrait;
98102
use YearMonthTrait;

src/Assert/StringTrait.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 StringTrait
11+
{
12+
/**
13+
* @param string $value
14+
* @param string $message
15+
*/
16+
protected static function validString(string $value, string $message = ''): void
17+
{
18+
}
19+
}

src/Type/StringValue.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
/**
8+
* @package simplesaml/xml-common
9+
*/
10+
class StringValue extends AbstractValueType
11+
{
12+
}

tests/Assert/StringTest.php

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\StringTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class StringTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $str
24+
*/
25+
#[DataProvider('provideString')]
26+
public function testString(bool $shouldPass, string $str): void
27+
{
28+
try {
29+
Assert::validString($str);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: boolean, 1: string, 2: string}>
39+
*/
40+
public static function provideString(): array
41+
{
42+
return [
43+
'preserve spaces' => [true, ' Snoopy ', ' Snoopy '],
44+
'replace whitespace' => [true, " Snoopy\t\n\rrulez ", ' Snoopy rulez '],
45+
];
46+
}
47+
}

tests/Type/StringValueTest.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\StringValue;
11+
12+
/**
13+
* Class \SimpleSAML\Test\Type\StringValueValueTest
14+
*
15+
* @package simplesamlphp/xml-common
16+
*/
17+
#[CoversClass(StringValue::class)]
18+
final class StringValueTest extends TestCase
19+
{
20+
/**
21+
* @param string $str
22+
* @param string $stringValue
23+
*/
24+
#[DataProvider('provideString')]
25+
public function testString(string $str, string $stringValue): void
26+
{
27+
$value = StringValue::fromString($str);
28+
$this->assertEquals($stringValue, $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+
'no trim' => ['Snoopy ', 'Snoopy '],
41+
'no replace whitespace' => ["Snoopy\trulez", "Snoopy\trulez"],
42+
'no collapse' => ["Snoopy\t\n\rrulez", "Snoopy\t\n\rrulez"],
43+
];
44+
}
45+
}

0 commit comments

Comments
 (0)