Apply contribution guidelines to "Punct" rule

After writing the integration tests of the "Punct" 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-02-10 20:39:07 -02:00 committed by Henrique Moody
parent e62d4e145d
commit 540c0a4a17
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 110 additions and 72 deletions

View file

@ -3,7 +3,7 @@
- `Punct()`
- `Punct(string ...$additionalChars)`
Accepts only punctuation characters:
Validates whether the input composed by only punctuation characters.
```php
v::punct()->validate('&,.;[]'); // true

View file

@ -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 PunctException extends FilteredValidationException
final class PunctException extends FilteredValidationException
{
/**
* {@inheritdoc}
@ -25,11 +26,11 @@ class PunctException extends FilteredValidationException
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must contain only punctuation characters',
self::EXTRA => '{{name}} must contain only punctuation characters and "{{additionalChars}}"',
self::EXTRA => '{{name}} must contain only punctuation characters and {{additionalChars}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not contain punctuation characters',
self::EXTRA => '{{name}} must not contain punctuation characters or "{{additionalChars}}"',
self::EXTRA => '{{name}} must not contain punctuation characters or {{additionalChars}}',
],
];
}

View file

@ -13,13 +13,21 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function ctype_punct;
/**
* Validates whether the input composed by only punctuation 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 Punct extends AbstractFilterRule
final class Punct extends AbstractFilterRule
{
/**
* {@inheritdoc}
*/
protected function validateFilteredInput(string $input): bool
{
return ctype_punct($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\PunctException;
use Respect\Validation\Validator as v;
try {
v::punct()->check('a');
} catch (PunctException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::punct('c')->check('b');
} catch (PunctException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::punct())->check('.');
} catch (PunctException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::punct('d'))->check('?');
} catch (PunctException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::punct()->assert('e');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::punct('f')->assert('g');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::punct())->assert('!');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::punct('h'))->assert(';');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
"a" must contain only punctuation characters
"b" must contain only punctuation characters and "c"
"." must not contain punctuation characters
"?" must not contain punctuation characters or "d"
- "e" must contain only punctuation characters
- "g" must contain only punctuation characters and "f"
- "!" must not contain punctuation characters
- ";" must not contain punctuation characters or "h"

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\PunctException
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Punct
*
* @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 PunctTest extends TestCase
final class PunctTest extends RuleTestCase
{
/**
* @dataProvider providerForValidPunct
*
* @test
* {@inheritdoc}
*/
public function validDataWithPunctShouldReturnTrue(string $validPunct): void
public function providerForValidInput(): array
{
$validator = new Punct();
self::assertTrue($validator->validate($validPunct));
}
$sut = new Punct();
/**
* @dataProvider providerForInvalidPunct
* @expectedException \Respect\Validation\Exceptions\PunctException
*
* @test
*
* @param mixed $invalidPunct
*/
public function invalidPunctShouldFailAndThrowPunctException($invalidPunct): void
{
$validator = new Punct();
self::assertFalse($validator->validate($invalidPunct));
$validator->assert($invalidPunct);
}
/**
* @dataProvider providerAdditionalChars
*
* @test
*/
public function additionalCharsShouldBeRespected(string $additional, string $input): void
{
$validator = new Punct($additional);
self::assertTrue($validator->validate($input));
}
/**
* @return string[][]
*/
public function providerAdditionalChars(): array
{
return [
['abc123 ', '!@#$%^&*(){} abc 123'],
["abc123 \t\n", "[]?+=/\\-_|\"',<>. \t \n abc 123"],
[$sut, '.'],
[$sut, ',;:'],
[$sut, '-@#$*'],
[$sut, '()[]{}'],
[new Punct('abc123 '), '!@#$%^&*(){} abc 123'],
[new Punct("abc123 \t\n"), "[]?+=/\\-_|\"',<>. \t \n abc 123"],
];
}
/**
* @return string[][]
* {@inheritdoc}
*/
public function providerForValidPunct(): array
public function providerForInvalidInput(): array
{
return [
['.'],
[',;:'],
['-@#$*'],
['()[]{}'],
];
}
$sut = new Punct();
/**
* @return mixed[][]
*/
public function providerForInvalidPunct(): array
{
return [
[''],
['16-50'],
['a'],
[' '],
['Foo'],
['12.1'],
['-12'],
[-12],
['( )_{}'],
[$sut, ''],
[$sut, '16-50'],
[$sut, 'a'],
[$sut, ' '],
[$sut, 'Foo'],
[$sut, '12.1'],
[$sut, '-12'],
[$sut, -12],
[$sut, '( )_{}'],
];
}
}