mirror of
https://github.com/Respect/Validation.git
synced 2024-06-09 01:02:16 +02:00
Apply contribution guidelines to "Cntrl" rule
After writing the integration tests of the "Cntrl" rule we noticed that it is generating extra double-quotes for the "{{additionalChars}}" template placeholder. This commit will also remove those extra double-quotes. Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
688fbde552
commit
74c3d1acc3
|
@ -3,7 +3,8 @@
|
|||
- `Cntrl()`
|
||||
- `Cntrl(string ...$additionalChars)`
|
||||
|
||||
This is similar to `Alnum()`, but only accepts control characters:
|
||||
Validates if all of the characters in the provided string, are control
|
||||
characters.
|
||||
|
||||
```php
|
||||
v::cntrl()->validate("\n\r\t"); // true
|
||||
|
|
|
@ -15,9 +15,10 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
/**
|
||||
* @author Andre Ramaciotti <andre@ramaciotti.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class CntrlException extends FilteredValidationException
|
||||
final class CntrlException extends FilteredValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -25,11 +26,11 @@ class CntrlException extends FilteredValidationException
|
|||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must contain only control characters',
|
||||
self::EXTRA => '{{name}} must contain only control characters and "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must contain only control characters and {{additionalChars}}',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not contain control characters',
|
||||
self::EXTRA => '{{name}} must not contain control characters or "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must not contain control characters or {{additionalChars}}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,13 +13,21 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use function ctype_cntrl;
|
||||
|
||||
/**
|
||||
* Validates if all of the characters in the provided string, are control characters.
|
||||
*
|
||||
* @author Andre Ramaciotti <andre@ramaciotti.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
*/
|
||||
class Cntrl extends AbstractFilterRule
|
||||
final class Cntrl extends AbstractFilterRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function validateFilteredInput(string $input): bool
|
||||
{
|
||||
return ctype_cntrl($input);
|
||||
|
|
71
tests/integration/rules/cntrl.phpt
Normal file
71
tests/integration/rules/cntrl.phpt
Normal file
|
@ -0,0 +1,71 @@
|
|||
--CREDITS--
|
||||
Danilo Correa <danilosilva87@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\CntrlException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::cntrl()->check('16-50');
|
||||
} catch (CntrlException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::cntrl('16')->check('16-50');
|
||||
} catch (CntrlException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::cntrl())->check("\n");
|
||||
} catch (CntrlException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::cntrl('16'))->check("16\n");
|
||||
} catch (CntrlException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::cntrl()->assert('Foo');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::cntrl('Bar')->assert('Foo');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::cntrl())->assert("\n");
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::cntrl('Bar'))->assert("Bar\n");
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
"16-50" must contain only control characters
|
||||
"16-50" must contain only control characters and "16"
|
||||
"\n" must not contain control characters
|
||||
"16\n" must not contain control characters or "16"
|
||||
- "Foo" must contain only control characters
|
||||
- "Foo" must contain only control characters and "Bar"
|
||||
- "\n" must not contain control characters
|
||||
- "Bar\n" must not contain control characters or "Bar"
|
|
@ -13,11 +13,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Test\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\CntrlException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\AbstractFilterRule
|
||||
* @covers \Respect\Validation\Rules\Cntrl
|
||||
*
|
||||
|
@ -27,84 +28,44 @@ use Respect\Validation\Test\TestCase;
|
|||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
* @author Pascal Borreli <pascal@borreli.com>
|
||||
*/
|
||||
final class CntrlTest extends TestCase
|
||||
final class CntrlTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForValidCntrl
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validDataWithCntrlShouldReturnTrue(string $input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$validator = new Cntrl();
|
||||
self::assertTrue($validator->validate($input));
|
||||
}
|
||||
$cntrl = new Cntrl();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidCntrl
|
||||
* @expectedException \Respect\Validation\Exceptions\CntrlException
|
||||
*
|
||||
* @test
|
||||
*
|
||||
* @param mixed $input
|
||||
*/
|
||||
public function invalidCntrlShouldFailAndThrowCntrlException($input): void
|
||||
{
|
||||
$validator = new Cntrl();
|
||||
self::assertFalse($validator->validate($input));
|
||||
$validator->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerAdditionalChars
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function additionalCharsShouldBeRespected(string $additional, string $input): void
|
||||
{
|
||||
$validator = new Cntrl($additional);
|
||||
self::assertTrue($validator->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function providerAdditionalChars(): array
|
||||
{
|
||||
return [
|
||||
['!@#$%^&*(){} ', '!@#$%^&*(){} '],
|
||||
['[]?+=/\\-_|"\',<>. ', "[]?+=/\\-_|\"',<>. \t \n"],
|
||||
'\n' => [$cntrl, "\n"],
|
||||
'\r' => [$cntrl, "\r"],
|
||||
'\t' => [$cntrl, "\t"],
|
||||
'\n\r\t' => [$cntrl, "\n\r\t"],
|
||||
'Ignoring all characters' => [new Cntrl('!@#$%^&*(){} '), '!@#$%^&*(){} '],
|
||||
'Ignoring some characters' => [new Cntrl('[]?+=/\\-_|"\',<>. '), "[]?+=/\\-_|\"',<>. \t \n"],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidCntrl(): array
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
["\n"],
|
||||
["\r"],
|
||||
["\t"],
|
||||
["\n\r\t"],
|
||||
];
|
||||
}
|
||||
$cntrl = new Cntrl();
|
||||
|
||||
/**
|
||||
* @return mixed[][]
|
||||
*/
|
||||
public function providerForInvalidCntrl(): array
|
||||
{
|
||||
return [
|
||||
[''],
|
||||
['16-50'],
|
||||
['a'],
|
||||
[' '],
|
||||
['Foo'],
|
||||
['12.1'],
|
||||
['-12'],
|
||||
[-12],
|
||||
['alganet'],
|
||||
'empty parameter' => [$cntrl, ''],
|
||||
'16-50' => [$cntrl, '16-50'],
|
||||
'a' => [$cntrl, 'a'],
|
||||
'white space' => [$cntrl, ' '],
|
||||
'Foo' => [$cntrl, 'Foo'],
|
||||
'12.1' => [$cntrl, '12.1'],
|
||||
'"-12"' => [$cntrl, '-12'],
|
||||
'-12' => [$cntrl, -12],
|
||||
'alganet' => [$cntrl, 'alganet'],
|
||||
'empty array parameter' => [$cntrl, []],
|
||||
'object' => [$cntrl, new stdClass()],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue