You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?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';
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->assertEquals('i am ok', $validator->getMessage()); } /** * @expectedException Exception */ public function testNullMessage() { $validator = new RegexValidator('/a/i'); $validator->setMessage(null, null); $validator->isValid('1212'); } /** * @TODO: RegexValidator - wrong regex throws an error. Check this. * @expectedException PHPUnit_Framework_Error */ public function testWrongRegexp() { $validator = new RegexValidator('/^[a-z][0-9]$*/i'); $this->assertFalse($validator->isValid('to423$%ny')); } /** * @expectedException Exception * @expectedExceptionMessage regex */ public function testRegexReturnsFalse() { $validator = new RegexValidator('/(?:\D+|<\d+>)*[!?]/'); $this->assertFalse($validator->isValid('foobar foobar foobar')); } }
|