Skip to content

Commit dbf1884

Browse files
committed
Create assertion and type-class for xs:boolean
1 parent 94d7dd4 commit dbf1884

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* @method static void validAnyURI(mixed $value, string $message = '', string $exception = '')
1313
* @method static void validBase64Binary(mixed $value, string $message = '', string $exception = '')
14+
* @method static void validBoolean(mixed $value, string $message = '', string $exception = '')
1415
* @method static void validDate(mixed $value, string $message = '', string $exception = '')
1516
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
1617
* @method static void validDay(mixed $value, string $message = '', string $exception = '')
@@ -35,6 +36,7 @@
3536
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
3637
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
3738
* @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '')
39+
* @method static void nullOrValidBoolean(mixed $value, string $message = '', string $exception = '')
3840
* @method static void nullOrValidDate(mixed $value, string $message = '', string $exception = '')
3941
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
4042
* @method static void nullOrValidDay(mixed $value, string $message = '', string $exception = '')
@@ -59,6 +61,7 @@
5961
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
6062
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
6163
* @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '')
64+
* @method static void allValidBoolean(mixed $value, string $message = '', string $exception = '')
6265
* @method static void allValidDate(mixed $value, string $message = '', string $exception = '')
6366
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
6467
* @method static void allValidDay(mixed $value, string $message = '', string $exception = '')
@@ -86,6 +89,7 @@ class Assert extends BaseAssert
8689
{
8790
use AnyURITrait;
8891
use Base64BinaryTrait;
92+
use BooleanTrait;
8993
use DateTrait;
9094
use DateTimeTrait;
9195
use DayTrait;

src/Assert/BooleanTrait.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 BooleanTrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validBoolean(string $value, string $message = ''): void
19+
{
20+
parent::oneOf(
21+
$value,
22+
['true', 'false', '1', '0'],
23+
$message ?: '%s is not a valid xs:boolean',
24+
InvalidArgumentException::class,
25+
);
26+
}
27+
}

src/Type/BooleanValue.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use function str_replace;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class BooleanValue extends AbstractValueType
16+
{
17+
/**
18+
* Sanitize the value.
19+
*
20+
* @param string $value The unsanitized value
21+
* @return string
22+
*/
23+
protected function sanitizeValue(string $value): string
24+
{
25+
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
26+
}
27+
28+
29+
/**
30+
* Validate the value.
31+
*
32+
* @param string $value
33+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
34+
* @return void
35+
*/
36+
protected function validateValue(string $value): void
37+
{
38+
// Note: value must already be sanitized before validating
39+
Assert::validBoolean($this->sanitizeValue($value), SchemaViolationException::class);
40+
}
41+
}

tests/Assert/BooleanTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\BooleanTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class BooleanTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $boolean
24+
*/
25+
#[DataProvider('provideBoolean')]
26+
public function testValidBoolean(bool $shouldPass, string $boolean): void
27+
{
28+
try {
29+
Assert::validBoolean($boolean);
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 provideBoolean(): array
41+
{
42+
return [
43+
'true' => [true, 'true'],
44+
'false' => [true, 'false'],
45+
'one' => [true, '1'],
46+
'zero' => [true, '0'],
47+
'vrai' => [false, 'vrai'],
48+
'faux' => [false, 'faux'],
49+
];
50+
}
51+
}

tests/Type/BooleanValueTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\BooleanValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\BooleanValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(BooleanValue::class)]
19+
final class BooleanValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $boolean
24+
*/
25+
#[DataProvider('provideBoolean')]
26+
public function testBoolean(bool $shouldPass, string $boolean): void
27+
{
28+
try {
29+
BooleanValue::fromString($boolean);
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 provideBoolean(): array
41+
{
42+
return [
43+
'true' => [true, 'true'],
44+
'false' => [true, 'false'],
45+
'one' => [true, '1'],
46+
'zero' => [true, '0'],
47+
'whitespace collapse' => [true, ' tr ue '],
48+
'vrai' => [false, 'vrai'],
49+
'faux' => [false, 'faux'],
50+
];
51+
}
52+
}

0 commit comments

Comments
 (0)