9d3b945315650977c6b5fe57db7bf2091621b041
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / SqlReaderTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\Database\Schema;
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\SqlReader;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class SqlReaderTest extends UnitTestCase
26 {
27 /**
28 * @var bool Reset singletons created by subject
29 */
30 protected $resetSingletonInstances = true;
31
32 /**
33 * @test
34 */
35 public function getStatementArraySplitsStatements()
36 {
37 $subject = new SqlReader();
38 $result = $subject->getStatementArray(
39 'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
40 LF .
41 'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
42 );
43 $this->assertCount(2, $result);
44 $this->assertStringStartsWith('CREATE TABLE', $result[0]);
45 $this->assertStringStartsWith('INSERT INTO', $result[1]);
46 }
47
48 /**
49 * @test
50 */
51 public function getStatementArrayFiltersStatements()
52 {
53 $subject = new SqlReader();
54 $result = $subject->getStatementArray(
55 'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
56 LF .
57 'INSERT INTO aTestTable(`aTestField`) VALUES(1);',
58 '^CREATE TABLE'
59 );
60 $this->assertCount(1, $result);
61 $this->assertStringStartsWith('CREATE TABLE', array_pop($result));
62 }
63
64 /**
65 * @test
66 */
67 public function getInsertStatementArrayResult()
68 {
69 $subject = new SqlReader();
70 $result = $subject->getInsertStatementArray(
71 'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
72 LF .
73 'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
74 );
75
76 $this->assertCount(1, $result);
77 $this->assertStringStartsWith('INSERT', array_pop($result));
78 }
79
80 /**
81 * @test
82 */
83 public function getInsertStatementArrayResultWithNewline()
84 {
85 $subject = new SqlReader();
86 $result = $subject->getInsertStatementArray(
87 'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
88 LF .
89 'INSERT INTO aTestTable(`aTestField`) ' .
90 LF .
91 'VALUES(1);'
92 );
93
94 $this->assertCount(1, $result);
95 $this->assertSame('INSERT INTO aTestTable(`aTestField`) VALUES(1);', array_pop($result));
96 }
97
98 /**
99 * @test
100 */
101 public function getCreateTableStatementArrayResult()
102 {
103 $subject = new SqlReader();
104 $result = $subject->getCreateTableStatementArray(
105 'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
106 LF .
107 'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
108 );
109 $this->assertCount(1, $result);
110 $this->assertStringStartsWith('CREATE TABLE', array_pop($result));
111 }
112 }