Browse Source

Validator classes tested

master
Anton Grebnev 13 years ago
parent
commit
152759cde6
  1. 27
      tests/validator/EmailValidatorTest.php
  2. 36
      tests/validator/EqualValidatorTest.php
  3. 37
      tests/validator/NotEmptyValidatorTest.php
  4. 76
      tests/validator/RegexValidatorTest.php

27
tests/validator/EmailValidatorTest.php

@ -0,0 +1,27 @@
<?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';
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'));
}
}

36
tests/validator/EqualValidatorTest.php

@ -0,0 +1,36 @@
<?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/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');
}
}

37
tests/validator/NotEmptyValidatorTest.php

@ -0,0 +1,37 @@
<?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/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));
}
}

76
tests/validator/RegexValidatorTest.php

@ -0,0 +1,76 @@
<?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'));
}
}
Loading…
Cancel
Save