New rule Graphical based on ctype_graph

This commit is contained in:
Andre Ramaciotti 2013-01-20 17:27:27 -02:00 committed by nickl-
parent a643f6f125
commit 86f7a6bc26
3 changed files with 97 additions and 0 deletions

View 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}}"'
)
);
}

View file

@ -0,0 +1,11 @@
<?php
namespace Respect\Validation\Rules;
class Graphical extends AbstractCtypeRule
{
public $additionalChars = '';
protected $ctypeFunc = 'ctype_graph';
}

View 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'),
);
}
}