mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Better tests, parameter validation for Alnum and Alpha
This commit is contained in:
parent
f18d700b9f
commit
c29295a4d0
|
@ -4,26 +4,35 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotAlphanumericException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Alnum extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_ALPHANUMERIC = 'Alnum_1';
|
||||
const MSG_NOT_ALPHANUMERIC_ADDITIONAL = 'Alnum_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_ALPHANUMERIC => '%s does not contains only letters and digits',
|
||||
self::MSG_NOT_ALPHANUMERIC_ADDITIONAL => '%s does not contains only letters and digits (including %s)'
|
||||
self::MSG_NOT_ALPHANUMERIC => '"%s" does not contains only letters and digits',
|
||||
self::MSG_NOT_ALPHANUMERIC_ADDITIONAL => '"%s" does not contains only letters and digits (including "%s")'
|
||||
);
|
||||
protected $additionalChars = '';
|
||||
|
||||
public function __construct($additionalChars='')
|
||||
{
|
||||
if (!is_string($additionalChars))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s" is not a valid list of additional characters to be loaded',
|
||||
$this->getStringRepresentation($additionalChars)
|
||||
)
|
||||
);
|
||||
$this->additionalChars = $additionalChars;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return (boolean) preg_match(
|
||||
"#^[a-zA-Z0-9{$this->additionalChars}]+$#", $input
|
||||
return is_string($input) && preg_match(
|
||||
"#^[a-zA-Z0-9]+$#",
|
||||
str_replace(str_split($this->additionalChars), '', $input)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -32,14 +41,17 @@ class Alnum extends AbstractRule
|
|||
if (!$this->validate($input))
|
||||
if (empty($this->additionalChars))
|
||||
throw new NotAlphanumericException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_ALPHANUMERIC),
|
||||
$input)
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHANUMERIC),
|
||||
$this->getStringRepresentation($input)
|
||||
)
|
||||
);
|
||||
else
|
||||
throw new NotAlphanumericException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHANUMERIC_ADDITIONAL),
|
||||
$input, $this->additionalChars
|
||||
$this->getStringRepresentation($input),
|
||||
$this->additionalChars
|
||||
)
|
||||
);
|
||||
return true;
|
||||
|
|
|
@ -4,26 +4,35 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotAlphaException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Alpha extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_ALPHA = 'Alpha_1';
|
||||
const MSG_NOT_ALPHA_ADDITIONAL = 'Alpha_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_ALPHA => '%s does not contains only letters',
|
||||
self::MSG_NOT_ALPHA_ADDITIONAL => '%s does not contains only letters (including %s)'
|
||||
self::MSG_NOT_ALPHA => '"%s" does not contains only letters',
|
||||
self::MSG_NOT_ALPHA_ADDITIONAL => '"%s" does not contains only letters (including "%s")'
|
||||
);
|
||||
protected $additionalChars = '';
|
||||
|
||||
public function __construct($additionalChars='')
|
||||
{
|
||||
if (!is_string($additionalChars))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s" is not a valid list of additional characters to be loaded',
|
||||
$this->getStringRepresentation($additionalChars)
|
||||
)
|
||||
);
|
||||
$this->additionalChars = $additionalChars;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return (boolean) preg_match(
|
||||
"#^[a-zA-Z{$this->additionalChars}]+$#", $input
|
||||
return is_string($input) && preg_match(
|
||||
"#^[a-zA-Z]+$#",
|
||||
str_replace(str_split($this->additionalChars), '', $input)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -32,14 +41,17 @@ class Alpha extends AbstractRule
|
|||
if (!$this->validate($input))
|
||||
if (empty($this->additionalChars))
|
||||
throw new NotAlphaException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_ALPHA),
|
||||
$input)
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHA),
|
||||
$this->getStringRepresentation($input)
|
||||
)
|
||||
);
|
||||
else
|
||||
throw new NotAlphaException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHA_ADDITIONAL),
|
||||
$input, $this->additionalChars
|
||||
$this->getStringRepresentation($input),
|
||||
$this->additionalChars
|
||||
)
|
||||
);
|
||||
return true;
|
||||
|
|
|
@ -24,10 +24,29 @@ class AlnumTest extends \PHPUnit_Framework_TestCase
|
|||
$validator->assert($invalidAlnum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters($aditional)
|
||||
{
|
||||
$validator = new Alnum($aditional);
|
||||
}
|
||||
|
||||
public function providerForInvalidParams()
|
||||
{
|
||||
return array(
|
||||
array(new \stdClass),
|
||||
array(array()),
|
||||
array(0x2)
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForValidAlnum()
|
||||
{
|
||||
return array(
|
||||
array('alganet', ''),
|
||||
array('0alg-anet0', '0-9'),
|
||||
array('1', ''),
|
||||
array('a', ''),
|
||||
array('foobar', ''),
|
||||
|
@ -44,6 +63,11 @@ class AlnumTest extends \PHPUnit_Framework_TestCase
|
|||
array('', ''),
|
||||
array('dgç', ''),
|
||||
array('alganet alganet', ''),
|
||||
array(1e21, ''),
|
||||
array(0, ''),
|
||||
array(null, ''),
|
||||
array(new \stdClass, ''),
|
||||
array(array(), ''),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,15 +24,33 @@ class AlphaTest extends \PHPUnit_Framework_TestCase
|
|||
$validator->assert($invalidAlpha);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters($aditional)
|
||||
{
|
||||
$validator = new Alpha($aditional);
|
||||
}
|
||||
|
||||
public function providerForInvalidParams()
|
||||
{
|
||||
return array(
|
||||
array(new \stdClass),
|
||||
array(array()),
|
||||
array(0x2)
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForValidAlpha()
|
||||
{
|
||||
return array(
|
||||
array('alganet', ''),
|
||||
array('0alg-anet0', '0-9'),
|
||||
array('a', ''),
|
||||
array('foobar', ''),
|
||||
array('rubinho_', '_'),
|
||||
array('google.com', '.'),
|
||||
array('al ganet', ' '),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -43,10 +61,15 @@ class AlphaTest extends \PHPUnit_Framework_TestCase
|
|||
array('_', ''),
|
||||
array('', ''),
|
||||
array('dgç', ''),
|
||||
array('1abc', ''),
|
||||
array('122al', ''),
|
||||
array('122', ''),
|
||||
array(11123, ''),
|
||||
array('alganet alganet', ''),
|
||||
array('123', ''),
|
||||
array(123, ''),
|
||||
array(1e21, ''),
|
||||
array(0, ''),
|
||||
array(null, ''),
|
||||
array(new \stdClass, ''),
|
||||
array(array(), ''),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue