DateTimeValidatorTest.php 2.7 KB

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