DateValidatorTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\Date;
  4. use Symfony\Component\Validator\Constraints\DateValidator;
  5. class DateValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. public function setUp()
  9. {
  10. $this->validator = new DateValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new Date()));
  15. }
  16. public function testExpectsStringCompatibleType()
  17. {
  18. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  19. $this->validator->isValid(new \stdClass(), new Date());
  20. }
  21. /**
  22. * @dataProvider getValidDates
  23. */
  24. public function testValidDates($date)
  25. {
  26. $this->assertTrue($this->validator->isValid($date, new Date()));
  27. }
  28. public function getValidDates()
  29. {
  30. return array(
  31. array('2010-01-01'),
  32. array('1955-12-12'),
  33. array('2030-05-31'),
  34. );
  35. }
  36. /**
  37. * @dataProvider getInvalidDates
  38. */
  39. public function testInvalidDates($date)
  40. {
  41. $this->assertFalse($this->validator->isValid($date, new Date()));
  42. }
  43. public function getInvalidDates()
  44. {
  45. return array(
  46. array('foobar'),
  47. array('2010-13-01'),
  48. array('2010-04-32'),
  49. array('2010-02-29'),
  50. );
  51. }
  52. public function testMessageIsSet()
  53. {
  54. $constraint = new Date(array(
  55. 'message' => 'myMessage'
  56. ));
  57. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  58. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  59. $this->assertEquals($this->validator->getMessageParameters(), array(
  60. '{{ value }}' => 'foobar',
  61. ));
  62. }
  63. }