RegexValidatorTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Tests\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraints\Regex;
  12. use Symfony\Component\Validator\Constraints\RegexValidator;
  13. class RegexValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new RegexValidator();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new Regex(array('pattern' => '/^[0-9]+$/'))));
  27. }
  28. public function testEmptyStringIsValid()
  29. {
  30. $this->assertTrue($this->validator->isValid('', new Regex(array('pattern' => '/^[0-9]+$/'))));
  31. }
  32. public function testExpectsStringCompatibleType()
  33. {
  34. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  35. $this->validator->isValid(new \stdClass(), new Regex(array('pattern' => '/^[0-9]+$/')));
  36. }
  37. /**
  38. * @dataProvider getValidValues
  39. */
  40. public function testValidValues($value)
  41. {
  42. $constraint = new Regex(array('pattern' => '/^[0-9]+$/'));
  43. $this->assertTrue($this->validator->isValid($value, $constraint));
  44. }
  45. public function getValidValues()
  46. {
  47. return array(
  48. array(0),
  49. array('0'),
  50. array('090909'),
  51. array(90909),
  52. );
  53. }
  54. /**
  55. * @dataProvider getInvalidValues
  56. */
  57. public function testInvalidValues($value)
  58. {
  59. $constraint = new Regex(array('pattern' => '/^[0-9]+$/'));
  60. $this->assertFalse($this->validator->isValid($value, $constraint));
  61. }
  62. public function getInvalidValues()
  63. {
  64. return array(
  65. array('abcd'),
  66. array('090foo'),
  67. );
  68. }
  69. public function testMessageIsSet()
  70. {
  71. $constraint = new Regex(array(
  72. 'pattern' => '/^[0-9]+$/',
  73. 'message' => 'myMessage'
  74. ));
  75. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  76. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  77. $this->assertEquals($this->validator->getMessageParameters(), array(
  78. '{{ value }}' => 'foobar',
  79. ));
  80. }
  81. public function testConstraintGetDefaultOption()
  82. {
  83. $constraint = new Regex(array(
  84. 'pattern' => '/^[0-9]+$/',
  85. ));
  86. $this->assertEquals('pattern', $constraint->getDefaultOption());
  87. }
  88. }