RegexValidatorTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\Regex;
  4. use Symfony\Component\Validator\Constraints\RegexValidator;
  5. class RegexValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. public function setUp()
  9. {
  10. $this->validator = new RegexValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new Regex(array('pattern' => '/^[0-9]+$/'))));
  15. }
  16. public function testExpectsStringCompatibleType()
  17. {
  18. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  19. $this->validator->isValid(new \stdClass(), new Regex(array('pattern' => '/^[0-9]+$/')));
  20. }
  21. /**
  22. * @dataProvider getValidValues
  23. */
  24. public function testValidValues($value)
  25. {
  26. $constraint = new Regex(array('pattern' => '/^[0-9]+$/'));
  27. $this->assertTrue($this->validator->isValid($value, $constraint));
  28. }
  29. public function getValidValues()
  30. {
  31. return array(
  32. array(0),
  33. array('0'),
  34. array('090909'),
  35. array(90909),
  36. );
  37. }
  38. /**
  39. * @dataProvider getInvalidValues
  40. */
  41. public function testInvalidValues($value)
  42. {
  43. $constraint = new Regex(array('pattern' => '/^[0-9]+$/'));
  44. $this->assertFalse($this->validator->isValid($value, $constraint));
  45. }
  46. public function getInvalidValues()
  47. {
  48. return array(
  49. array('abcd'),
  50. array('090foo'),
  51. );
  52. }
  53. public function testMessageIsSet()
  54. {
  55. $constraint = new Regex(array(
  56. 'pattern' => '/^[0-9]+$/',
  57. 'message' => 'myMessage'
  58. ));
  59. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  60. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  61. $this->assertEquals($this->validator->getMessageParameters(), array(
  62. '{{ value }}' => 'foobar',
  63. ));
  64. }
  65. }