[BUGFIX] Properly validate DateTimeImmutable objects
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / DateTimeValidatorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
4
5 /* *
6 * This script belongs to the Extbase framework. *
7 * *
8 * It is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU Lesser General Public License as published by the *
10 * Free Software Foundation, either version 3 of the License, or (at your *
11 * option) any later version. *
12 * *
13 * This script is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
15 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
16 * General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with the script. *
20 * If not, see http://www.gnu.org/licenses/lgpl.html *
21 * *
22 * The TYPO3 project - inspiring people to share! *
23 * */
24
25 use TYPO3\CMS\Extbase\Validation\Validator\DateTimeValidator;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28 /**
29 * Test case
30 */
31 class DateTimeValidatorTest extends UnitTestCase
32 {
33 /**
34 * @test
35 * @dataProvider dateTimeValues
36 */
37 public function acceptsDateTimeValues($value)
38 {
39 $validator = new DateTimeValidator();
40 $result = $validator->validate($value);
41
42 $this->assertFalse($result->hasErrors());
43 }
44
45 /**
46 * @return array
47 */
48 public function dateTimeValues(): array
49 {
50 return [
51 \DateTime::class => [
52 new \DateTime(),
53 ],
54 'Extended ' . \DateTime::class => [
55 new class extends \DateTime {
56 },
57 ],
58 \DateTimeImmutable::class => [
59 new \DateTimeImmutable(),
60 ],
61 'Extended ' . \DateTimeImmutable::class => [
62 new class extends \DateTimeImmutable {
63 },
64 ],
65 ];
66 }
67
68 /**
69 * @test
70 */
71 public function addsErrorForInvalidValue()
72 {
73 $validator = $this->getMockBuilder(DateTimeValidator::class)
74 ->setMethods(['translateErrorMessage'])
75 ->getMock();
76 $result = $validator->validate(false);
77
78 $this->assertTrue($result->hasErrors());
79 }
80 }