Apply contribution guidelines to "Graph" rule

After writing the integration tests of the "Graph" 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-09 13:03:58 -02:00 committed by Henrique Moody
parent ffc349cb03
commit fc433ad449
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 116 additions and 71 deletions

View file

@ -3,7 +3,8 @@
- `Graph()`
- `Graph(string ...$additionalChars)`
Validates all characters that are graphically represented.
Validates if all characters in the input are printable and actually creates
visible output (no white space).
```php
v::graph()->validate('LKM@#$%4;'); // 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 GraphException extends FilteredValidationException
final class GraphException extends FilteredValidationException
{
/**
* {@inheritdoc}
@ -25,11 +26,11 @@ class GraphException extends FilteredValidationException
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must contain only graphical characters',
self::EXTRA => '{{name}} must contain only graphical characters and "{{additionalChars}}"',
self::EXTRA => '{{name}} must contain only graphical characters and {{additionalChars}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not contain graphical characters',
self::EXTRA => '{{name}} must not contain graphical characters or "{{additionalChars}}"',
self::EXTRA => '{{name}} must not contain graphical characters or {{additionalChars}}',
],
];
}

View file

@ -13,13 +13,21 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function ctype_graph;
/**
* Validates if all characters in the input are printable and actually creates visible output (no white space).
*
* @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 Graph extends AbstractFilterRule
final class Graph extends AbstractFilterRule
{
/**
* {@inheritdoc}
*/
protected function validateFilteredInput(string $input): bool
{
return ctype_graph($input);

View 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\GraphException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::graph()->check("foo\nbar");
} catch (GraphException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::graph('foo')->check("foo\nbar");
} catch (GraphException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::graph())->check("foobar");
} catch (GraphException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::graph("\n"))->check("foo\nbar");
} catch (GraphException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::graph()->assert("foo\nbar");
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::graph('foo')->assert("foo\nbar");
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::graph())->assert("foobar");
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::graph("\n"))->assert("foo\nbar");
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
"foo\nbar" must contain only graphical characters
"foo\nbar" must contain only graphical characters and "foo"
"foobar" must not contain graphical characters
"foo\nbar" must not contain graphical characters or "\n"
- "foo\nbar" must contain only graphical characters
- "foo\nbar" must contain only graphical characters and "foo"
- "foobar" must not contain graphical characters
- "foo\nbar" must not contain graphical characters or "\n"

View file

@ -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\GraphException
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractFilterRule
* @covers \Respect\Validation\Rules\Graph
*
@ -27,82 +27,46 @@ use Respect\Validation\Test\TestCase;
* @author Nick Lombard <github@jigsoft.co.za>
* @author Pascal Borreli <pascal@borreli.com>
*/
final class GraphTest extends TestCase
final class GraphTest extends RuleTestCase
{
/**
* @dataProvider providerForValidGraph
*
* @test
* @inheritdoc
*/
public function validDataWithGraphCharsShouldReturnTrue(string $validGraph): void
{
$validator = new Graph();
self::assertTrue($validator->validate($validGraph));
}
/**
* @dataProvider providerForInvalidGraph
* @expectedException \Respect\Validation\Exceptions\GraphException
*
* @test
*
* @param mixed $invalidGraph
*/
public function invalidGraphShouldFailAndThrowGraphException($invalidGraph): void
{
$validator = new Graph();
self::assertFalse($validator->validate($invalidGraph));
$validator->assert($invalidGraph);
}
/**
* @dataProvider providerAdditionalChars
*
* @test
*/
public function additionalCharsShouldBeRespected(string $additional, string $input): void
{
$validator = new Graph($additional);
self::assertTrue($validator->validate($input));
}
/**
* @return string[][]
*/
public function providerAdditionalChars(): array
public function providerForValidInput(): array
{
$graph = new Graph();
return [
[' ', '!@#$%^&*(){} abc 123'],
[" \t\n", "[]?+=/\\-_|\"',<>. \t \n abc 123"],
'String with special characters "LKA#@%.54"' => [$graph, 'LKA#@%.54'],
'String "foobar"' => [$graph, 'foobar'],
'String 16-50' => [$graph, '16-50'],
'String 123' => [$graph, '123'],
'String with special characters "#$%&*_"' => [$graph, '#$%&*_'],
'Ignoring control characters "\n"' => [new Graph("\n"), "#$%&*_\n~"],
'Ignoring control characters "\n#\t&\r"' => [new Graph("\n#\t&\r"), "#$%&*_\n~\t**\r"],
'Ignoring character "_"' => [new Graph("_"), "abc\#$%&*_"],
'Ignoring characters "# $"' => [new Graph("# $"), "#$%&*_"],
'Ignoring character with space' => [new Graph(' '), '!@#$%^&*(){} abc 123'],
'Ignoring control characters " \t\n"' => [new Graph(" \t\n"), "[]?+=/\\-_|\"',<>. \t \n abc 123"],
];
}
/**
* @return string[][]
* {@inheritdoc}
*/
public function providerForValidGraph(): array
public function providerForInvalidInput(): array
{
return [
['LKA#@%.54'],
['foobar'],
['16-50'],
['123'],
['#$%&*_'],
];
}
$graph = new Graph();
/**
* @return mixed[][]
*/
public function providerForInvalidGraph(): array
{
return [
[''],
[null],
["foo\nbar"],
["foo\tbar"],
['foo bar'],
[' '],
'String empty' => [$graph, ''],
'Parameter null' => [$graph, null],
'String with "\n"' => [$graph, "foo\nbar"],
'String with "\t"' => [$graph, "foo\tbar"],
'String with "foo bar"' => [$graph, 'foo bar'],
'String with space' => [$graph, ' '],
'Igonring space' => [new Graph(' '), "@__§¬¬¬\n"],
'Ignoring control characters "foo\nbar"' => [new Graph("foo\nbar"), "foo\nbar\ree"],
];
}
}