DateTimeValidatorTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\DateTime;
  4. use Symfony\Component\Validator\Constraints\DateTimeValidator;
  5. class DateTimeValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. protected function setUp()
  9. {
  10. $this->validator = new DateTimeValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new DateTime()));
  15. }
  16. public function testEmptyStringIsValid()
  17. {
  18. $this->assertTrue($this->validator->isValid('', new DateTime()));
  19. }
  20. public function testExpectsStringCompatibleType()
  21. {
  22. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  23. $this->validator->isValid(new \stdClass(), new DateTime());
  24. }
  25. /**
  26. * @dataProvider getValidDateTimes
  27. */
  28. public function testValidDateTimes($date)
  29. {
  30. $this->assertTrue($this->validator->isValid($date, new DateTime()));
  31. }
  32. public function getValidDateTimes()
  33. {
  34. return array(
  35. array('2010-01-01 01:02:03'),
  36. array('1955-12-12 00:00:00'),
  37. array('2030-05-31 23:59:59'),
  38. );
  39. }
  40. /**
  41. * @dataProvider getInvalidDateTimes
  42. */
  43. public function testInvalidDateTimes($date)
  44. {
  45. $this->assertFalse($this->validator->isValid($date, new DateTime()));
  46. }
  47. public function getInvalidDateTimes()
  48. {
  49. return array(
  50. array('foobar'),
  51. array('2010-01-01'),
  52. array('00:00:00'),
  53. array('2010-01-01 00:00'),
  54. array('2010-13-01 00:00:00'),
  55. array('2010-04-32 00:00:00'),
  56. array('2010-02-29 00:00:00'),
  57. array('2010-01-01 24:00:00'),
  58. array('2010-01-01 00:60:00'),
  59. array('2010-01-01 00:00:60'),
  60. );
  61. }
  62. public function testMessageIsSet()
  63. {
  64. $constraint = new DateTime(array(
  65. 'message' => 'myMessage'
  66. ));
  67. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  68. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  69. $this->assertEquals($this->validator->getMessageParameters(), array(
  70. '{{ value }}' => 'foobar',
  71. ));
  72. }
  73. }