DateTimeValidatorTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\DateTime;
  12. use Symfony\Component\Validator\Constraints\DateTimeValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class DateTimeValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new DateTimeValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new DateTime());
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new DateTime());
  32. $this->assertNoViolation();
  33. }
  34. public function testDateTimeClassIsValid()
  35. {
  36. $this->validator->validate(new \DateTime(), new DateTime());
  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 DateTime());
  45. }
  46. /**
  47. * @dataProvider getValidDateTimes
  48. */
  49. public function testValidDateTimes($dateTime)
  50. {
  51. $this->validator->validate($dateTime, new DateTime());
  52. $this->assertNoViolation();
  53. }
  54. public function getValidDateTimes()
  55. {
  56. return array(
  57. array('2010-01-01 01:02:03'),
  58. array('1955-12-12 00:00:00'),
  59. array('2030-05-31 23:59:59'),
  60. );
  61. }
  62. /**
  63. * @dataProvider getInvalidDateTimes
  64. */
  65. public function testInvalidDateTimes($dateTime, $code)
  66. {
  67. $constraint = new DateTime(array(
  68. 'message' => 'myMessage',
  69. ));
  70. $this->validator->validate($dateTime, $constraint);
  71. $this->buildViolation('myMessage')
  72. ->setParameter('{{ value }}', '"'.$dateTime.'"')
  73. ->setCode($code)
  74. ->assertRaised();
  75. }
  76. public function getInvalidDateTimes()
  77. {
  78. return array(
  79. array('foobar', DateTime::INVALID_FORMAT_ERROR),
  80. array('2010-01-01', DateTime::INVALID_FORMAT_ERROR),
  81. array('00:00:00', DateTime::INVALID_FORMAT_ERROR),
  82. array('2010-01-01 00:00', DateTime::INVALID_FORMAT_ERROR),
  83. array('2010-13-01 00:00:00', DateTime::INVALID_DATE_ERROR),
  84. array('2010-04-32 00:00:00', DateTime::INVALID_DATE_ERROR),
  85. array('2010-02-29 00:00:00', DateTime::INVALID_DATE_ERROR),
  86. array('2010-01-01 24:00:00', DateTime::INVALID_TIME_ERROR),
  87. array('2010-01-01 00:60:00', DateTime::INVALID_TIME_ERROR),
  88. array('2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR),
  89. );
  90. }
  91. }