RegexValidatorTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Validator\Constraints\Regex;
  12. use Symfony\Component\Validator\Constraints\RegexValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class RegexValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new RegexValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new Regex(array('pattern' => '/^[0-9]+$/')));
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new Regex(array('pattern' => '/^[0-9]+$/')));
  32. $this->assertNoViolation();
  33. }
  34. /**
  35. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  36. */
  37. public function testExpectsStringCompatibleType()
  38. {
  39. $this->validator->validate(new \stdClass(), new Regex(array('pattern' => '/^[0-9]+$/')));
  40. }
  41. /**
  42. * @dataProvider getValidValues
  43. */
  44. public function testValidValues($value)
  45. {
  46. $constraint = new Regex(array('pattern' => '/^[0-9]+$/'));
  47. $this->validator->validate($value, $constraint);
  48. $this->assertNoViolation();
  49. }
  50. public function getValidValues()
  51. {
  52. return array(
  53. array(0),
  54. array('0'),
  55. array('090909'),
  56. array(90909),
  57. );
  58. }
  59. /**
  60. * @dataProvider getInvalidValues
  61. */
  62. public function testInvalidValues($value)
  63. {
  64. $constraint = new Regex(array(
  65. 'pattern' => '/^[0-9]+$/',
  66. 'message' => 'myMessage',
  67. ));
  68. $this->validator->validate($value, $constraint);
  69. $this->buildViolation('myMessage')
  70. ->setParameter('{{ value }}', '"'.$value.'"')
  71. ->setCode(Regex::REGEX_FAILED_ERROR)
  72. ->assertRaised();
  73. }
  74. public function getInvalidValues()
  75. {
  76. return array(
  77. array('abcd'),
  78. array('090foo'),
  79. );
  80. }
  81. }