source: branches/version-2_12-dev/test/class/SC_Query_Test.php @ 22567

Revision 22567, 9.0 KB checked in by shutta, 13 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 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 は, できるだけ単独で動くようにしたい
26require_once(realpath(dirname(__FILE__)) . "/../require.php");
27require_once(realpath(dirname(__FILE__)) . "/../../data/class/SC_Query.php");
28
29/**
30 * SC_Query のテストケース.
31 *
32 * @author Kentaro Ohkouchi
33 * @version $Id$
34 */
35class SC_Query_Test extends PHPUnit_Framework_TestCase
36{
37
38    /** SC_Query インスタンス */
39    var $objQuery;
40
41    var $expected;
42    var $actual;
43
44    function setUp()
45    {
46        $this->objQuery = new SC_Query();
47        $this->objQuery->begin();
48    }
49
50    function tearDown()
51    {
52        // MySQL では CREATE TABLE がロールバックされないので DROP TABLE を行う
53        $this->dropTestTable();
54        $this->objQuery->rollback();
55        $this->objQuery = null;
56    }
57
58    function verify()
59    {
60        $this->assertEquals($this->expected, $this->actual);
61    }
62
63    /**
64     * インスタンスを取得するテストケース.
65     */
66    function testGetInstance()
67    {
68        $this->expected = true;
69        $this->actual = is_object($this->objQuery);
70
71        $this->verify();
72    }
73
74    /**
75     * SC_Query::query() を使用して, CREATE TABLE を実行するテストケース.
76     */
77    function testCreateTable()
78    {
79        $result = $this->createTestTable();
80
81        $this->expected = false;
82        $this->actual = PEAR::isError($result);
83
84        $this->verify();
85    }
86
87    /**
88     * SC_Query::getAll() のテストケース.
89     */
90    function testGetAll()
91    {
92        $result = $this->createTestTable();
93        $result = $this->setTestData(1, '2', 'f');
94
95        $this->expected =  array(array('id' => '1',
96                                       'column1' => '1',
97                                       'column2' => '2',
98                                       'column3' => 'f'));
99        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table WHERE id = ?", array(1));
100
101        $this->verify();
102    }
103
104    /**
105     * SC_Query::select() のテストケース.
106     */
107    function testSelect()
108    {
109        $this->createTestTable();
110        $result = $this->setTestData(1, '2', 'f');
111
112        $this->expected =  array(array('id' => '1',
113                                       'column1' => '1',
114                                       'column2' => '2',
115                                       'column3' => 'f'));
116
117        $this->actual = $this->objQuery->setWhere("id = ?")
118                                       ->setOrder('id')
119                                       ->select("*", 'test_table', "", array(1));
120
121        $this->verify();
122    }
123
124
125    /**
126     * SC_Query::getOne() のテストケース.
127     */
128    function testGetOne()
129    {
130        $this->createTestTable();
131        $this->setTestData(1, '2', 'f');
132        $this->setTestData(1, '2', 'f');
133        $this->setTestData(1, '2', 'f');
134
135        $this->expected = 3;
136        $this->actual = $this->objQuery->getOne("SELECT COUNT(*) FROM test_table");
137
138        $this->verify();
139    }
140
141    /**
142     * SC_Query::getRow() のテストケース.
143     */
144    function testGetRow()
145    {
146        $this->createTestTable();
147        $this->setTestData(1, '1', 'f');
148        $this->setTestData(2, '2', 'f');
149        $this->setTestData(3, '3', 'f');
150
151        $this->expected = array('column1' => 1, 'column2' => 1);
152        $this->actual = $this->objQuery->getRow("column1, column2", 'test_table', "id = ?", array(1));
153        $this->verify();
154    }
155
156    /**
157     * SC_Query::getCol() のテストケース.
158     */
159    function testGetCol()
160    {
161        $this->createTestTable();
162        $this->setTestData(1, '1', 'f');
163        $this->setTestData(2, '2', 'f');
164        $this->setTestData(3, '3', 'f');
165
166        $this->expected = array(1, 2);
167        $this->actual = $this->objQuery->getCol('column1', 'test_table', "id < ?",  array(3));
168
169        $this->verify();
170
171    }
172
173    /**
174     * SC_Query::query() で INSERT を実行するテストケース.
175     */
176    function testQuery1()
177    {
178        $this->createTestTable();
179        $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)";
180        $data = array('1', '1', '1', 'f');
181
182        $this->objQuery->query($sql, $data);
183
184        $this->expected =  array(array('id' => '1',
185                                       'column1' => '1',
186                                       'column2' => '1',
187                                       'column3' => 'f'));
188
189        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
190
191        $this->verify();
192    }
193
194    function testInsert()
195    {
196        $this->createTestTable();
197
198        $this->objQuery->insert('test_table',
199                                array('id' => '1',
200                                      'column1' => '1',
201                                      'column2' => '1',
202                                      'column3' => 'f'));
203
204        $this->expected =  array(array('id' => '1',
205                                       'column1' => '1',
206                                       'column2' => '1',
207                                       'column3' => 'f'));
208
209        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
210
211        $this->verify();
212    }
213
214    /**
215     * SC_Query::query() で UPDATE を実行するテストケース.
216     */
217    function testQuery2()
218    {
219        $this->createTestTable();
220        $this->setTestData(1, '2', 'f');
221
222        $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?";
223        $data = array('2', '2', '1');
224
225        $this->objQuery->query($sql, $data);
226
227        $this->expected =  array(array('id' => '1',
228                                       'column1' => '2',
229                                       'column2' => '2',
230                                       'column3' => 'f'));
231
232        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
233
234        $this->verify();
235    }
236
237    function testUpdate()
238    {
239        $this->createTestTable();
240        $this->setTestData(1, '2', 'f');
241
242        $this->objQuery->update('test_table',
243                                array('id' => '1',
244                                      'column1' => '2',
245                                      'column2' => '2',
246                                      'column3' => 'f'),
247                                "id = ?", array(1));
248        $this->expected =  array(array('id' => '1',
249                                       'column1' => '2',
250                                       'column2' => '2',
251                                       'column3' => 'f'));
252
253        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
254
255        $this->verify();
256    }
257
258    function testListTables()
259    {
260        $tables = $this->objQuery->listTables();
261        $this->assertTrue(in_array('mtb_zip', $tables));
262    }
263
264    function testListSequences()
265    {
266        $sequences = $this->objQuery->listSequences();
267        $this->assertTrue(in_array('dtb_products_product_id', $sequences));
268    }
269
270    function testListTableFields()
271    {
272        $this->expected = array('id', 'name', 'rank', 'remarks');
273        $this->actual = $this->objQuery->listTableFields('mtb_constants');
274        $this->verify();
275    }
276
277    function testListTableIndexes()
278    {
279        $indexes = $this->objQuery->listTableIndexes('dtb_mobile_kara_mail');
280        $this->assertTrue(in_array('dtb_mobile_kara_mail_create_date_key', $indexes));
281    }
282
283    function createTestTable()
284    {
285        $sql = "CREATE TABLE test_table ("
286            . "id SERIAL PRIMARY KEY,"
287            . "column1 numeric(9),"
288            . "column2 varchar(20),"
289            . "column3 char(1)"
290            . ")";
291        return $this->objQuery->query($sql);
292    }
293
294    function dropTestTable()
295    {
296        $tables = $this->objQuery->listTables();
297        if (in_array('test_table', $tables)) {
298            $this->objQuery->query("DROP TABLE test_table");
299        }
300        return;
301    }
302
303    function setTestData($column1, $column2, $column3)
304    {
305        $fields_values = array($column1, $column2, $column3);
306        $sql = "INSERT INTO test_table (column1, column2, column3) VALUES (?, ?, ?)";
307        $result = $this->objQuery->query($sql, $fields_values);
308        if (PEAR::isError($result)) {
309            var_dump($result);
310        }
311        return $result;
312    }
313}
314?>
Note: See TracBrowser for help on using the repository browser.