Fixed variable phpDoc for fixture
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / tests / sqlparser_general_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Xavier Perseguers <typo3@perseguers.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 require_once('BaseTestCase.php');
27
28 /**
29 * Testcase for class ux_t3lib_sqlparser
30 *
31 * $Id$
32 *
33 * @author Xavier Perseguers <typo3@perseguers.ch>
34 *
35 * @package TYPO3
36 * @subpackage dbal
37 */
38 class sqlparser_general_testcase extends BaseTestCase {
39
40 /**
41 * @var ux_t3lib_sqlparser (extended to make protected methods public)
42 */
43 protected $fixture;
44
45 /**
46 * Prepares the environment before running a test.
47 */
48 public function setUp() {
49 $className = self::buildAccessibleProxy('ux_t3lib_sqlparser');
50 $this->fixture = new $className;
51 }
52
53 /**
54 * Cleans up the environment after running a test.
55 */
56 public function tearDown() {
57 unset($this->fixture);
58 }
59
60 /**
61 * Cleans a SQL query.
62 *
63 * @param mixed $sql
64 * @return mixed (string or array)
65 */
66 private function cleanSql($sql) {
67 if (!is_string($sql)) {
68 return $sql;
69 }
70
71 $sql = str_replace("\n", ' ', $sql);
72 $sql = preg_replace('/\s+/', ' ', $sql);
73 return $sql;
74 }
75
76 /**
77 * @test
78 */
79 public function canExtractPartsOfAQuery() {
80 $parseString = "SELECT *\nFROM pages WHERE pid IN (1,2,3,4)";
81 $regex = '^SELECT[[:space:]]+(.*)[[:space:]]+';
82 $trimAll = TRUE;
83 $fields = $this->fixture->_callRef('nextPart', $parseString, $regex, $trimAll);
84
85 $this->assertEquals(
86 '*',
87 $fields
88 );
89 $this->assertEquals(
90 'FROM pages WHERE pid IN (1,2,3,4)',
91 $parseString
92 );
93
94 $regex = '^FROM ([^)]+) WHERE';
95 $table = $this->fixture->_callRef('nextPart', $parseString, $regex);
96
97 $this->assertEquals(
98 'pages',
99 $table
100 );
101 $this->assertEquals(
102 'pages WHERE pid IN (1,2,3,4)',
103 $parseString
104 );
105 }
106
107 /**
108 * @test
109 */
110 public function canGetIntegerValue() {
111 $parseString = '1024';
112 $value = $this->fixture->_callRef('getValue', $parseString);
113 $expected = array(1024);
114
115 $this->assertEquals($expected, $value);
116 }
117
118 /**
119 * @test
120 */
121 public function canGetStringValue() {
122 $parseString = '"some owner\\\' string"';
123 $value = $this->fixture->_callRef('getValue', $parseString);
124 $expected = array('some owner\' string', '"');
125
126 $this->assertEquals($expected, $value);
127 }
128
129 /**
130 * @test
131 */
132 public function canGetListOfValues() {
133 $parseString = '( 1, 2, 3 ,4)';
134 $operator = 'IN';
135 $values = $this->fixture->_callRef('getValue', $parseString, $operator);
136 $expected = array(
137 array(1),
138 array(2),
139 array(3),
140 array(4)
141 );
142
143 $this->assertEquals($expected, $values);
144 }
145
146 /**
147 * @test
148 */
149 public function parseFromTablesWithInnerJoinReturnsArray() {
150 $parseString = 'be_users INNER JOIN pages ON pages.cruser_id = be_users.uid';
151 $tables = $this->fixture->parseFromTables($parseString);
152
153 $this->assertTrue(is_array($tables), $tables);
154 $this->assertTrue(empty($parseString), 'parseString is not empty');
155 }
156
157 /**
158 * @test
159 */
160 public function parseFromTablesWithLeftOuterJoinReturnsArray() {
161 $parseString = 'be_users LEFT OUTER JOIN pages ON be_users.uid = pages.cruser_id';
162 $tables = $this->fixture->parseFromTables($parseString);
163
164 $this->assertTrue(is_array($tables), $tables);
165 $this->assertTrue(empty($parseString), 'parseString is not empty');
166 }
167
168 /**
169 * @test
170 */
171 public function parseFromTablesWithMultipleJoinsReturnsArray() {
172 $parseString = 'be_users LEFT OUTER JOIN pages ON be_users.uid = pages.cruser_id INNER JOIN cache_pages cp ON cp.page_id = pages.uid';
173 $tables = $this->fixture->parseFromTables($parseString);
174
175 $this->assertTrue(is_array($tables), $tables);
176 $this->assertTrue(empty($parseString), 'parseString is not empty');
177 }
178
179 /**
180 * @test
181 */
182 public function parseWhereClauseReturnsArray() {
183 $parseString = 'uid IN (1,2) AND (starttime < ' . time() . ' OR cruser_id + 10 < 20)';
184 $where = $this->fixture->parseWhereClause($parseString);
185
186 $this->assertTrue(is_array($where), $where);
187 $this->assertTrue(empty($parseString), 'parseString is not empty');
188 }
189
190 /**
191 * @test
192 */
193 public function canSelectAllFieldsFromPages() {
194 $sql = 'SELECT * FROM pages';
195 $expected = $sql;
196 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
197
198 $this->assertEquals($expected, $actual);
199 }
200
201 /**
202 * @test
203 */
204 public function canUseInnerJoinInSelect() {
205 $sql = 'SELECT pages.uid, be_users.username FROM be_users INNER JOIN pages ON pages.cruser_id = be_users.uid';
206 $expected = 'SELECT pages.uid, be_users.username FROM be_users INNER JOIN pages ON pages.cruser_id=be_users.uid';
207 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
208
209 $this->assertEquals($expected, $actual);
210 }
211
212 /**
213 * @test
214 */
215 public function canUseMultipleInnerJoinsInSelect() {
216 $sql = 'SELECT * FROM tt_news_cat INNER JOIN tt_news_cat_mm ON tt_news_cat.uid = tt_news_cat_mm.uid_foreign INNER JOIN tt_news ON tt_news.uid = tt_news_cat_mm.uid_local';
217 $expected = 'SELECT * FROM tt_news_cat INNER JOIN tt_news_cat_mm ON tt_news_cat.uid=tt_news_cat_mm.uid_foreign INNER JOIN tt_news ON tt_news.uid=tt_news_cat_mm.uid_local';
218 $actual = $this->cleanSql($this->fixture->debug_testSQL($sql));
219
220 $this->assertEquals($expected, $actual);
221 }
222
223 }
224 ?>