[TASK] Reduce memory footprint of unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / Tests / Unit / Database / DatabaseConnectionTest.php
1 <?php
2 namespace TYPO3\CMS\Dbal\Tests\Unit\Database;
3
4 /**
5 * Testcase for class DatabaseConnection.
6 *
7 * @author Xavier Perseguers <xavier@typo3.org>
8 */
9 class DatabaseConnectionTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
10
11 /**
12 * @var array
13 */
14 protected $temporaryFiles;
15
16 /**
17 * Prepares the environment before running a test.
18 */
19 public function setUp() {
20 $this->temporaryFiles = array();
21 $className = self::buildAccessibleProxy('TYPO3\\CMS\\Dbal\\Database\\DatabaseConnection');
22 $GLOBALS['TYPO3_DB'] = new $className();
23 $GLOBALS['TYPO3_DB']->lastHandlerKey = '_DEFAULT';
24 }
25
26 /**
27 * Cleans up the environment after running a test.
28 */
29 public function tearDown() {
30 // Clear DBAL-generated cache files
31 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
32 // Delete temporary files
33 foreach ($this->temporaryFiles as $filename) {
34 unlink($filename);
35 }
36 parent::tearDown();
37 }
38
39 /**
40 * Cleans a SQL query.
41 *
42 * @param mixed $sql
43 * @return mixed (string or array)
44 */
45 private function cleanSql($sql) {
46 if (!is_string($sql)) {
47 return $sql;
48 }
49 $sql = str_replace('
50 ', ' ', $sql);
51 $sql = preg_replace('/\\s+/', ' ', $sql);
52 return trim($sql);
53 }
54
55 /**
56 * Creates a fake extension with a given table definition.
57 *
58 * @param string $tableDefinition SQL script to create the extension's tables
59 * @return void
60 */
61 protected function createFakeExtension($tableDefinition) {
62 // Prepare a fake extension configuration
63 $ext_tables = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('ext_tables');
64 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($ext_tables, $tableDefinition);
65 $this->temporaryFiles[] = $ext_tables;
66 $GLOBALS['TYPO3_LOADED_EXT']['test_dbal'] = array(
67 'ext_tables.sql' => $ext_tables
68 );
69 // Append our test table to the list of existing tables
70 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
71 $GLOBALS['TYPO3_DB']->_call('initInternalVariables');
72 }
73
74 /**
75 * @test
76 * @see http://forge.typo3.org/issues/21502
77 */
78 public function concatCanBeParsedAfterLikeOperator() {
79 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'sys_refindex, tx_dam_file_tracking', 'sys_refindex.tablename = \'tx_dam_file_tracking\'' . ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)'));
80 $expected = 'SELECT * FROM sys_refindex, tx_dam_file_tracking WHERE sys_refindex.tablename = \'tx_dam_file_tracking\'';
81 $expected .= ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)';
82 $this->assertEquals($expected, $query);
83 }
84
85 /**
86 * @test
87 * @see http://forge.typo3.org/issues/20346
88 */
89 public function floatNumberCanBeStoredInDatabase() {
90 $this->createFakeExtension('
91 CREATE TABLE tx_test_dbal (
92 foo double default \'0\',
93 foobar integer default \'0\'
94 );
95 ');
96 $data = array(
97 'foo' => 99.12,
98 'foobar' => -120
99 );
100 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data));
101 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'99.12\', \'-120\' )';
102 $this->assertEquals($expected, $query);
103 }
104
105 /**
106 * @test
107 * @see http://forge.typo3.org/issues/20427
108 */
109 public function positive64BitIntegerIsSupported() {
110 $this->createFakeExtension('
111 CREATE TABLE tx_test_dbal (
112 foo int default \'0\',
113 foobar bigint default \'0\'
114 );
115 ');
116 $data = array(
117 'foo' => 9223372036854775807,
118 'foobar' => 9223372036854775807
119 );
120 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data));
121 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'9223372036854775807\', \'9223372036854775807\' )';
122 $this->assertEquals($expected, $query);
123 }
124
125 /**
126 * @test
127 * @see http://forge.typo3.org/issues/20427
128 */
129 public function negative64BitIntegerIsSupported() {
130 $this->createFakeExtension('
131 CREATE TABLE tx_test_dbal (
132 foo int default \'0\',
133 foobar bigint default \'0\'
134 );
135 ');
136 $data = array(
137 'foo' => -9.2233720368548E+18,
138 'foobar' => -9.2233720368548E+18
139 );
140 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTquery('tx_test_dbal', $data));
141 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'-9223372036854775808\', \'-9223372036854775808\' )';
142 $this->assertEquals($expected, $query);
143 }
144
145 /**
146 * @test
147 */
148 public function sqlForInsertWithMultipleRowsIsValid() {
149 $fields = array('uid', 'pid', 'title', 'body');
150 $rows = array(
151 array('1', '2', 'Title #1', 'Content #1'),
152 array('3', '4', 'Title #2', 'Content #2'),
153 array('5', '6', 'Title #3', 'Content #3')
154 );
155 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->INSERTmultipleRows('tt_content', $fields, $rows));
156 $expected = 'INSERT INTO tt_content (uid, pid, title, body) VALUES ';
157 $expected .= '(\'1\', \'2\', \'Title #1\', \'Content #1\'), ';
158 $expected .= '(\'3\', \'4\', \'Title #2\', \'Content #2\'), ';
159 $expected .= '(\'5\', \'6\', \'Title #3\', \'Content #3\')';
160 $this->assertEquals($expected, $query);
161 }
162
163 /**
164 * @test
165 * @see http://forge.typo3.org/issues/16708
166 */
167 public function minFunctionAndInOperatorCanBeParsed() {
168 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'pages', 'MIN(uid) IN (1,2,3,4)'));
169 $expected = 'SELECT * FROM pages WHERE MIN(uid) IN (1,2,3,4)';
170 $this->assertEquals($expected, $query);
171 }
172
173 /**
174 * @test
175 * @see http://forge.typo3.org/issues/16708
176 */
177 public function maxFunctionAndInOperatorCanBeParsed() {
178 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'pages', 'MAX(uid) IN (1,2,3,4)'));
179 $expected = 'SELECT * FROM pages WHERE MAX(uid) IN (1,2,3,4)';
180 $this->assertEquals($expected, $query);
181 }
182
183 /**
184 * @test
185 * @see http://forge.typo3.org/issues/21514
186 */
187 public function likeBinaryOperatorIsKept() {
188 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'tt_content', 'bodytext LIKE BINARY \'test\''));
189 $expected = 'SELECT * FROM tt_content WHERE bodytext LIKE BINARY \'test\'';
190 $this->assertEquals($expected, $query);
191 }
192
193 /**
194 * @test
195 * @see http://forge.typo3.org/issues/21514
196 */
197 public function notLikeBinaryOperatorIsKept() {
198 $query = $this->cleanSql($GLOBALS['TYPO3_DB']->SELECTquery('*', 'tt_content', 'bodytext NOT LIKE BINARY \'test\''));
199 $expected = 'SELECT * FROM tt_content WHERE bodytext NOT LIKE BINARY \'test\'';
200 $this->assertEquals($expected, $query);
201 }
202
203 ///////////////////////////////////////
204 // Tests concerning prepared queries
205 ///////////////////////////////////////
206 /**
207 * @test
208 * @see http://forge.typo3.org/issues/23374
209 */
210 public function similarNamedParametersAreProperlyReplaced() {
211 $sql = 'SELECT * FROM cache WHERE tag = :tag1 OR tag = :tag10 OR tag = :tag100';
212 $parameterValues = array(
213 ':tag1' => 'tag-one',
214 ':tag10' => 'tag-two',
215 ':tag100' => 'tag-three'
216 );
217 $className = self::buildAccessibleProxy('TYPO3\\CMS\\Core\\Database\\PreparedStatement');
218 $query = $sql;
219 $precompiledQueryParts = array();
220 $statement = new $className($sql, 'cache');
221 $statement->bindValues($parameterValues);
222 $parameters = $statement->_get('parameters');
223 $statement->_callRef('replaceValuesInQuery', $query, $precompiledQueryParts, $parameters);
224 $expected = 'SELECT * FROM cache WHERE tag = \'tag-one\' OR tag = \'tag-two\' OR tag = \'tag-three\'';
225 $this->assertEquals($expected, $query);
226 }
227
228 }