From 152759cde6c5fe1a5066d76b3eb8d75996de190c Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Mon, 10 Oct 2011 14:24:25 +0400 Subject: [PATCH] Validator classes tested --- tests/validator/EmailValidatorTest.php | 27 +++++++++++ tests/validator/EqualValidatorTest.php | 36 +++++++++++++++ tests/validator/NotEmptyValidatorTest.php | 37 +++++++++++++++ tests/validator/RegexValidatorTest.php | 76 +++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 tests/validator/EmailValidatorTest.php create mode 100644 tests/validator/EqualValidatorTest.php create mode 100644 tests/validator/NotEmptyValidatorTest.php create mode 100644 tests/validator/RegexValidatorTest.php diff --git a/tests/validator/EmailValidatorTest.php b/tests/validator/EmailValidatorTest.php new file mode 100644 index 0000000..274ff5f --- /dev/null +++ b/tests/validator/EmailValidatorTest.php @@ -0,0 +1,27 @@ + + * @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'; +require_once dirname(__FILE__) . '/../../validator/EmailValidator.php'; + +class EmailValidatorTest extends PHPUnit_Framework_TestCase +{ + + public function testConstruct() + { + $validator = new EmailValidator(); + $this->assertTrue($validator->isValid('mail@mail.erw')); + $this->assertFalse($validator->isValid('asdasd')); + } +} \ No newline at end of file diff --git a/tests/validator/EqualValidatorTest.php b/tests/validator/EqualValidatorTest.php new file mode 100644 index 0000000..f3e033e --- /dev/null +++ b/tests/validator/EqualValidatorTest.php @@ -0,0 +1,36 @@ + + * @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/EqualValidator.php'; + +class EqualValidatorTest extends PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $validator = new EqualValidator('token'); + $this->assertFalse($validator->isValid('not token')); + $this->assertTrue($validator->isValid('token')); + } + + /** + * @expectedException Exception + */ + public function testNullToken() + { + $validator = new EqualValidator(null); + $validator->isValid('not token'); + } + +} + \ No newline at end of file diff --git a/tests/validator/NotEmptyValidatorTest.php b/tests/validator/NotEmptyValidatorTest.php new file mode 100644 index 0000000..4e677ef --- /dev/null +++ b/tests/validator/NotEmptyValidatorTest.php @@ -0,0 +1,37 @@ + + * @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/NotEmptyValidator.php'; + +class NotEmptyValidatorTest extends PHPUnit_Framework_TestCase +{ + public function testValidator() + { + $validator = new NotEmptyValidator(); + $this->assertFalse($validator->isValid('')); + $this->assertTrue($validator->isValid('token')); + $this->assertTrue($validator->isValid(1212)); + $this->assertTrue($validator->isValid(array(1,2,2))); + $this->assertFalse($validator->isValid(array())); + $this->assertNotEmpty($validator->getMessage()); + } + + public function testEmptyValue() + { + $validator = new NotEmptyValidator(null); + $this->assertFalse($validator->isValid(null)); + } + +} + \ No newline at end of file diff --git a/tests/validator/RegexValidatorTest.php b/tests/validator/RegexValidatorTest.php new file mode 100644 index 0000000..913c192 --- /dev/null +++ b/tests/validator/RegexValidatorTest.php @@ -0,0 +1,76 @@ + + * @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')); + } +} \ No newline at end of file