RegexValidatorTest.php 2.1 KB

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