-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create assertion and type-class for xs:unsignedLong
- Loading branch information
Showing
5 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |