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