mirror of
https://github.com/Respect/Validation.git
synced 2024-06-15 20:15:06 +02:00
New rule Graphical based on ctype_graph
This commit is contained in:
parent
a643f6f125
commit
86f7a6bc26
19
library/Respect/Validation/Exceptions/GraphicalException.php
Normal file
19
library/Respect/Validation/Exceptions/GraphicalException.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class GraphicalException extends AlphaException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must contain only graphical characters',
|
||||
self::EXTRA => '{{name}} must contain only graphical characters and "{{additionalChars}}"'
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not contain graphical characters',
|
||||
self::EXTRA => '{{name}} must not contain graphical characters or "{{additionalChars}}"'
|
||||
)
|
||||
);
|
||||
|
||||
}
|
11
library/Respect/Validation/Rules/Graphical.php
Normal file
11
library/Respect/Validation/Rules/Graphical.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Graphical extends AbstractCtypeRule
|
||||
{
|
||||
|
||||
public $additionalChars = '';
|
||||
protected $ctypeFunc = 'ctype_graph';
|
||||
|
||||
}
|
67
tests/library/Respect/Validation/Rules/GraphicalTest.php
Normal file
67
tests/library/Respect/Validation/Rules/GraphicalTest.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class GraphicalTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidGraphical
|
||||
*/
|
||||
public function testValidDataWithGraphicalCharsShouldReturnTrue($validGraphical, $aditional='')
|
||||
{
|
||||
$validator = new Graphical($aditional);
|
||||
$this->assertTrue($validator->validate($validGraphical));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidGraphical
|
||||
* @expectedException Respect\Validation\Exceptions\GraphicalException
|
||||
*/
|
||||
public function testInvalidGraphicalShouldFailAndThrowGraphicalException($invalidGraphical, $aditional='')
|
||||
{
|
||||
$validator = new Graphical($aditional);
|
||||
$this->assertFalse($validator->validate($invalidGraphical));
|
||||
$this->assertFalse($validator->assert($invalidGraphical));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($aditional)
|
||||
{
|
||||
$validator = new Graphical($aditional);
|
||||
}
|
||||
|
||||
public function providerForInvalidParams()
|
||||
{
|
||||
return array(
|
||||
array(new \stdClass),
|
||||
array(array()),
|
||||
array(0x2)
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForValidGraphical()
|
||||
{
|
||||
return array(
|
||||
array('LKA#@%.54'),
|
||||
array('foobar'),
|
||||
array('16-50'),
|
||||
array('foobar'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidGraphical()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array(''),
|
||||
array("foo\nbar"),
|
||||
array("foo\tbar"),
|
||||
array('foo bar'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue