d1c39ca1dc69718b844bc710d916d9ef88884859
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / Parser / CreateTableFragmentTest.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 TYPO3\CMS\Core\Database\Schema\Parser\AST\AbstractCreateStatement;
20 use TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateTableStatement;
21 use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
22
23 /**
24 * Tests for CreateTableStatement
25 */
26 class CreateTableFragmentTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * Each parameter array consists of the following values:
30 * - create table SQL fragment
31 * - table name
32 * - is temporary
33 *
34 * @return array
35 */
36 public function canParseCreateTableFragmentDataProvider(): array
37 {
38 return [
39 'CREATE TABLE' => [
40 'CREATE TABLE aTable (aField INT);',
41 'aTable',
42 false
43 ],
44 'CREATE TEMPORARY TABLE' => [
45 'CREATE TEMPORARY TABLE aTable (aField INT);',
46 'aTable',
47 true
48 ],
49 'CREATE TABLE IF NOT EXISTS' => [
50 'CREATE TABLE IF NOT EXISTS aTable (aField INT);',
51 'aTable',
52 false
53 ],
54 'CREATE TEMPORARY TABLE IF NOT EXISTS' => [
55 'CREATE TEMPORARY TABLE IF NOT EXISTS aTable (aField INT);',
56 'aTable',
57 true
58 ],
59 'CREATE TABLE (quoted table name)' => [
60 'CREATE TABLE `aTable` (aField INT);',
61 'aTable',
62 false
63 ],
64 'CREATE TEMPORARY TABLE (quoted table name)' => [
65 'CREATE TEMPORARY TABLE `aTable` (aField INT);',
66 'aTable',
67 true
68 ],
69 'CREATE TABLE IF NOT EXISTS (quoted table name)' => [
70 'CREATE TABLE IF NOT EXISTS `aTable` (aField INT);',
71 'aTable',
72 false
73 ],
74 'CREATE TEMPORARY TABLE IF NOT EXISTS (quoted table name)' => [
75 'CREATE TEMPORARY TABLE IF NOT EXISTS `aTable` (aField INT);',
76 'aTable',
77 true
78 ],
79 ];
80 }
81
82 /**
83 * @test
84 * @dataProvider canParseCreateTableFragmentDataProvider
85 * @param string $statement
86 * @param string $tableName
87 * @param bool $isTemporary
88 */
89 public function canParseCreateTableFragment(string $statement, string $tableName, bool $isTemporary)
90 {
91 $subject = $this->createSubject($statement);
92 $this->assertInstanceOf(CreateTableStatement::class, $subject);
93 $this->assertSame($tableName, $subject->tableName->schemaObjectName);
94 $this->assertSame($isTemporary, $subject->isTemporary);
95 }
96
97 /**
98 * Parse the CREATE TABLE statement and return the reference definition
99 *
100 * @param string $statement
101 * @return AbstractCreateStatement|CreateTableStatement
102 */
103 protected function createSubject(string $statement): AbstractCreateStatement
104 {
105 $parser = new Parser($statement);
106 return $parser->getAST();
107 }
108 }