1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2010 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 | $this->objQuery->rollback(); |
---|
50 | $this->objQuery = null; |
---|
51 | } |
---|
52 | |
---|
53 | function verify() { |
---|
54 | $this->assertEquals($this->expected, $this->actual); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * インスタンスを取得するテストケース. |
---|
59 | */ |
---|
60 | function testGetInstance() { |
---|
61 | $this->expected = true; |
---|
62 | $this->actual = is_object($this->objQuery); |
---|
63 | |
---|
64 | $this->verify(); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * SC_Query::query() を使用して, CREATE TABLE を実行するテストケース. |
---|
69 | */ |
---|
70 | function testCreateTable() { |
---|
71 | $result = $this->createTestTable(); |
---|
72 | |
---|
73 | $this->expected = false; |
---|
74 | $this->actual = PEAR::isError($result); |
---|
75 | |
---|
76 | $this->verify(); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * SC_Query::getAll() のテストケース. |
---|
81 | */ |
---|
82 | function testGetAll() { |
---|
83 | $this->createTestTable(); |
---|
84 | $result = $this->setTestData(1, "2", "f"); |
---|
85 | |
---|
86 | $this->expected = array(array("id" => "1", |
---|
87 | "column1" => "1", |
---|
88 | "column2" => "2", |
---|
89 | "column3" => "f")); |
---|
90 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table WHERE id = ?", array(1)); |
---|
91 | |
---|
92 | $this->verify(); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * SC_Query::getOne() のテストケース. |
---|
97 | */ |
---|
98 | function testGetOne() { |
---|
99 | $this->createTestTable(); |
---|
100 | $this->setTestData(1, "2", "f"); |
---|
101 | $this->setTestData(1, "2", "f"); |
---|
102 | $this->setTestData(1, "2", "f"); |
---|
103 | |
---|
104 | $this->expected = 3; |
---|
105 | $this->actual = $this->objQuery->getOne("SELECT COUNT(*) FROM test_table"); |
---|
106 | |
---|
107 | $this->verify(); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * SC_Query::getRow() のテストケース. |
---|
112 | */ |
---|
113 | function testGetRow() { |
---|
114 | $this->createTestTable(); |
---|
115 | $this->setTestData(1, "1", "f"); |
---|
116 | $this->setTestData(2, "2", "f"); |
---|
117 | $this->setTestData(3, "3", "f"); |
---|
118 | |
---|
119 | $this->expected = array("column1" => 1, "column2" => 1); |
---|
120 | $this->actual = $this->objQuery->getRow("test_table", "column1, column2", "id = ?", array(1)); |
---|
121 | $this->verify(); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * SC_Query::getCol() のテストケース. |
---|
126 | */ |
---|
127 | function testGetCol() { |
---|
128 | $this->createTestTable(); |
---|
129 | $this->setTestData(1, "1", "f"); |
---|
130 | $this->setTestData(2, "2", "f"); |
---|
131 | $this->setTestData(3, "3", "f"); |
---|
132 | |
---|
133 | $this->expected = array(1, 2); |
---|
134 | $this->actual = $this->objQuery->getCol("test_table", "column1", "id < ?", array(3)); |
---|
135 | |
---|
136 | $this->verify(); |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * SC_Query::query() で INSERT を実行するテストケース. |
---|
142 | */ |
---|
143 | function testQuery1() { |
---|
144 | $this->createTestTable(); |
---|
145 | $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)"; |
---|
146 | $data = array("1", "1", "1", "f"); |
---|
147 | |
---|
148 | $this->objQuery->query($sql, $data); |
---|
149 | |
---|
150 | $this->expected = array(array("id" => "1", |
---|
151 | "column1" => "1", |
---|
152 | "column2" => "1", |
---|
153 | "column3" => "f")); |
---|
154 | |
---|
155 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
156 | |
---|
157 | $this->verify(); |
---|
158 | } |
---|
159 | |
---|
160 | function testInsert() { |
---|
161 | $this->createTestTable(); |
---|
162 | |
---|
163 | $this->objQuery->insert("test_table", |
---|
164 | array("id" => "1", |
---|
165 | "column1" => "1", |
---|
166 | "column2" => "1", |
---|
167 | "column3" => "f")); |
---|
168 | |
---|
169 | $this->expected = array(array("id" => "1", |
---|
170 | "column1" => "1", |
---|
171 | "column2" => "1", |
---|
172 | "column3" => "f")); |
---|
173 | |
---|
174 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
175 | |
---|
176 | $this->verify(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * SC_Query::query() で UPDATE を実行するテストケース. |
---|
181 | */ |
---|
182 | function testQuery2() { |
---|
183 | $this->createTestTable(); |
---|
184 | $this->setTestData(1, "2", "f"); |
---|
185 | |
---|
186 | $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?"; |
---|
187 | $data = array("2", "2", "1"); |
---|
188 | |
---|
189 | $this->objQuery->query($sql, $data); |
---|
190 | |
---|
191 | $this->expected = array(array("id" => "1", |
---|
192 | "column1" => "2", |
---|
193 | "column2" => "2", |
---|
194 | "column3" => "f")); |
---|
195 | |
---|
196 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
197 | |
---|
198 | $this->verify(); |
---|
199 | } |
---|
200 | |
---|
201 | function testUpdate() { |
---|
202 | $this->createTestTable(); |
---|
203 | $this->setTestData(1, "2", "f"); |
---|
204 | |
---|
205 | $this->objQuery->update("test_table", |
---|
206 | array("id" => "1", |
---|
207 | "column1" => "2", |
---|
208 | "column2" => "2", |
---|
209 | "column3" => "f"), |
---|
210 | "id = ?", array(1)); |
---|
211 | $this->expected = array(array("id" => "1", |
---|
212 | "column1" => "2", |
---|
213 | "column2" => "2", |
---|
214 | "column3" => "f")); |
---|
215 | |
---|
216 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
---|
217 | |
---|
218 | $this->verify(); |
---|
219 | } |
---|
220 | |
---|
221 | function createTestTable() { |
---|
222 | $sql = "CREATE TABLE test_table (" |
---|
223 | . "id SERIAL PRIMARY KEY," |
---|
224 | . "column1 numeric(9)," |
---|
225 | . "column2 varchar(20)," |
---|
226 | . "column3 char(1)" |
---|
227 | . ")"; |
---|
228 | return $this->objQuery->query($sql); |
---|
229 | } |
---|
230 | |
---|
231 | function setTestData($column1, $column2, $column3) { |
---|
232 | $fields_values = array($column1, $column2, $column3); |
---|
233 | $sql = "INSERT INTO test_table (column1, column2, column3) VALUES (?, ?, ?)"; |
---|
234 | $result = $this->objQuery->query($sql, $fields_values); |
---|
235 | if (PEAR::isError($result)) { |
---|
236 | var_dump($result); |
---|
237 | } |
---|
238 | return $result; |
---|
239 | } |
---|
240 | } |
---|
241 | ?> |
---|