Apply contribution guidelines to "Xdigit" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2019-04-05 20:09:17 +02:00
parent 18ce48371c
commit cb528ae612
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 99 additions and 83 deletions

View file

@ -3,7 +3,7 @@
- `Xdigit()`
- `Xdigit(string ...$additionalChars)`
Accepts an hexadecimal number:
Validates whether the input is an hexadecimal number or not.
```php
v::xdigit()->validate('abc123'); // true

View file

@ -25,11 +25,11 @@ final class XdigitException extends FilteredValidationException
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} contain only hexadecimal digits',
self::EXTRA => '{{name}} contain only hexadecimal digits and "{{additionalChars}}"',
self::EXTRA => '{{name}} contain only hexadecimal digits and {{additionalChars}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not contain hexadecimal digits',
self::EXTRA => '{{name}} must not contain hexadecimal digits or "{{additionalChars}}"',
self::EXTRA => '{{name}} must not contain hexadecimal digits or {{additionalChars}}',
],
];
}

View file

@ -19,8 +19,11 @@ use function ctype_xdigit;
* @author Andre Ramaciotti <andre@ramaciotti.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class Xdigit extends AbstractFilterRule
final class Xdigit extends AbstractFilterRule
{
/**
* {@inheritDoc}
*/
protected function validateFilteredInput(string $input): bool
{
return ctype_xdigit($input);

View file

@ -0,0 +1,70 @@
--CREDITS--
Henrique Moody <henriquemoody@gmail.com>
--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\XdigitException;
use Respect\Validation\Validator as v;
try {
v::xdigit()->check('aaa%a');
} catch (XdigitException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::xdigit(' ')->check('bbb%b');
} catch (XdigitException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::xdigit())->check('ccccc');
} catch (XdigitException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::xdigit('% '))->check('ddd%d');
} catch (XdigitException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::xdigit()->assert('eee^e');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::xdigit())->assert('fffff');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::xdigit('* &%')->assert('000^0');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::xdigit('^'))->assert('111^1');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
"aaa%a" contain only hexadecimal digits
"bbb%b" contain only hexadecimal digits and " "
"ccccc" must not contain hexadecimal digits
"ddd%d" must not contain hexadecimal digits or "% "
- "eee^e" contain only hexadecimal digits
- "fffff" must not contain hexadecimal digits
- "000^0" contain only hexadecimal digits and "* &%"
- "111^1" must not contain hexadecimal digits or "^"

View file

@ -13,11 +13,11 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\XdigitException
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Xdigit
*
@ -27,94 +27,37 @@ use Respect\Validation\Test\TestCase;
* @author Nick Lombard <github@jigsoft.co.za>
* @author Pascal Borreli <pascal@borreli.com>
*/
final class XdigitTest extends TestCase
final class XdigitTest extends RuleTestCase
{
/**
* @var Xdigit
* {@inheritDoc}
*/
protected $xdigitsValidator;
protected function setUp(): void
{
$this->xdigitsValidator = new Xdigit();
}
/**
* @dataProvider providerForXdigit
*
* @test
*
* @param mixed $input
*/
public function validateValidHexasdecimalDigits($input): void
{
$this->xdigitsValidator->assert($input);
$this->xdigitsValidator->check($input);
self::assertTrue($this->xdigitsValidator->validate($input));
}
/**
* @dataProvider providerForNotXdigit
* @expectedException \Respect\Validation\Exceptions\XdigitException
*
* @test
*
* @param mixed $input
*/
public function invalidHexadecimalDigitsShouldThrowXdigitException($input): void
{
self::assertFalse($this->xdigitsValidator->validate($input));
$this->xdigitsValidator->assert($input);
}
/**
* @dataProvider providerAdditionalChars
*
* @test
*/
public function additionalCharsShouldBeRespected(string $additional, string $input): void
{
$validator = new Xdigit($additional);
self::assertTrue($validator->validate($input));
}
/**
* @return string[][]
*/
public function providerAdditionalChars(): array
public function providerForValidInput(): array
{
return [
['!@#$%^&*(){} ', '!@#$%^&*(){} abc 123'],
["[]?+=/\\-_|\"',<>. \t\n", "[]?+=/\\-_|\"',<>. \t \n abc 123"],
[new Xdigit(), 'FFF'],
[new Xdigit(), '15'],
[new Xdigit(), 'DE12FA'],
[new Xdigit(), '1234567890abcdef'],
[new Xdigit(), 443],
[new Xdigit(), 0x123],
[new Xdigit('!@#$%^&*(){} '), '!@#$%^&*(){} abc 123'],
[new Xdigit("[]?+=/\\-_|\"',<>. \t\n"), "[]?+=/\\-_|\"',<>. \t \n abc 123"],
];
}
/**
* @return mixed[][]
* {@inheritDoc}
*/
public function providerForXdigit(): array
public function providerForInvalidInput(): array
{
return [
['FFF'],
['15'],
['DE12FA'],
['1234567890abcdef'],
[0x123],
];
}
/**
* @return mixed[][]
*/
public function providerForNotXdigit(): array
{
return [
[''],
[null],
['j'],
[' '],
['Foo'],
['1.5'],
[new Xdigit(), ''],
[new Xdigit(), null],
[new Xdigit(), 'j'],
[new Xdigit(), ' '],
[new Xdigit(), 'Foo'],
[new Xdigit(), '1.5'],
];
}
}