source: branches/version-2_13_3/test/class/SC_Query_Test.php @ 23546

Revision 23546, 9.0 KB checked in by shutta, 12 years ago (diff)

#2580 Copyrightを更新
Copyrightを2014に更新

  • 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-2014 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     * SC_Query::getOne() のテストケース.
126     */
127    function testGetOne()
128    {
129        $this->createTestTable();
130        $this->setTestData(1, '2', 'f');
131        $this->setTestData(1, '2', 'f');
132        $this->setTestData(1, '2', 'f');
133
134        $this->expected = 3;
135        $this->actual = $this->objQuery->getOne("SELECT COUNT(*) FROM test_table");
136
137        $this->verify();
138    }
139
140    /**
141     * SC_Query::getRow() のテストケース.
142     */
143    function testGetRow()
144    {
145        $this->createTestTable();
146        $this->setTestData(1, '1', 'f');
147        $this->setTestData(2, '2', 'f');
148        $this->setTestData(3, '3', 'f');
149
150        $this->expected = array('column1' => 1, 'column2' => 1);
151        $this->actual = $this->objQuery->getRow("column1, column2", 'test_table', "id = ?", array(1));
152        $this->verify();
153    }
154
155    /**
156     * SC_Query::getCol() のテストケース.
157     */
158    function testGetCol()
159    {
160        $this->createTestTable();
161        $this->setTestData(1, '1', 'f');
162        $this->setTestData(2, '2', 'f');
163        $this->setTestData(3, '3', 'f');
164
165        $this->expected = array(1, 2);
166        $this->actual = $this->objQuery->getCol('column1', 'test_table', "id < ?",  array(3));
167
168        $this->verify();
169    }
170
171    /**
172     * SC_Query::query() で INSERT を実行するテストケース.
173     */
174    function testQuery1()
175    {
176        $this->createTestTable();
177        $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)";
178        $data = array('1', '1', '1', 'f');
179
180        $this->objQuery->query($sql, $data);
181
182        $this->expected =  array(array('id' => '1',
183                                       'column1' => '1',
184                                       'column2' => '1',
185                                       'column3' => 'f'));
186
187        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
188
189        $this->verify();
190    }
191
192    function testInsert()
193    {
194        $this->createTestTable();
195
196        $this->objQuery->insert('test_table',
197                                array('id' => '1',
198                                      'column1' => '1',
199                                      'column2' => '1',
200                                      'column3' => 'f'));
201
202        $this->expected =  array(array('id' => '1',
203                                       'column1' => '1',
204                                       'column2' => '1',
205                                       'column3' => 'f'));
206
207        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
208
209        $this->verify();
210    }
211
212    /**
213     * SC_Query::query() で UPDATE を実行するテストケース.
214     */
215    function testQuery2()
216    {
217        $this->createTestTable();
218        $this->setTestData(1, '2', 'f');
219
220        $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?";
221        $data = array('2', '2', '1');
222
223        $this->objQuery->query($sql, $data);
224
225        $this->expected =  array(array('id' => '1',
226                                       'column1' => '2',
227                                       'column2' => '2',
228                                       'column3' => 'f'));
229
230        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
231
232        $this->verify();
233    }
234
235    function testUpdate()
236    {
237        $this->createTestTable();
238        $this->setTestData(1, '2', 'f');
239
240        $this->objQuery->update('test_table',
241                                array('id' => '1',
242                                      'column1' => '2',
243                                      'column2' => '2',
244                                      'column3' => 'f'),
245                                "id = ?", array(1));
246        $this->expected =  array(array('id' => '1',
247                                       'column1' => '2',
248                                       'column2' => '2',
249                                       'column3' => 'f'));
250
251        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
252
253        $this->verify();
254    }
255
256    function testListTables()
257    {
258        $tables = $this->objQuery->listTables();
259        $this->assertTrue(in_array('mtb_zip', $tables));
260    }
261
262    function testListSequences()
263    {
264        $sequences = $this->objQuery->listSequences();
265        $this->assertTrue(in_array('dtb_products_product_id', $sequences));
266    }
267
268    function testListTableFields()
269    {
270        $this->expected = array('id', 'name', 'rank', 'remarks');
271        $this->actual = $this->objQuery->listTableFields('mtb_constants');
272        $this->verify();
273    }
274
275    function testListTableIndexes()
276    {
277        $indexes = $this->objQuery->listTableIndexes('dtb_mobile_kara_mail');
278        $this->assertTrue(in_array('dtb_mobile_kara_mail_create_date_key', $indexes));
279    }
280
281    function createTestTable()
282    {
283        $sql = "CREATE TABLE test_table ("
284            . "id SERIAL PRIMARY KEY,"
285            . "column1 numeric(9),"
286            . "column2 varchar(20),"
287            . "column3 char(1)"
288            . ")";
289
290        return $this->objQuery->query($sql);
291    }
292
293    function dropTestTable()
294    {
295        $tables = $this->objQuery->listTables();
296        if (in_array('test_table', $tables)) {
297            $this->objQuery->query("DROP TABLE test_table");
298        }
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
312        return $result;
313    }
314}
315?>
Note: See TracBrowser for help on using the repository browser.