[FEATURE] Doctrine: Implement SchemaMigrationService
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / Parser / TableBuilderTest.php
1 <?php
2 declare(strict_types=1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser;
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 Doctrine\DBAL\Schema\ForeignKeyConstraint;
20 use Doctrine\DBAL\Schema\Index;
21 use Doctrine\DBAL\Schema\Table;
22 use Doctrine\DBAL\Types\IntegerType;
23 use Doctrine\DBAL\Types\SmallIntType;
24 use Doctrine\DBAL\Types\TextType;
25 use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
26 use TYPO3\CMS\Core\Database\Schema\SqlReader;
27 use TYPO3\CMS\Core\Tests\UnitTestCase;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30 /**
31 * Tests for TableBuilder
32 */
33 class TableBuilderTest extends UnitTestCase
34 {
35 /**
36 * @var Table
37 */
38 protected $table;
39
40 /**
41 * Setup test subject
42 */
43 protected function setUp()
44 {
45 parent::setUp();
46 $sqlFile = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', 'tablebuilder.sql']));
47 $sqlReader = GeneralUtility::makeInstance(SqlReader::class);
48 $statements = $sqlReader->getCreateTableStatementArray($sqlFile);
49
50 $parser = GeneralUtility::makeInstance(Parser::class, $statements[0]);
51 $this->table = $parser->parse()[0];
52 }
53
54 /**
55 * @test
56 */
57 public function hasExpectedTableName()
58 {
59 $this->assertSame('aTestTable', $this->table->getName());
60 }
61
62 /**
63 * @test
64 */
65 public function hasExpectedTableEngine()
66 {
67 $this->assertTrue($this->table->hasOption('engine'));
68 $this->assertSame('MyISAM', $this->table->getOption('engine'));
69 }
70
71 /**
72 * @test
73 */
74 public function hasExpectedTableCollation()
75 {
76 $this->assertTrue($this->table->hasOption('charset'));
77 $this->assertSame('latin1', $this->table->getOption('charset'));
78 }
79
80 /**
81 * @test
82 */
83 public function hasExpectedTableCharacterSet()
84 {
85 $this->assertTrue($this->table->hasOption('collate'));
86 $this->assertSame('latin1_german_cs', $this->table->getOption('collate'));
87 }
88
89 /**
90 * @test
91 */
92 public function hasExpectedTableRowFormat()
93 {
94 $this->assertTrue($this->table->hasOption('row_format'));
95 $this->assertSame('DYNAMIC', $this->table->getOption('row_format'));
96 }
97
98 /**
99 * @test
100 */
101 public function hasExpectedTableAutoIncrementValue()
102 {
103 $this->assertTrue($this->table->hasOption('auto_increment'));
104 $this->assertSame('1', $this->table->getOption('auto_increment'));
105 }
106
107 /**
108 * @test
109 */
110 public function isExpectedUidColumn()
111 {
112 $subject = $this->table->getColumn('uid');
113 $this->assertInstanceOf(IntegerType::class, $subject->getType());
114 $this->assertSame(11, $subject->getLength());
115 $this->assertFalse($subject->getUnsigned());
116 $this->assertTrue($subject->getNotnull());
117 $this->assertNull($subject->getDefault());
118 $this->assertTrue($subject->getAutoincrement());
119 }
120
121 /**
122 * @test
123 */
124 public function isExpectedPidColumn()
125 {
126 $subject = $this->table->getColumn('pid');
127 $this->assertInstanceOf(IntegerType::class, $subject->getType());
128 $this->assertSame(11, $subject->getLength());
129 $this->assertFalse($subject->getUnsigned());
130 $this->assertTrue($subject->getNotnull());
131 $this->assertFalse($subject->getAutoincrement());
132 $this->assertSame('0', $subject->getDefault());
133 }
134
135 /**
136 * @test
137 */
138 public function isExpectedTstampColumn()
139 {
140 $subject = $this->table->getColumn('tstamp');
141 $this->assertInstanceOf(IntegerType::class, $subject->getType());
142 $this->assertSame(11, $subject->getLength());
143 $this->assertTrue($subject->getUnsigned());
144 $this->assertTrue($subject->getNotnull());
145 $this->assertFalse($subject->getAutoincrement());
146 $this->assertSame('0', $subject->getDefault());
147 }
148
149 /**
150 * @test
151 */
152 public function isExpectedSortingColumn()
153 {
154 $subject = $this->table->getColumn('sorting');
155 $this->assertInstanceOf(IntegerType::class, $subject->getType());
156 $this->assertSame(11, $subject->getLength());
157 $this->assertTrue($subject->getUnsigned());
158 $this->assertTrue($subject->getNotnull());
159 $this->assertFalse($subject->getAutoincrement());
160 $this->assertSame(0, $subject->getDefault());
161 }
162
163 /**
164 * @test
165 */
166 public function isExpectedDeletedColumn()
167 {
168 $subject = $this->table->getColumn('deleted');
169 $this->assertInstanceOf(SmallIntType::class, $subject->getType());
170 $this->assertSame(1, $subject->getLength());
171 $this->assertTrue($subject->getUnsigned());
172 $this->assertTrue($subject->getNotnull());
173 $this->assertFalse($subject->getAutoincrement());
174 $this->assertSame('0', $subject->getDefault());
175 }
176
177 /**
178 * @test
179 */
180 public function isExpectedTSconfigColumn()
181 {
182 $subject = $this->table->getColumn('TSconfig');
183 $this->assertInstanceOf(TextType::class, $subject->getType());
184 $this->assertSame(65535, $subject->getLength());
185 $this->assertFalse($subject->getNotnull());
186 $this->assertNull($subject->getDefault());
187 }
188
189 /**
190 * @test
191 */
192 public function isExpectedNoCacheColumn()
193 {
194 $subject = $this->table->getColumn('no_cache');
195 $this->assertInstanceOf(IntegerType::class, $subject->getType());
196 $this->assertSame(10, $subject->getLength());
197 $this->assertTrue($subject->getUnsigned());
198 $this->assertTrue($subject->getNotnull());
199 $this->assertFalse($subject->getAutoincrement());
200 $this->assertSame('0', $subject->getDefault());
201 }
202
203 /**
204 * @test
205 */
206 public function isExpectedPrimaryKey()
207 {
208 $subject = $this->table->getPrimaryKey();
209 $this->assertInstanceOf(Index::class, $subject);
210 $this->assertTrue($subject->isPrimary());
211 $this->assertSame(['uid'], $subject->getColumns());
212 }
213
214 /**
215 * @test
216 */
217 public function isExpectedParentKey()
218 {
219 $subject = $this->table->getIndex('parent');
220 $this->assertInstanceOf(Index::class, $subject);
221 $this->assertTrue($subject->isUnique());
222 $this->assertSame(['pid', 'deleted', 'sorting'], $subject->getColumns());
223 }
224
225 /**
226 * @test
227 */
228 public function isExpectedNoCacheKey()
229 {
230 $subject = $this->table->getIndex('noCache');
231 $this->assertInstanceOf(Index::class, $subject);
232 $this->assertTrue($subject->isSimpleIndex());
233 $this->assertSame(['no_cache'], $subject->getColumns());
234 }
235
236 /**
237 * @test
238 */
239 public function isExpectedForeignKey()
240 {
241 $subject = $this->table->getForeignKey('fk_overlay');
242 $this->assertInstanceOf(ForeignKeyConstraint::class, $subject);
243 $this->assertSame(['pid'], $subject->getForeignColumns());
244 $this->assertSame(['uid'], $subject->getLocalColumns());
245 $this->assertSame('aTestTable', $subject->getLocalTableName());
246 $this->assertSame('pages_language_overlay', $subject->getForeignTableName());
247 }
248 }