ac54e7a4c3d73931735e704d7484b7aac6a412b7
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / Parser / ColumnDefinitionItemTest.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\CreateColumnDefinitionItem;
20 use TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateTableStatement;
21 use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
22
23 /**
24 * Tests for CreateColumnDefinitionItem
25 */
26 class ColumnDefinitionItemTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * @test
30 */
31 public function canParseUnquotedMysqlKeywordAsTableName()
32 {
33 $subject = $this->createSubject('CREATE TABLE `aTable`(checksum VARCHAR(64));');
34
35 $this->assertInstanceOf(CreateColumnDefinitionItem::class, $subject);
36 $this->assertSame($subject->columnName->schemaObjectName, 'checksum');
37 }
38
39 /**
40 * The old regular expression based create table parser processed invalid dump files
41 * where the last column/index definition included a comma before the closing parenthesis.
42 * Emulate this behaviour to avoid breaking lots of (partial) dump files.
43 *
44 * @test
45 */
46 public function canParseCreateDefinitionWithTrailingComma()
47 {
48 $subject = $this->createSubject('CREATE TABLE `aTable`(aField VARCHAR(64), );');
49
50 $this->assertInstanceOf(CreateColumnDefinitionItem::class, $subject);
51 }
52
53 /**
54 * Parse the CREATE TABLE statement and return the reference definition
55 *
56 * @param string $statement
57 * @return \TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateColumnDefinitionItem
58 */
59 protected function createSubject(string $statement): CreateColumnDefinitionItem
60 {
61 $parser = new Parser($statement);
62 /** @var CreateTableStatement $createTableStatement */
63 $createTableStatement = $parser->getAST();
64
65 return $createTableStatement->createDefinition->items[0];
66 }
67 }