DateValidatorTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. protected 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 testEmptyStringIsValid()
  17. {
  18. $this->assertTrue($this->validator->isValid('', new Date()));
  19. }
  20. public function testExpectsStringCompatibleType()
  21. {
  22. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  23. $this->validator->isValid(new \stdClass(), new Date());
  24. }
  25. /**
  26. * @dataProvider getValidDates
  27. */
  28. public function testValidDates($date)
  29. {
  30. $this->assertTrue($this->validator->isValid($date, new Date()));
  31. }
  32. public function getValidDates()
  33. {
  34. return array(
  35. array('2010-01-01'),
  36. array('1955-12-12'),
  37. array('2030-05-31'),
  38. );
  39. }
  40. /**
  41. * @dataProvider getInvalidDates
  42. */
  43. public function testInvalidDates($date)
  44. {
  45. $this->assertFalse($this->validator->isValid($date, new Date()));
  46. }
  47. public function getInvalidDates()
  48. {
  49. return array(
  50. array('foobar'),
  51. array('2010-13-01'),
  52. array('2010-04-32'),
  53. array('2010-02-29'),
  54. );
  55. }
  56. public function testMessageIsSet()
  57. {
  58. $constraint = new Date(array(
  59. 'message' => 'myMessage'
  60. ));
  61. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  62. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  63. $this->assertEquals($this->validator->getMessageParameters(), array(
  64. '{{ value }}' => 'foobar',
  65. ));
  66. }
  67. }