DateValidatorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Date;
  12. use Symfony\Component\Validator\Constraints\DateValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class DateValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new DateValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new Date());
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new Date());
  32. $this->assertNoViolation();
  33. }
  34. public function testDateTimeClassIsValid()
  35. {
  36. $this->validator->validate(new \DateTime(), new Date());
  37. $this->assertNoViolation();
  38. }
  39. /**
  40. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  41. */
  42. public function testExpectsStringCompatibleType()
  43. {
  44. $this->validator->validate(new \stdClass(), new Date());
  45. }
  46. /**
  47. * @dataProvider getValidDates
  48. */
  49. public function testValidDates($date)
  50. {
  51. $this->validator->validate($date, new Date());
  52. $this->assertNoViolation();
  53. }
  54. public function getValidDates()
  55. {
  56. return array(
  57. array('2010-01-01'),
  58. array('1955-12-12'),
  59. array('2030-05-31'),
  60. );
  61. }
  62. /**
  63. * @dataProvider getInvalidDates
  64. */
  65. public function testInvalidDates($date, $code)
  66. {
  67. $constraint = new Date(array(
  68. 'message' => 'myMessage',
  69. ));
  70. $this->validator->validate($date, $constraint);
  71. $this->buildViolation('myMessage')
  72. ->setParameter('{{ value }}', '"'.$date.'"')
  73. ->setCode($code)
  74. ->assertRaised();
  75. }
  76. public function getInvalidDates()
  77. {
  78. return array(
  79. array('foobar', Date::INVALID_FORMAT_ERROR),
  80. array('foobar 2010-13-01', Date::INVALID_FORMAT_ERROR),
  81. array('2010-13-01 foobar', Date::INVALID_FORMAT_ERROR),
  82. array('2010-13-01', Date::INVALID_DATE_ERROR),
  83. array('2010-04-32', Date::INVALID_DATE_ERROR),
  84. array('2010-02-29', Date::INVALID_DATE_ERROR),
  85. );
  86. }
  87. }