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