Skip to content

Commit

Permalink
Create assertion and type-class for xs:unsignedLong
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 14, 2025
1 parent 0c10520 commit 769a9ff
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* @method static void validString(mixed $value, string $message = '', string $exception = '')
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
* @method static void validToken(mixed $value, string $message = '', string $exception = '')
* @method static void validUnsignedLong(mixed $value, string $message = '', string $exception = '')
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '')
Expand Down Expand Up @@ -82,6 +83,7 @@
* @method static void nullOrValidString(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidToken(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
* @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '')
Expand Down Expand Up @@ -119,6 +121,7 @@
* @method static void allValidString(mixed $value, string $message = '', string $exception = '')
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
* @method static void allValidToken(mixed $value, string $message = '', string $exception = '')
* @method static void allValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
*/
class Assert extends BaseAssert
Expand Down Expand Up @@ -159,5 +162,6 @@ class Assert extends BaseAssert
use StringTrait;
use TimeTrait;
use TokenTrait;
use UnsignedLongTrait;
use YearMonthTrait;
}
30 changes: 30 additions & 0 deletions src/Assert/UnsignedLongTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

/**
* @package simplesamlphp/xml-common
*/
trait UnsignedLongTrait
{
/** @var string */
private static string $unsignedLong_regex = '/^([+]?[0]*)(?:[0-9]|[1-9][0-9]{1,19}|1000000000000000|10000000000000000|100000000000000000|1000000000000000000|1[0-8]000000000000000000|18[0-4]00000000000000000|184[0-4]0000000000000000|1844[0-6]000000000000000|18446[0-7]00000000000000|184467[0-4]0000000000000|1844674[0-4]000000000000|184467440[0-7]0000000000|1844674407[0-3]000000000|18446744073[0-7]00000000|1844674407370000000[0-9]|18446744073709[0-5]00000|184467440737095[0-5]0000|1844674407370955[0-2]000)$/D';

/**
* @param string $value
* @param string $message
*/
protected static function validUnsignedLong(string $value, string $message = ''): void
{
parent::regex(
$value,
self::$unsignedLong_regex,
$message ?: '%s is not a valid xs:unsignedLong',
InvalidArgumentException::class,
);
}
}
26 changes: 26 additions & 0 deletions src/Type/UnsignedLongValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Type;

use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

/**
* @package simplesaml/xml-common
*/
class UnsignedLongValue extends IntegerValue
{
/**
* Validate the value.
*
* @param string $value
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
* @return void
*/
protected function validateValue(string $value): void
{
Assert::validUnsignedLong($this->sanitizeValue($value), SchemaViolationException::class);
}
}
55 changes: 55 additions & 0 deletions tests/Assert/UnsignedLongTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML\Assert;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\XML\Assert\Assert;

/**
* Class \SimpleSAML\Test\XML\Assert\UnsignedLongTest
*
* @package simplesamlphp/xml-common
*/
#[CoversClass(Assert::class)]
final class UnsignedLongTest extends TestCase
{
/**
* @param boolean $shouldPass
* @param string $unsignedLong
*/
#[DataProvider('provideUnsignedLong')]
public function testValidUnsignedLong(bool $shouldPass, string $unsignedLong): void
{
try {
Assert::validUnsignedLong($unsignedLong);
$this->assertTrue($shouldPass);
} catch (AssertionFailedException $e) {
$this->assertFalse($shouldPass);
}
}


/**
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideUnsignedLong(): array
{
return [
'empty' => [false, ''],
'valid positive integer' => [true, '18446744073709551615'],
'invalid positive out-of-bounds' => [true, '18446744073709551616'],
'valid signed positive integer' => [true, '+18446744073709551615'],
'valid zero' => [true, '0'],
'valid negative leading zeros' => [true, '0000000000000000000005'],
'invalid with fractional' => [false, '1.'],
'invalid with space' => [false, '12 34'],
'invalid negative' => [false, '-1'],
'invalid with thousands-delimiter' => [false, '1,234'],
];
}
}
55 changes: 55 additions & 0 deletions tests/Type/UnsignedLongValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML\Type;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\Type\UnsignedLongValue;

/**
* Class \SimpleSAML\Test\Type\UnsignedLongValueTest
*
* @package simplesamlphp/xml-common
*/
#[CoversClass(UnsignedLongValue::class)]
final class UnsignedLongValueTest extends TestCase
{
/**
* @param boolean $shouldPass
* @param string $unsignedLong
*/
#[DataProvider('provideUnsignedLong')]
public function testUnsignedLong(bool $shouldPass, string $unsignedLong): void
{
try {
UnsignedLongValue::fromString($unsignedLong);
$this->assertTrue($shouldPass);
} catch (SchemaViolationException $e) {
$this->assertFalse($shouldPass);
}
}


/**
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideUnsignedLong(): array
{
return [
'empty' => [false, ''],
'valid positive integer' => [true, '18446744073709551615'],
'invalid positive out-of-bounds' => [true, '18446744073709551616'],
'valid signed positive integer' => [true, '+18446744073709551615'],
'valid zero' => [true, '0'],
'valid negative leading zeros' => [true, '0000000000000000000005'],
'invalid with fractional' => [false, '1.'],
'valid with whitespace collapse' => [true, " 1 234 \n"],
'invalid negative' => [false, '-1'],
'invalid with thousands-delimiter' => [false, '1,234'],
];
}
}

0 comments on commit 769a9ff

Please sign in to comment.