[FEATURE] Doctrine: Implement SchemaMigrationService
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / Parser / DataTypes / DateTimeTypesTest.php
1 <?php
2 declare(strict_types=1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\DataTypes;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Database\Schema\Exception\StatementException;
20 use TYPO3\CMS\Core\Database\Schema\Parser\AST\DataType\DateDataType;
21 use TYPO3\CMS\Core\Database\Schema\Parser\AST\DataType\DateTimeDataType;
22 use TYPO3\CMS\Core\Database\Schema\Parser\AST\DataType\TimeDataType;
23 use TYPO3\CMS\Core\Database\Schema\Parser\AST\DataType\TimestampDataType;
24 use TYPO3\CMS\Core\Database\Schema\Parser\AST\DataType\YearDataType;
25 use TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\AbstractDataTypeBaseTestCase;
26
27 /**
28 * Tests for parsing DATE/TIME related SQL data types
29 */
30 class DateTimeTypesTest extends AbstractDataTypeBaseTestCase
31 {
32 /**
33 * Data provider for canParseDateTimeType()
34 *
35 * @return array
36 */
37 public function canParseDateTimeTypeProvider(): array
38 {
39 return [
40 'DATE' => [
41 'DATE',
42 DateDataType::class,
43 null,
44 ],
45 'YEAR' => [
46 'YEAR',
47 YearDataType::class,
48 null,
49 ],
50 'TIME' => [
51 'TIME',
52 TimeDataType::class,
53 0,
54 ],
55 'TIME with fractional second part' => [
56 'TIME(3)',
57 TimeDataType::class,
58 3,
59 ],
60 'TIMESTAMP' => [
61 'TIMESTAMP',
62 TimestampDataType::class,
63 0,
64 ],
65 'TIMESTAMP with fractional second part' => [
66 'TIMESTAMP(3)',
67 TimestampDataType::class,
68 3,
69 ],
70 'DATETIME' => [
71 'DATETIME',
72 DateTimeDataType::class,
73 0,
74 ],
75 'DATETIME with fractional second part' => [
76 'DATETIME(3)',
77 DateTimeDataType::class,
78 3,
79 ],
80 ];
81 }
82
83 /**
84 * @test
85 * @dataProvider canParseDateTimeTypeProvider
86 * @param string $columnDefinition
87 * @param string $className
88 * @param int $length
89 */
90 public function canParseDataType(string $columnDefinition, string $className, int $length = null)
91 {
92 $subject = $this->createSubject($columnDefinition);
93
94 $this->assertInstanceOf($className, $subject->dataType);
95
96 // DATE & YEAR don't support fractional second parts
97 if ($length !== null) {
98 $this->assertSame($length, $subject->dataType->getLength());
99 }
100 }
101
102 /**
103 * @test
104 */
105 public function parseDateTimeTypeWithInvalidLowerBound()
106 {
107 $this->expectException(StatementException::class);
108 $this->expectExceptionMessageRegExp(
109 '@Error: the fractional seconds part for TIME, DATETIME or TIMESTAMP columns must >= 0@'
110 );
111 $this->createSubject('TIME(-1)');
112 }
113
114 /**
115 * @test
116 */
117 public function parseDateTimeTypeWithInvalidUpperBound()
118 {
119 $this->expectException(StatementException::class);
120 $this->expectExceptionMessageRegExp(
121 '@Error: the fractional seconds part for TIME, DATETIME or TIMESTAMP columns must <= 6@'
122 );
123 $this->createSubject('TIME(7)');
124 }
125 }