Apply contribution guidelines to "Space" rule

After writing the integration tests of the "Space" 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:
Danilo Correa 2019-03-03 18:33:10 -03:00 committed by Henrique Moody
parent b340b74cfd
commit d2c4912582
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 105 additions and 70 deletions

View file

@ -3,7 +3,7 @@
- `Space()`
- `Space(string ...$additionalChars)`
Accepts only whitespace:
Validates whether the input contains only whitespaces characters.
```php
v::space()->validate(' '); // true

View file

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

View file

@ -16,12 +16,18 @@ namespace Respect\Validation\Rules;
use function ctype_space;
/**
* Validates whether the input contains only whitespaces 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>
*/
final class Space extends AbstractFilterRule
{
/**
* {@inheritdoc}
*/
protected function validateFilteredInput(string $input): bool
{
return ctype_space($input);

View file

@ -0,0 +1,70 @@
--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\SpaceException;
use Respect\Validation\Validator as v;
try {
v::space()->check('ab');
} catch (SpaceException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::space('c')->check('cd');
} catch (SpaceException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::space())->check("\t");
} catch (SpaceException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::space('def'))->check("\r");
} catch (SpaceException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::space()->assert('ef');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::space('e')->assert('gh');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::space())->assert("\n");
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::space('yk'))->assert(' k');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
"ab" must contain only space characters
"cd" must contain only space characters and "c"
"\t" must not contain space characters
"\r" must not contain space characters or "def"
- "ef" must contain only space characters
- "gh" must contain only space characters and "e"
- "\n" must not contain space characters
- " k" must not contain space characters or "yk"

View file

@ -13,98 +13,57 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\SpaceException
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Space
*
* @author Andre Ramaciotti <andre@ramaciotti.com>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
* @author Pascal Borreli <pascal@borreli.com>
*/
final class SpaceTest extends TestCase
final class SpaceTest extends RuleTestCase
{
/**
* @dataProvider providerForValidSpace
*
* @test
* {@inheritdoc}
*/
public function validDataWithSpaceShouldReturnTrue(string $validSpace): void
public function providerForValidInput(): array
{
$validator = new Space();
self::assertTrue($validator->validate($validSpace));
}
$sut = new Space();
/**
* @dataProvider providerForInvalidSpace
* @expectedException \Respect\Validation\Exceptions\SpaceException
*
* @test
*
* @param mixed $invalidSpace
*/
public function invalidSpaceShouldFailAndThrowSpaceException($invalidSpace): void
{
$validator = new Space();
self::assertFalse($validator->validate($invalidSpace));
$validator->assert($invalidSpace);
}
/**
* @dataProvider providerAdditionalChars
*
* @test
*/
public function additionalCharsShouldBeRespected(string $additional, string $input): void
{
$validator = new Space($additional);
self::assertTrue($validator->validate($input));
}
/**
* @return string[][]
*/
public function providerAdditionalChars(): array
{
return [
['!@#$%^&*(){}', '!@#$%^&*(){} '],
['[]?+=/\\-_|"\',<>.', "[]?+=/\\-_|\"',<>. \t \n "],
'new line' => [$sut, "\n"],
'1 space' => [$sut, ' '],
'4 spaces' => [$sut, ' '],
'tab' => [$sut, "\t"],
'2 spaces' => [$sut, ' '],
'characters "!@#$%^&*(){} "' => [new Space('!@#$%^&*(){}'), '!@#$%^&*(){} '],
'characters "[]?+=/\\-_|\"\',<>. \t \n "' => [new Space('[]?+=/\\-_|"\',<>.'), "[]?+=/\\-_|\"',<>. \t \n "],
];
}
/**
* @return string[][]
* {@inheritdoc}
*/
public function providerForValidSpace(): array
public function providerForInvalidInput(): array
{
return [
["\n"],
[' '],
[' '],
["\t"],
[' '],
];
}
$sut = new Space();
/**
* @return mixed[][]
*/
public function providerForInvalidSpace(): array
{
return [
[''],
['16-50'],
['a'],
['Foo'],
['12.1'],
['-12'],
[-12],
['_'],
'string empty' => [$sut, ''],
'string 16-56' => [$sut, '16-50'],
'string a' => [$sut, 'a'],
'string Foo' => [$sut, 'Foo'],
'string negative float' => [$sut, '12.1'],
'string negative number' => [$sut, '-12'],
'negative number ' => [$sut, -12],
'underline' => [$sut, '_'],
];
}
}