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.

72 lines
2.3 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-07
  8. *
  9. * Unit tests for RegexValdator class
  10. */
  11. require_once dirname(__FILE__) . '/../../validator/iValidator.php';
  12. require_once dirname(__FILE__) . '/../../validator/Validator.php';
  13. require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
  14. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  15. class RegexValidatorTest extends PHPUnit_Framework_TestCase
  16. {
  17. public function testIsValid()
  18. {
  19. $validator = new RegexValidator('/^[a-z]*$/i');
  20. $this->assertTrue($validator->isValid('anTon', array(1,2,3)));
  21. $this->assertFalse($validator->isValid('12ejkas,.21'));
  22. }
  23. public function testGetMessage()
  24. {
  25. $validator = new RegexValidator('/^[a-z0-9]*$/i');
  26. $this->assertTrue($validator->isValid('ton342ad21y'));
  27. $this->assertEmpty($validator->getMessage());
  28. $this->assertFalse($validator->isValid('!!#asd'));
  29. $this->assertNotEmpty($validator->getMessage());
  30. }
  31. public function testSetMessage()
  32. {
  33. $validator = new RegexValidator('/a/i');
  34. $validator->isValid('2sa131');
  35. $this->assertEmpty($validator->getMessage());
  36. $validator->setMessage('i am ok');
  37. $validator->isValid('2131');
  38. $this->assertEquals('i am ok', $validator->getMessage());
  39. }
  40. public function testNullMessage()
  41. {
  42. $this->setExpectedException('GeneralException', 'Message template "regex_not_match" unknown.');
  43. $validator = new RegexValidator('/a/i');
  44. $validator->setMessage(null, null);
  45. $validator->isValid('1212');
  46. }
  47. /**
  48. * @TODO: RegexValidator - wrong regex throws an error. Check this.
  49. */
  50. public function testWrongRegexp()
  51. {
  52. $this->setExpectedException('PHPUnit_Framework_Error');
  53. $validator = new RegexValidator('/^[a-z][0-9]$*/i');
  54. $this->assertFalse($validator->isValid('to423$%ny'));
  55. }
  56. public function testRegexReturnsFalse()
  57. {
  58. $this->setExpectedException('GeneralException', 'regex');
  59. $validator = new RegexValidator('/(?:\D+|<\d+>)*[!?]/');
  60. $this->assertFalse($validator->isValid('foobar foobar foobar'));
  61. }
  62. }