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.

76 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. class RegexValidatorTest extends PHPUnit_Framework_TestCase
  15. {
  16. public function testIsValid()
  17. {
  18. $validator = new RegexValidator('/^[a-z]*$/i');
  19. $this->assertTrue($validator->isValid('anTon', array(1,2,3)));
  20. $this->assertFalse($validator->isValid('12ejkas,.21'));
  21. }
  22. public function testGetMessage()
  23. {
  24. $validator = new RegexValidator('/^[a-z0-9]*$/i');
  25. $this->assertTrue($validator->isValid('ton342ad21y'));
  26. $this->assertEmpty($validator->getMessage());
  27. $this->assertFalse($validator->isValid('!!#asd'));
  28. $this->assertNotEmpty($validator->getMessage());
  29. }
  30. public function testSetMessage()
  31. {
  32. $validator = new RegexValidator('/a/i');
  33. $validator->isValid('2sa131');
  34. $this->assertEmpty($validator->getMessage());
  35. $validator->setMessage('i am ok');
  36. $validator->isValid('2131');
  37. $this->assertEquals('i am ok', $validator->getMessage());
  38. }
  39. /**
  40. * @expectedException Exception
  41. * @expectedExceptionMessage Message template "regex_not_match" unknown.
  42. */
  43. public function testNullMessage()
  44. {
  45. $validator = new RegexValidator('/a/i');
  46. $validator->setMessage(null, null);
  47. $validator->isValid('1212');
  48. }
  49. /**
  50. * @TODO: RegexValidator - wrong regex throws an error. Check this.
  51. * @expectedException PHPUnit_Framework_Error
  52. */
  53. public function testWrongRegexp()
  54. {
  55. $validator = new RegexValidator('/^[a-z][0-9]$*/i');
  56. $this->assertFalse($validator->isValid('to423$%ny'));
  57. }
  58. /**
  59. * @expectedException Exception
  60. * @expectedExceptionMessage regex
  61. */
  62. public function testRegexReturnsFalse()
  63. {
  64. $validator = new RegexValidator('/(?:\D+|<\d+>)*[!?]/');
  65. $this->assertFalse($validator->isValid('foobar foobar foobar'));
  66. }
  67. }