DateTimeValidatorTest.php 2.2 KB

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