‪TYPO3CMS  11.5
SqlReaderTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\PhpUnit\ProphecyTrait;
21 use Psr\EventDispatcher\EventDispatcherInterface;
23 use TYPO3\CMS\Core\Package\PackageManager;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪SqlReaderTest extends UnitTestCase
30 {
31  use ProphecyTrait;
32 
36  protected ‪$resetSingletonInstances = true;
37 
41  public function ‪getStatementArraySplitsStatements(): void
42  {
43  $subject = new ‪SqlReader($this->prophesize(EventDispatcherInterface::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
44  $result = $subject->getStatementArray(
45  'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
46  LF .
47  'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
48  );
49  self::assertCount(2, $result);
50  self::assertStringStartsWith('CREATE TABLE', $result[0]);
51  self::assertStringStartsWith('INSERT INTO', $result[1]);
52  }
53 
57  public function ‪getStatementArrayFiltersStatements(): void
58  {
59  $subject = new ‪SqlReader($this->prophesize(EventDispatcherInterface::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
60  $result = $subject->getStatementArray(
61  'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
62  LF .
63  'INSERT INTO aTestTable(`aTestField`) VALUES(1);',
64  '^CREATE TABLE'
65  );
66  self::assertCount(1, $result);
67  self::assertStringStartsWith('CREATE TABLE', array_pop($result));
68  }
69 
73  public function ‪getInsertStatementArrayResult(): void
74  {
75  $subject = new ‪SqlReader($this->prophesize(EventDispatcherInterface::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
76  $result = $subject->getInsertStatementArray(
77  'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
78  LF .
79  'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
80  );
81 
82  self::assertCount(1, $result);
83  self::assertStringStartsWith('INSERT', array_pop($result));
84  }
85 
89  public function ‪getInsertStatementArrayResultWithNewline(): void
90  {
91  $subject = new ‪SqlReader($this->prophesize(EventDispatcherInterface::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
92  $result = $subject->getInsertStatementArray(
93  'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
94  LF .
95  'INSERT INTO aTestTable(`aTestField`) ' .
96  LF .
97  'VALUES(1);'
98  );
99 
100  self::assertCount(1, $result);
101  self::assertSame('INSERT INTO aTestTable(`aTestField`) VALUES(1);', array_pop($result));
102  }
103 
107  public function ‪getCreateTableStatementArrayResult(): void
108  {
109  $subject = new ‪SqlReader($this->prophesize(EventDispatcherInterface::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
110  $result = $subject->getCreateTableStatementArray(
111  'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
112  LF .
113  'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
114  );
115  self::assertCount(1, $result);
116  self::assertStringStartsWith('CREATE TABLE', array_pop($result));
117  }
118 
124  public function ‪getCreateTableStatementArrayResultWithComment(string $comment): void
125  {
126  $subject = new ‪SqlReader($this->prophesize(EventDispatcherInterface::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
127  $result = $subject->getCreateTableStatementArray(
128  $comment . LF . 'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
129  LF .
130  'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
131  );
132  self::assertCount(1, $result);
133  self::assertStringStartsWith('CREATE TABLE', array_pop($result));
134  }
135 
136  public function ‪commentProvider(): array
137  {
138  return [
139  'Single line comment starting with "#"' => [
140  '# Comment',
141  ],
142  'Single line comment starting with "--"' => [
143  '-- Comment',
144  ],
145  'Single line c-style comment' => [
146  '/* Same line c-style comment */',
147  ],
148  'Multiline comment variant 1' => [
149  '/*' . LF . 'Some comment text' . LF . 'more text' . LF . '*/',
150  ],
151  'Multiline comment variant 2' => [
152  '/* More' . LF . ' comments */',
153  ],
154  ];
155  }
156 }
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\getCreateTableStatementArrayResultWithComment
‪getCreateTableStatementArrayResultWithComment(string $comment)
Definition: SqlReaderTest.php:122
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema
Definition: ConnectionMigratorTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\commentProvider
‪commentProvider()
Definition: SqlReaderTest.php:134
‪TYPO3\CMS\Core\Database\Schema\SqlReader
Definition: SqlReader.php:31
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\getStatementArraySplitsStatements
‪getStatementArraySplitsStatements()
Definition: SqlReaderTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\getInsertStatementArrayResult
‪getInsertStatementArrayResult()
Definition: SqlReaderTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest
Definition: SqlReaderTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: SqlReaderTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\getCreateTableStatementArrayResult
‪getCreateTableStatementArrayResult()
Definition: SqlReaderTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\getInsertStatementArrayResultWithNewline
‪getInsertStatementArrayResultWithNewline()
Definition: SqlReaderTest.php:87
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\SqlReaderTest\getStatementArrayFiltersStatements
‪getStatementArrayFiltersStatements()
Definition: SqlReaderTest.php:55