DateTimeValidatorTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Tests\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraints\DateTime;
  12. use Symfony\Component\Validator\Constraints\DateTimeValidator;
  13. class DateTimeValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new DateTimeValidator();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new DateTime()));
  27. }
  28. public function testEmptyStringIsValid()
  29. {
  30. $this->assertTrue($this->validator->isValid('', new DateTime()));
  31. }
  32. public function testDateTimeClassIsValid()
  33. {
  34. $this->assertTrue($this->validator->isValid(new \DateTime(), new DateTime()));
  35. }
  36. public function testExpectsStringCompatibleType()
  37. {
  38. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  39. $this->validator->isValid(new \stdClass(), new DateTime());
  40. }
  41. /**
  42. * @dataProvider getValidDateTimes
  43. */
  44. public function testValidDateTimes($date)
  45. {
  46. $this->assertTrue($this->validator->isValid($date, new DateTime()));
  47. }
  48. public function getValidDateTimes()
  49. {
  50. return array(
  51. array('2010-01-01 01:02:03'),
  52. array('1955-12-12 00:00:00'),
  53. array('2030-05-31 23:59:59'),
  54. );
  55. }
  56. /**
  57. * @dataProvider getInvalidDateTimes
  58. */
  59. public function testInvalidDateTimes($date)
  60. {
  61. $this->assertFalse($this->validator->isValid($date, new DateTime()));
  62. }
  63. public function getInvalidDateTimes()
  64. {
  65. return array(
  66. array('foobar'),
  67. array('2010-01-01'),
  68. array('00:00:00'),
  69. array('2010-01-01 00:00'),
  70. array('2010-13-01 00:00:00'),
  71. array('2010-04-32 00:00:00'),
  72. array('2010-02-29 00:00:00'),
  73. array('2010-01-01 24:00:00'),
  74. array('2010-01-01 00:60:00'),
  75. array('2010-01-01 00:00:60'),
  76. );
  77. }
  78. public function testMessageIsSet()
  79. {
  80. $constraint = new DateTime(array(
  81. 'message' => 'myMessage'
  82. ));
  83. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  84. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  85. $this->assertEquals($this->validator->getMessageParameters(), array(
  86. '{{ value }}' => 'foobar',
  87. ));
  88. }
  89. }