mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 06:42:22 +02:00
Apply contribution guidelines to "Consonant" rule
After seeing the integration tests of the "Consonant" 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
74c3d1acc3
commit
ffc349cb03
|
@ -3,7 +3,7 @@
|
|||
- `Consonant()`
|
||||
- `Consonant(string ...$additionalChars)`
|
||||
|
||||
Similar to `Alnum()`. Validates strings that contain only consonants:
|
||||
Validates if the input contains only consonants.
|
||||
|
||||
```php
|
||||
v::consonant()->validate('xkcd'); // true
|
||||
|
|
|
@ -15,9 +15,10 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Kleber Hamada Sato <kleberhs007@yahoo.com>
|
||||
*/
|
||||
class ConsonantException extends FilteredValidationException
|
||||
final class ConsonantException extends FilteredValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -25,11 +26,11 @@ class ConsonantException extends FilteredValidationException
|
|||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must contain only consonants',
|
||||
self::EXTRA => '{{name}} must contain only consonants and "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must contain only consonants and {{additionalChars}}',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not contain consonants',
|
||||
self::EXTRA => '{{name}} must not contain consonants or "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must not contain consonants or {{additionalChars}}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -16,11 +16,17 @@ namespace Respect\Validation\Rules;
|
|||
use function preg_match;
|
||||
|
||||
/**
|
||||
* Validates if the input contains only consonants.
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
*/
|
||||
class Consonant extends AbstractFilterRule
|
||||
final class Consonant extends AbstractFilterRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function validateFilteredInput(string $input): bool
|
||||
{
|
||||
return preg_match('/^(\s|[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z])*$/', $input) > 0;
|
||||
|
|
73
tests/integration/rules/consonant.phpt
Normal file
73
tests/integration/rules/consonant.phpt
Normal file
|
@ -0,0 +1,73 @@
|
|||
--CREDITS--
|
||||
Danilo Correa <danilosilva87@gmail.com>
|
||||
Edson Lima <dddwebdeveloper@gmail.com>
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ConsonantException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::consonant()->check('aeiou');
|
||||
} catch (ConsonantException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::consonant('d')->check('daeiou');
|
||||
} catch (ConsonantException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::consonant())->check('bcd');
|
||||
} catch (ConsonantException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::consonant('a'))->check('abcd');
|
||||
} catch (ConsonantException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::consonant()->assert('aeiou');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::consonant('d')->assert('daeiou');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::consonant())->assert('bcd');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::consonant('a'))->assert('abcd');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
"aeiou" must contain only consonants
|
||||
"daeiou" must contain only consonants and "d"
|
||||
"bcd" must not contain consonants
|
||||
"abcd" must not contain consonants or "a"
|
||||
- "aeiou" must contain only consonants
|
||||
- "daeiou" must contain only consonants and "d"
|
||||
- "bcd" must not contain consonants
|
||||
- "abcd" must not contain consonants or "a"
|
|
@ -1,18 +0,0 @@
|
|||
--CREDITS--
|
||||
Edson Lima <dddwebdeveloper@gmail.com>
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::consonant()->check('bcd');
|
||||
v::consonant()->assert('ddd');
|
||||
v::not(v::consonant())->check('uou');
|
||||
v::not(v::consonant())->assert('aaaaa');
|
||||
?>
|
||||
--EXPECT--
|
|
@ -1,21 +0,0 @@
|
|||
--CREDITS--
|
||||
Edson Lima <dddwebdeveloper@gmail.com>
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ConsonantException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::consonant()->check('top nos falsetes');
|
||||
} catch (ConsonantException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"top nos falsetes" must contain only consonants
|
|
@ -1,21 +0,0 @@
|
|||
--CREDITS--
|
||||
Edson Lima <dddwebdeveloper@gmail.com>
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ConsonantException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::consonant())->check('ddd');
|
||||
} catch (ConsonantException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"ddd" must not contain consonants
|
|
@ -1,21 +0,0 @@
|
|||
--CREDITS--
|
||||
Edson Lima <dddwebdeveloper@gmail.com>
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::consonant()->assert('Jaspion');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
- "Jaspion" must contain only consonants
|
|
@ -1,21 +0,0 @@
|
|||
--CREDITS--
|
||||
Edson Lima <dddwebdeveloper@gmail.com>
|
||||
Henrique Moody <henriquemoody@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::consonant())->assert('bb');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
- "bb" must not contain consonants
|
|
@ -13,104 +13,67 @@ 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\ConsonantException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\AbstractFilterRule
|
||||
* @covers \Respect\Validation\Rules\Consonant
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Kleber Hamada Sato <kleberhs007@yahoo.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
* @author Pascal Borreli <pascal@borreli.com>
|
||||
*/
|
||||
final class ConsonantTest extends TestCase
|
||||
final class ConsonantTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForValidConsonants
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validDataWithConsonantsShouldReturnTrue(string $validConsonants): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$validator = new Consonant();
|
||||
self::assertTrue($validator->validate($validConsonants));
|
||||
}
|
||||
$consonant = new Consonant();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidConsonants
|
||||
* @expectedException \Respect\Validation\Exceptions\ConsonantException
|
||||
*
|
||||
* @test
|
||||
*
|
||||
* @param mixed $invalidConsonants
|
||||
*/
|
||||
public function invalidConsonantsShouldFailAndThrowConsonantException($invalidConsonants): void
|
||||
{
|
||||
$validator = new Consonant();
|
||||
self::assertFalse($validator->validate($invalidConsonants));
|
||||
$validator->assert($invalidConsonants);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerAdditionalChars
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function additionalCharsShouldBeRespected(string $additional, string $input): void
|
||||
{
|
||||
$validator = new Consonant($additional);
|
||||
self::assertTrue($validator->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function providerAdditionalChars(): array
|
||||
{
|
||||
return [
|
||||
['!@#$%^&*(){}', '!@#$%^&*(){} bc dfg'],
|
||||
['[]?+=/\\-_|"\',<>.', "[]?+=/\\-_|\"',<>. \t \n bc dfg"],
|
||||
'Letter "b"' => [$consonant, 'b'],
|
||||
'Letter "c"' => [$consonant, 'c'],
|
||||
'Letter "d"' => [$consonant, 'd'],
|
||||
'Letter "w"' => [$consonant, 'w'],
|
||||
'Letter "y"' => [$consonant, 'y'],
|
||||
'String "bcdfghklmnp"' => [$consonant, 'bcdfghklmnp'],
|
||||
'String with space in the middle "bcdfghklm np"' => [$consonant, 'bcdfghklm np'],
|
||||
'String "qrst"' => [$consonant, 'qrst'],
|
||||
'String with cntrl "\nz\t"' => [$consonant, "\nz\t"],
|
||||
'String "zbcxwyrspq"' => [$consonant, 'zbcxwyrspq'],
|
||||
'Ignoring characters "!@#$%^&*(){}"' => [new Consonant('!@#$%^&*(){}'), '!@#$%^&*(){} bc dfg'],
|
||||
'Ignoring characters "[]?+=/\\-_|"\',<>."' => [
|
||||
new Consonant('[]?+=/\\-_|"\',<>.'), "[]?+=/\\-_|\"',<>. \t \n bc dfg",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidConsonants(): array
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['b'],
|
||||
['c'],
|
||||
['d'],
|
||||
['w'],
|
||||
['y'],
|
||||
['y', ''],
|
||||
['bcdfghklmnp'],
|
||||
['bcdfghklm np'],
|
||||
['qrst'],
|
||||
["\nz\t"],
|
||||
['zbcxwyrspq'],
|
||||
];
|
||||
}
|
||||
$consonant = new Consonant();
|
||||
|
||||
/**
|
||||
* @return mixed[][]
|
||||
*/
|
||||
public function providerForInvalidConsonants(): array
|
||||
{
|
||||
return [
|
||||
[''],
|
||||
[null],
|
||||
['16'],
|
||||
['aeiou'],
|
||||
['a'],
|
||||
['Foo'],
|
||||
[-50],
|
||||
['basic'],
|
||||
'Parameter empty' => [$consonant, ''],
|
||||
'Letter "a"' => [$consonant, 'a'],
|
||||
'Parameter "null"' => [$consonant, null],
|
||||
'Number "16"' => [$consonant, '16'],
|
||||
'Alphabet "aeiou"' => [$consonant, 'aeiou'],
|
||||
'String "Foo"' => [$consonant, 'Foo'],
|
||||
'Negative integer "-50"' => [$consonant, -50],
|
||||
'String "basic"' => [$consonant, 'basic'],
|
||||
'Array empty' => [$consonant, []],
|
||||
'Integer' => [$consonant, 10],
|
||||
'Instance stdClass' => [$consonant, new stdClass()],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue