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.

77 lines
2.4 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. /**
  41. * @expectedException GeneralException
  42. * @expectedExceptionMessage Message template "regex_not_match" unknown.
  43. */
  44. public function testNullMessage()
  45. {
  46. $validator = new RegexValidator('/a/i');
  47. $validator->setMessage(null, null);
  48. $validator->isValid('1212');
  49. }
  50. /**
  51. * @TODO: RegexValidator - wrong regex throws an error. Check this.
  52. * @expectedException PHPUnit_Framework_Error
  53. */
  54. public function testWrongRegexp()
  55. {
  56. $validator = new RegexValidator('/^[a-z][0-9]$*/i');
  57. $this->assertFalse($validator->isValid('to423$%ny'));
  58. }
  59. /**
  60. * @expectedException GeneralException
  61. * @expectedExceptionMessage regex
  62. */
  63. public function testRegexReturnsFalse()
  64. {
  65. $validator = new RegexValidator('/(?:\D+|<\d+>)*[!?]/');
  66. $this->assertFalse($validator->isValid('foobar foobar foobar'));
  67. }
  68. }