1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\Validator\Tests\Constraints; 13 14use Symfony\Component\Validator\Constraints\DateTime; 15use Symfony\Component\Validator\Constraints\DateTimeValidator; 16use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; 17 18class DateTimeValidatorTest extends ConstraintValidatorTestCase 19{ 20 protected function createValidator() 21 { 22 return new DateTimeValidator(); 23 } 24 25 public function testNullIsValid() 26 { 27 $this->validator->validate(null, new DateTime()); 28 29 $this->assertNoViolation(); 30 } 31 32 public function testEmptyStringIsValid() 33 { 34 $this->validator->validate('', new DateTime()); 35 36 $this->assertNoViolation(); 37 } 38 39 public function testDateTimeClassIsValid() 40 { 41 $this->validator->validate(new \DateTime(), new DateTime()); 42 43 $this->assertNoViolation(); 44 } 45 46 public function testDateTimeImmutableClassIsValid() 47 { 48 $this->validator->validate(new \DateTimeImmutable(), new DateTime()); 49 50 $this->assertNoViolation(); 51 } 52 53 public function testExpectsStringCompatibleType() 54 { 55 $this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException'); 56 $this->validator->validate(new \stdClass(), new DateTime()); 57 } 58 59 public function testDateTimeWithDefaultFormat() 60 { 61 $this->validator->validate('1995-05-10 19:33:00', new DateTime()); 62 63 $this->assertNoViolation(); 64 65 $this->validator->validate('1995-03-24', new DateTime()); 66 67 $this->buildViolation('This value is not a valid datetime.') 68 ->setParameter('{{ value }}', '"1995-03-24"') 69 ->setCode(DateTime::INVALID_FORMAT_ERROR) 70 ->assertRaised(); 71 } 72 73 /** 74 * @dataProvider getValidDateTimes 75 */ 76 public function testValidDateTimes($format, $dateTime) 77 { 78 $constraint = new DateTime([ 79 'format' => $format, 80 ]); 81 82 $this->validator->validate($dateTime, $constraint); 83 84 $this->assertNoViolation(); 85 } 86 87 public function getValidDateTimes() 88 { 89 return [ 90 ['Y-m-d H:i:s e', '1995-03-24 00:00:00 UTC'], 91 ['Y-m-d H:i:s', '2010-01-01 01:02:03'], 92 ['Y/m/d H:i', '2010/01/01 01:02'], 93 ['F d, Y', 'December 31, 1999'], 94 ['d-m-Y', '10-05-1995'], 95 ]; 96 } 97 98 /** 99 * @dataProvider getInvalidDateTimes 100 */ 101 public function testInvalidDateTimes($format, $dateTime, $code) 102 { 103 $constraint = new DateTime([ 104 'message' => 'myMessage', 105 'format' => $format, 106 ]); 107 108 $this->validator->validate($dateTime, $constraint); 109 110 $this->buildViolation('myMessage') 111 ->setParameter('{{ value }}', '"'.$dateTime.'"') 112 ->setCode($code) 113 ->assertRaised(); 114 } 115 116 public function getInvalidDateTimes() 117 { 118 return [ 119 ['Y-m-d', 'foobar', DateTime::INVALID_FORMAT_ERROR], 120 ['H:i', '00:00:00', DateTime::INVALID_FORMAT_ERROR], 121 ['Y-m-d', '2010-01-01 00:00', DateTime::INVALID_FORMAT_ERROR], 122 ['Y-m-d e', '2010-01-01 TCU', DateTime::INVALID_FORMAT_ERROR], 123 ['Y-m-d H:i:s', '2010-13-01 00:00:00', DateTime::INVALID_DATE_ERROR], 124 ['Y-m-d H:i:s', '2010-04-32 00:00:00', DateTime::INVALID_DATE_ERROR], 125 ['Y-m-d H:i:s', '2010-02-29 00:00:00', DateTime::INVALID_DATE_ERROR], 126 ['Y-m-d H:i:s', '2010-01-01 24:00:00', DateTime::INVALID_TIME_ERROR], 127 ['Y-m-d H:i:s', '2010-01-01 00:60:00', DateTime::INVALID_TIME_ERROR], 128 ['Y-m-d H:i:s', '2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR], 129 ]; 130 } 131 132 public function testDateTimeWithTrailingData() 133 { 134 $this->validator->validate('1995-05-10 00:00:00', new DateTime([ 135 'format' => 'Y-m-d+', 136 ])); 137 $this->assertNoViolation(); 138 } 139} 140