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