mirror of
https://github.com/Respect/Validation.git
synced 2024-06-09 01:02:16 +02:00
Apply contribution guidelines to "Vowel" rule
After writing the integration tests of the "Vowel" rule we noticed that it is generating extra double-quotes for the "{{additionalChars}}" template placeholder. Besides, it considers any whitespace as valid. That is because in the past Validation would always consider spaces as a valid. This commit will also remove the extra double-quotes and do not allow whitespaces to be considered as valid. Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
18bdd399d7
commit
b98a7ac89b
|
@ -3,7 +3,7 @@
|
|||
- `Vowel()`
|
||||
- `Vowel(string ...$additionalChars)`
|
||||
|
||||
Similar to `Alnum()`. Validates strings that contains only vowels:
|
||||
Validates whether the input contains only vowels.
|
||||
|
||||
```php
|
||||
v::vowel()->validate('aei'); // true
|
||||
|
@ -13,6 +13,7 @@ v::vowel()->validate('aei'); // true
|
|||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Do not consider whitespaces as valid
|
||||
0.5.0 | Renamed from `Vowels` to `Vowel`
|
||||
0.3.9 | Created as `Vowels`
|
||||
|
||||
|
|
|
@ -25,11 +25,11 @@ final class VowelException extends FilteredValidationException
|
|||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must contain only vowels',
|
||||
self::EXTRA => '{{name}} must contain only vowels and "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must contain only vowels and {{additionalChars}}',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not contain vowels',
|
||||
self::EXTRA => '{{name}} must not contain vowels or "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must not contain vowels or {{additionalChars}}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -16,13 +16,18 @@ namespace Respect\Validation\Rules;
|
|||
use function preg_match;
|
||||
|
||||
/**
|
||||
* Validates whether the input contains only vowels.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
*/
|
||||
final class Vowel extends AbstractFilterRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function validateFilteredInput(string $input): bool
|
||||
{
|
||||
return preg_match('/^(\s|[aeiouAEIOU])*$/', $input) > 0;
|
||||
return preg_match('/^[aeiouAEIOU]+$/', $input) > 0;
|
||||
}
|
||||
}
|
||||
|
|
71
tests/integration/rules/vowel.phpt
Normal file
71
tests/integration/rules/vowel.phpt
Normal file
|
@ -0,0 +1,71 @@
|
|||
--CREDITS--
|
||||
Danilo Correa <danilosilva87@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\VowelException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::vowel()->check('b');
|
||||
} catch (VowelException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::vowel('c')->check('d');
|
||||
} catch (VowelException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::vowel())->check('a');
|
||||
} catch (VowelException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::vowel('f'))->check('e');
|
||||
} catch (VowelException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::vowel()->assert('g');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::vowel('h')->assert('j');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::vowel())->assert('i');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::vowel('k'))->assert('o');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
"b" must contain only vowels
|
||||
"d" must contain only vowels and "c"
|
||||
"a" must not contain vowels
|
||||
"e" must not contain vowels or "f"
|
||||
- "g" must contain only vowels
|
||||
- "j" must contain only vowels and "h"
|
||||
- "i" must not contain vowels
|
||||
- "o" must not contain vowels or "k"
|
|
@ -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\VowelException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\AbstractFilterRule
|
||||
* @covers \Respect\Validation\Rules\Vowel
|
||||
*
|
||||
|
@ -27,88 +27,48 @@ use Respect\Validation\Test\TestCase;
|
|||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
* @author Pascal Borreli <pascal@borreli.com>
|
||||
*/
|
||||
final class VowelTest extends TestCase
|
||||
final class VowelTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForValidVowels
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validDataWithVowelsShouldReturnTrue(string $validVowels): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$validator = new Vowel();
|
||||
self::assertTrue($validator->validate($validVowels));
|
||||
}
|
||||
$sut = new Vowel();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidVowels
|
||||
* @expectedException \Respect\Validation\Exceptions\VowelException
|
||||
*
|
||||
* @test
|
||||
*
|
||||
* @param mixed $invalidVowels
|
||||
*/
|
||||
public function invalidVowelsShouldFailAndThrowVowelException($invalidVowels): void
|
||||
{
|
||||
$validator = new Vowel();
|
||||
self::assertFalse($validator->validate($invalidVowels));
|
||||
$validator->assert($invalidVowels);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerAdditionalChars
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function additionalCharsShouldBeRespected(string $additional, string $input): void
|
||||
{
|
||||
$validator = new Vowel($additional);
|
||||
self::assertTrue($validator->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function providerAdditionalChars(): array
|
||||
{
|
||||
return [
|
||||
['!@#$%^&*(){}', '!@#$%^&*(){} aeo iu'],
|
||||
['[]?+=/\\-_|"\',<>.', "[]?+=/\\-_|\"',<>. \t \n aeo iu"],
|
||||
[$sut, 'a'],
|
||||
[$sut, 'e'],
|
||||
[$sut, 'i'],
|
||||
[$sut, 'o'],
|
||||
[$sut, 'u'],
|
||||
[$sut, 'aeiou'],
|
||||
[$sut, 'uoiea'],
|
||||
[new Vowel('!@#$%^&*(){}'), '!@#$%^&*(){}aeoiu'],
|
||||
[new Vowel('[]?+=/\\-_|"\',<>.'), '[]?+=/\\-_|"\',<>.aeoiu'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidVowels(): array
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['a'],
|
||||
['e'],
|
||||
['i'],
|
||||
['o'],
|
||||
['u'],
|
||||
['aeiou'],
|
||||
['aei ou'],
|
||||
["\na\t"],
|
||||
['uoiea'],
|
||||
];
|
||||
}
|
||||
$sut = new Vowel();
|
||||
|
||||
/**
|
||||
* @return mixed[][]
|
||||
*/
|
||||
public function providerForInvalidVowels(): array
|
||||
{
|
||||
return [
|
||||
[''],
|
||||
[null],
|
||||
['16'],
|
||||
['F'],
|
||||
['g'],
|
||||
['Foo'],
|
||||
[-50],
|
||||
['basic'],
|
||||
[$sut, ''],
|
||||
[$sut, ' '],
|
||||
[$sut, "\n"],
|
||||
[$sut, "\t"],
|
||||
[$sut, "\r"],
|
||||
[$sut, null],
|
||||
[$sut, '16'],
|
||||
[$sut, 'F'],
|
||||
[$sut, 'g'],
|
||||
[$sut, 'Foo'],
|
||||
[$sut, -50],
|
||||
[$sut, 'basic'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue