source: branches/version-2_13-dev/test/class/SC_Query_Test.php @ 22856

Revision 22856, 9.0 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

  • 主に空白・空白行の調整。もう少し整えたいが、一旦現状コミット。
  • 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     * 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    /**
173     * SC_Query::query() で INSERT を実行するテストケース.
174     */
175    function testQuery1()
176    {
177        $this->createTestTable();
178        $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)";
179        $data = array('1', '1', '1', 'f');
180
181        $this->objQuery->query($sql, $data);
182
183        $this->expected =  array(array('id' => '1',
184                                       'column1' => '1',
185                                       'column2' => '1',
186                                       'column3' => 'f'));
187
188        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
189
190        $this->verify();
191    }
192
193    function testInsert()
194    {
195        $this->createTestTable();
196
197        $this->objQuery->insert('test_table',
198                                array('id' => '1',
199                                      'column1' => '1',
200                                      'column2' => '1',
201                                      'column3' => 'f'));
202
203        $this->expected =  array(array('id' => '1',
204                                       'column1' => '1',
205                                       'column2' => '1',
206                                       'column3' => 'f'));
207
208        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
209
210        $this->verify();
211    }
212
213    /**
214     * SC_Query::query() で UPDATE を実行するテストケース.
215     */
216    function testQuery2()
217    {
218        $this->createTestTable();
219        $this->setTestData(1, '2', 'f');
220
221        $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?";
222        $data = array('2', '2', '1');
223
224        $this->objQuery->query($sql, $data);
225
226        $this->expected =  array(array('id' => '1',
227                                       'column1' => '2',
228                                       'column2' => '2',
229                                       'column3' => 'f'));
230
231        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
232
233        $this->verify();
234    }
235
236    function testUpdate()
237    {
238        $this->createTestTable();
239        $this->setTestData(1, '2', 'f');
240
241        $this->objQuery->update('test_table',
242                                array('id' => '1',
243                                      'column1' => '2',
244                                      'column2' => '2',
245                                      'column3' => 'f'),
246                                "id = ?", array(1));
247        $this->expected =  array(array('id' => '1',
248                                       'column1' => '2',
249                                       'column2' => '2',
250                                       'column3' => 'f'));
251
252        $this->actual = $this->objQuery->getAll("SELECT * FROM test_table");
253
254        $this->verify();
255    }
256
257    function testListTables()
258    {
259        $tables = $this->objQuery->listTables();
260        $this->assertTrue(in_array('mtb_zip', $tables));
261    }
262
263    function testListSequences()
264    {
265        $sequences = $this->objQuery->listSequences();
266        $this->assertTrue(in_array('dtb_products_product_id', $sequences));
267    }
268
269    function testListTableFields()
270    {
271        $this->expected = array('id', 'name', 'rank', 'remarks');
272        $this->actual = $this->objQuery->listTableFields('mtb_constants');
273        $this->verify();
274    }
275
276    function testListTableIndexes()
277    {
278        $indexes = $this->objQuery->listTableIndexes('dtb_mobile_kara_mail');
279        $this->assertTrue(in_array('dtb_mobile_kara_mail_create_date_key', $indexes));
280    }
281
282    function createTestTable()
283    {
284        $sql = "CREATE TABLE test_table ("
285            . "id SERIAL PRIMARY KEY,"
286            . "column1 numeric(9),"
287            . "column2 varchar(20),"
288            . "column3 char(1)"
289            . ")";
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
301        return;
302    }
303
304    function setTestData($column1, $column2, $column3)
305    {
306        $fields_values = array($column1, $column2, $column3);
307        $sql = "INSERT INTO test_table (column1, column2, column3) VALUES (?, ?, ?)";
308        $result = $this->objQuery->query($sql, $fields_values);
309        if (PEAR::isError($result)) {
310            var_dump($result);
311        }
312
313        return $result;
314    }
315}
316?>
Note: See TracBrowser for help on using the repository browser.