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