Files
majestic/tests/validator/RegexValidatorTest.php

75 lines
2.3 KiB
PHP
Raw Normal View History

2011-10-10 14:24:25 +04:00
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-07
*
* Unit tests for RegexValdator class
*/
require_once dirname(__FILE__) . '/../../validator/iValidator.php';
require_once dirname(__FILE__) . '/../../validator/Validator.php';
require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
2011-11-25 14:12:38 +04:00
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
2011-10-10 14:24:25 +04:00
class RegexValidatorTest extends PHPUnit_Framework_TestCase
{
public function testIsValid()
{
$validator = new RegexValidator('/^[a-z]*$/i');
$this->assertTrue($validator->isValid('anTon', array(1,2,3)));
$this->assertFalse($validator->isValid('12ejkas,.21'));
}
public function testGetMessage()
{
$validator = new RegexValidator('/^[a-z0-9]*$/i');
$this->assertTrue($validator->isValid('ton342ad21y'));
$this->assertEmpty($validator->getMessage());
$this->assertFalse($validator->isValid('!!#asd'));
$this->assertNotEmpty($validator->getMessage());
}
public function testSetMessage()
{
$validator = new RegexValidator('/a/i');
$validator->isValid('2sa131');
$this->assertEmpty($validator->getMessage());
$validator->setMessage('i am ok');
$validator->isValid('2131');
$this->assertSame('i am ok', $validator->getMessage());
2011-10-10 14:24:25 +04:00
}
2011-10-10 14:24:25 +04:00
public function testNullMessage()
{
$validator = new RegexValidator('/a/i');
$validator->setMessage(null, null);
2011-12-02 12:25:47 +04:00
$this->setExpectedException('GeneralException', 'Message template "regex_not_match" unknown.');
2011-10-10 14:24:25 +04:00
$validator->isValid('1212');
}
/**
* @TODO: RegexValidator - wrong regex throws an error. Check this.
*/
public function testWrongRegexp()
{
$validator = new RegexValidator('/^[a-z][0-9]$*/i');
2011-12-02 12:25:47 +04:00
$this->setExpectedException('PHPUnit_Framework_Error');
2011-10-10 14:24:25 +04:00
$this->assertFalse($validator->isValid('to423$%ny'));
}
public function testRegexReturnsFalse()
{
2011-10-10 14:24:25 +04:00
$validator = new RegexValidator('/(?:\D+|<\d+>)*[!?]/');
2011-12-02 12:25:47 +04:00
$this->setExpectedException('GeneralException', 'regex');
2011-10-10 14:24:25 +04:00
$this->assertFalse($validator->isValid('foobar foobar foobar'));
}
}