1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // {{{ requires |
---|
25 | // TODO SC_Query は, できるだけ単独で動くようにしたい |
---|
26 | require_once(realpath(dirname(__FILE__)) . "/../require.php"); |
---|
27 | require_once(realpath(dirname(__FILE__)) . "/../../data/class/SC_Query.php"); |
---|
28 | |
---|
29 | /** |
---|
30 | * SC_Query のテストケース. |
---|
31 | * |
---|
32 | * @author Kentaro Ohkouchi |
---|
33 | * @version $Id$ |
---|
34 | */ |
---|
35 | class SC_Query_Test extends PHPUnit_Framework_TestCase { |
---|
36 | |
---|
37 | /** SC_Query インスタンス */ |
---|
38 | var $objQuery; |
---|
39 | |
---|
40 | var $expected; |
---|
41 | var $actual; |
---|
42 | |
---|
43 | function setUp() { |
---|
44 | $this->objQuery = new SC_Query(); |
---|
45 | $this->objQuery->begin(); |
---|
46 | } |
---|
47 | |
---|
48 | function tearDown() { |
---|
49 | // MySQL では CREATE TABLE がロールバックされないので DROP TABLE を行う |
---|
50 | $this->dropTestTable(); |
---|
51 | $this->objQuery->rollback(); |
---|
52 | $this->objQuery = null; |
---|
53 | } |
---|
54 | |
---|
55 | function verify() { |
---|
56 | $this->assertEquals($this->expected, $this->actual); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * インスタンスを取得するテストケース. |
---|
61 | */ |
---|
62 | function testGetInstance() { |
---|
63 | $this->expected = true; |
---|
64 | $this->actual = is_object($this->objQuery); |
---|
65 | |
---|
66 | $this->verify(); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * SC_Query::query() を使用して, CREATE TABLE を実行するテストケース. |
---|
71 | */ |
---|
72 | function testCreateTable() { |
---|
73 | $result = $this->createTestTable(); |
---|
74 | |
---|
75 | $this->expected = false; |
---|
76 | $this->actual = PEAR::isError($result); |
---|
77 | |
---|
78 | $this->verify(); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * SC_Query::getAll() のテストケース. |
---|
83 | */ |
---|
84 | function testGetAll() { |
---|
85 | $result = $this->createTestTable(); |
---|
86 | $result = $this->setTestData(1, '2', 'f'); |
---|
87 | |
---|
88 | $this->expected = array(array('id' => '1', |
---|
89 | 'column1' => '1', |
---|
90 | 'column2' => '2', |
---|
91 | 'column3' => 'f')); |
---|
92 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table WHERE id = ?", array(1)); |
---|
93 | |
---|
94 | $this->verify(); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * SC_Query::select() のテストケース. |
---|
99 | */ |
---|
100 | function testSelect() { |
---|
101 | $this->createTestTable(); |
---|
102 | $result = $this->setTestData(1, '2', 'f'); |
---|
103 | |
---|
104 | $this->expected = array(array('id' => '1', |
---|
105 | 'column1' => '1', |
---|
106 | 'column2' => '2', |
---|
107 | 'column3' => 'f')); |
---|
108 | |
---|
109 | $this->actual = $this->objQuery->setWhere("id = ?") |
---|
110 | ->setOrder('id') |
---|
111 | ->select("*", 'test_table', "", array(1)); |
---|
112 | |
---|
113 | $this->verify(); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * SC_Query::getOne() のテストケース. |
---|
119 | */ |
---|
120 | function testGetOne() { |
---|
121 | $this->createTestTable(); |
---|
122 | $this->setTestData(1, '2', 'f'); |
---|
123 | $this->setTestData(1, '2', 'f'); |
---|
124 | $this->setTestData(1, '2', 'f'); |
---|
125 | |
---|
126 | $this->expected = 3; |
---|
127 | $this->actual = $this->objQuery->getOne("SELECT COUNT(*) FROM test_table"); |
---|
128 | |
---|
129 | $this->verify(); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * SC_Query::getRow() のテストケース. |
---|
134 | */ |
---|
135 | function testGetRow() { |
---|
136 | $this->createTestTable(); |
---|
137 | $this->setTestData(1, '1', 'f'); |
---|
138 | $this->setTestData(2, '2', 'f'); |
---|
139 | $this->setTestData(3, '3', 'f'); |
---|
140 | |
---|
141 | $this->expected = array('column1' => 1, 'column2' => 1); |
---|
142 | $this->actual = $this->objQuery->getRow("column1, column2", 'test_table', "id = ?", array(1)); |
---|
143 | $this->verify(); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * SC_Query::getCol() のテストケース. |
---|
148 | */ |
---|
149 | function testGetCol() { |
---|
150 | $this->createTestTable(); |
---|
151 | $this->setTestData(1, '1', 'f'); |
---|
152 | $this->setTestData(2, '2', 'f'); |
---|
153 | $this->setTestData(3, '3', 'f'); |
---|
154 | |
---|
155 | $this->expected = array(1, 2); |
---|
156 | $this->actual = $this->objQuery->getCol('column1', 'test_table', "id < ?", array(3)); |
---|
157 | |
---|
158 | $this->verify(); |
---|
159 | |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * SC_Query::query() で INSERT を実行するテストケース. |
---|
164 | */ |
---|
165 | function testQuery1() { |
---|
166 | $this->createTestTable(); |
---|
167 | $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)"; |
---|
168 | $data = array('1', '1', '1', 'f'); |
---|
169 | |
---|
170 | $this->objQuery->query($sql, $data); |
---|
171 | |
---|
172 | $this->expected = array(array('id' => '1', |
---|
173 | 'column1' => '1', |
---|
174 | 'column2' => '1', |
---|
175 | 'column3' => 'f')); |
---|
176 | |
---|
177 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
178 | |
---|
179 | $this->verify(); |
---|
180 | } |
---|
181 | |
---|
182 | function testInsert() { |
---|
183 | $this->createTestTable(); |
---|
184 | |
---|
185 | $this->objQuery->insert('test_table', |
---|
186 | array('id' => '1', |
---|
187 | 'column1' => '1', |
---|
188 | 'column2' => '1', |
---|
189 | 'column3' => 'f')); |
---|
190 | |
---|
191 | $this->expected = array(array('id' => '1', |
---|
192 | 'column1' => '1', |
---|
193 | 'column2' => '1', |
---|
194 | 'column3' => 'f')); |
---|
195 | |
---|
196 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
197 | |
---|
198 | $this->verify(); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * SC_Query::query() で UPDATE を実行するテストケース. |
---|
203 | */ |
---|
204 | function testQuery2() { |
---|
205 | $this->createTestTable(); |
---|
206 | $this->setTestData(1, '2', 'f'); |
---|
207 | |
---|
208 | $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?"; |
---|
209 | $data = array('2', '2', '1'); |
---|
210 | |
---|
211 | $this->objQuery->query($sql, $data); |
---|
212 | |
---|
213 | $this->expected = array(array('id' => '1', |
---|
214 | 'column1' => '2', |
---|
215 | 'column2' => '2', |
---|
216 | 'column3' => 'f')); |
---|
217 | |
---|
218 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
219 | |
---|
220 | $this->verify(); |
---|
221 | } |
---|
222 | |
---|
223 | function testUpdate() { |
---|
224 | $this->createTestTable(); |
---|
225 | $this->setTestData(1, '2', 'f'); |
---|
226 | |
---|
227 | $this->objQuery->update('test_table', |
---|
228 | array('id' => '1', |
---|
229 | 'column1' => '2', |
---|
230 | 'column2' => '2', |
---|
231 | 'column3' => 'f'), |
---|
232 | "id = ?", array(1)); |
---|
233 | $this->expected = array(array('id' => '1', |
---|
234 | 'column1' => '2', |
---|
235 | 'column2' => '2', |
---|
236 | 'column3' => 'f')); |
---|
237 | |
---|
238 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
239 | |
---|
240 | $this->verify(); |
---|
241 | } |
---|
242 | |
---|
243 | function testListTables() { |
---|
244 | $tables = $this->objQuery->listTables(); |
---|
245 | $this->assertTrue(in_array('mtb_zip', $tables)); |
---|
246 | } |
---|
247 | |
---|
248 | function testListSequences() { |
---|
249 | $sequences = $this->objQuery->listSequences(); |
---|
250 | $this->assertTrue(in_array('dtb_products_product_id', $sequences)); |
---|
251 | } |
---|
252 | |
---|
253 | function testListTableFields() { |
---|
254 | $this->expected = array('id', 'name', 'rank', 'remarks'); |
---|
255 | $this->actual = $this->objQuery->listTableFields('mtb_constants'); |
---|
256 | $this->verify(); |
---|
257 | } |
---|
258 | |
---|
259 | function testListTableIndexes() { |
---|
260 | $indexes = $this->objQuery->listTableIndexes('dtb_mobile_kara_mail'); |
---|
261 | $this->assertTrue(in_array('dtb_mobile_kara_mail_create_date_key', $indexes)); |
---|
262 | } |
---|
263 | |
---|
264 | function createTestTable() { |
---|
265 | $sql = "CREATE TABLE test_table (" |
---|
266 | . "id SERIAL PRIMARY KEY," |
---|
267 | . "column1 numeric(9)," |
---|
268 | . "column2 varchar(20)," |
---|
269 | . "column3 char(1)" |
---|
270 | . ")"; |
---|
271 | return $this->objQuery->query($sql); |
---|
272 | } |
---|
273 | |
---|
274 | function dropTestTable() { |
---|
275 | $tables = $this->objQuery->listTables(); |
---|
276 | if (in_array('test_table', $tables)) { |
---|
277 | $this->objQuery->query("DROP TABLE test_table"); |
---|
278 | } |
---|
279 | return; |
---|
280 | } |
---|
281 | |
---|
282 | function setTestData($column1, $column2, $column3) { |
---|
283 | $fields_values = array($column1, $column2, $column3); |
---|
284 | $sql = "INSERT INTO test_table (column1, column2, column3) VALUES (?, ?, ?)"; |
---|
285 | $result = $this->objQuery->query($sql, $fields_values); |
---|
286 | if (PEAR::isError($result)) { |
---|
287 | var_dump($result); |
---|
288 | } |
---|
289 | return $result; |
---|
290 | } |
---|
291 | } |
---|
292 | ?> |
---|