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

Revision 22856, 8.4 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
25require_once(realpath(dirname(__FILE__)) . "/../require.php");
26require_once(realpath(dirname(__FILE__)) . "/../../data/class/SC_DbConn.php");
27
28/**
29 * SC_DbConn のテストケース.
30 *
31 * @author Kentaro Ohkouchi
32 * @version $Id$
33 */
34class SC_DbConn_Test extends PHPUnit_Framework_TestCase
35{
36
37    /** SC_DbConn インスタンス */
38    var $objDbConn;
39
40    var $expected;
41    var $actual;
42
43    function setUp()
44    {
45        $this->objDbConn = new SC_DbConn();
46        $this->objDbConn->query('BEGIN');
47    }
48
49    function tearDown()
50    {
51        $this->objDbConn->query('ROLLBACK');
52        $this->objDbConn = null;
53    }
54
55    function verify()
56    {
57        $this->assertEquals($this->expected, $this->actual);
58    }
59
60    /**
61     * インスタンスを取得するテストケース.
62     */
63    function testGetInstance()
64    {
65        $this->expected = true;
66        $this->actual = is_object($this->objDbConn);
67
68        $this->verify();
69    }
70
71    /**
72     * SC_DbConn:query() を使用して, CREATE TABLE を実行するテストケース.
73     */
74    function testCreateTable()
75    {
76        $result = $this->createTestTable();
77
78        $this->expected = false;
79        $this->actual = PEAR::isError($result);
80
81        $this->verify();
82    }
83
84    /**
85     * SC_DbConn::getAll() のテストケース.
86     */
87    function testGetAll()
88    {
89        $this->createTestTable();
90        $result = $this->setTestData(1, '2', 'f');
91
92        $this->expected =  array(array('id' => '1',
93                                       'column1' => '1',
94                                       'column2' => '2',
95                                       'column3' => 'f'));
96        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table WHERE id = ?", array(1));
97
98        $this->verify();
99    }
100
101    /**
102     * SC_DbConn::getAll() のテストケース(エラー).
103     */
104    /*
105    function testGetAllIsError()
106    {
107
108        // SC_DbConn::getAll() は接続エラーが発生すると 0 を返す
109        $failur_dsn = "pgsql://user:pass@127.0.0.1:/xxxxx";
110        $failurDbConn = new SC_DbConn($failur_dsn, false, true);
111        $this->expected = 0;
112        $this->actual = $failurDbConn->getAll("SELECT * FROM test_table");
113
114        $this->verify();
115    }
116    */
117
118    /**
119     * SC_DbConn::getOne() のテストケース.
120     */
121    function testGetOne()
122    {
123        $this->createTestTable();
124        $this->setTestData(1, '2', 'f');
125        $this->setTestData(1, '2', 'f');
126        $this->setTestData(1, '2', 'f');
127
128        $this->expected = 3;
129        $this->actual = $this->objDbConn->getOne("SELECT COUNT(*) FROM test_table");
130
131        $this->verify();
132    }
133
134    /**
135     * SC_DbConn::getOne() のテストケース(エラー).
136     */
137    /*
138    function testGetOneIsError()
139    {
140        $this->createTestTable();
141        $this->setTestData(1, '2', 'f');
142        $this->setTestData(1, '2', 'f');
143        $this->setTestData(1, '2', 'f');
144
145        //$this->expected = new PEAR_Error();
146        $this->actual = $this->objDbConn->getOne("SELECT COUNT(*) FROM xxx_table");
147        var_dump($this->actual);
148        $this->verify();
149    }
150    */
151
152    /**
153     * SC_DbConn::getRow() のテストケース.
154     */
155    function testGetRow()
156    {
157        $this->createTestTable();
158        $this->setTestData(1, '1', 'f');
159        $this->setTestData(2, '2', 'f');
160        $this->setTestData(3, '3', 'f');
161
162        $this->expected = array('column1' => 1, 'column2' => 1);
163        $this->actual = $this->objDbConn->getRow("SELECT column1, column2 FROM test_table WHERE id = ?", array(1));
164        $this->verify();
165    }
166
167    /**
168     * SC_DbConn::getCol() のテストケース.
169     */
170    function testGetCol()
171    {
172        $this->createTestTable();
173        $this->setTestData(1, '1', 'f');
174        $this->setTestData(2, '2', 'f');
175        $this->setTestData(3, '3', 'f');
176
177        $this->expected = array(1, 2);
178        $this->actual = $this->objDbConn->getCol("SELECT column1, column2 FROM test_table WHERE id < ?", 'column1', array(3));
179
180        $this->verify();
181
182    }
183
184    /**
185     * SC_DbConn::autoExecute() で INSERT を実行するテストケース.
186     */
187    /*
188    function testAutoExecuteOfInsert()
189    {
190        $this->createTestTable();
191        $result = $this->setTestData(1, '2', 'f');
192
193        $this->expected =  array(array('id' => '1',
194                                       'column1' => '1',
195                                       'column2' => '2',
196                                       'column3' => 'f'));
197        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
198
199        //$this->assertEquals(1, $result);
200        $this->verify();
201    }
202    */
203    /**
204     * SC_DbConn::autoExecute() で UPDATE を実行するテストケース.
205     */
206    /*
207    function testAutoExecuteOfUpdate()
208    {
209        $this->createTestTable();
210        $this->setTestData(1, '2', 'f');
211
212        $data = array('id' => '1',
213                      'column1' => '2',
214                      'column2' => '3',
215                      'column3' => 't');
216
217        $result = $this->objDbConn->autoExecute('test_table', $data, "id = 1");
218
219        $this->expected =  array($data);
220        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
221
222        $this->assertEquals(1, $result);
223        $this->verify();
224    }
225    */
226
227    /**
228     * SC_DbConn::query() で INSERT を実行するテストケース.
229     */
230    function testQuery1()
231    {
232        $this->createTestTable();
233        $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)";
234        $data = array('1', '1', '1', 'f');
235
236        $this->objDbConn->query($sql, $data);
237
238        $this->expected =  array(array('id' => '1',
239                                       'column1' => '1',
240                                       'column2' => '1',
241                                       'column3' => 'f'));
242
243        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
244
245        $this->verify();
246    }
247
248    /**
249     * SC_DbConn::query() で UPDATE を実行するテストケース.
250     */
251    function testQuery2()
252    {
253        $this->createTestTable();
254        $this->setTestData(1, '2', 'f');
255
256        $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?";
257        $data = array('2', '2', '1');
258
259        $this->objDbConn->query($sql, $data);
260
261        $this->expected =  array(array('id' => '1',
262                                       'column1' => '2',
263                                       'column2' => '2',
264                                       'column3' => 'f'));
265
266        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
267
268        $this->verify();
269    }
270
271    /**
272     * SC_DbConn::prepare() は未使用
273     */
274    function testPrepare()
275    {
276    }
277
278    /**
279     * SC_DbConn::execute() は未使用
280     */
281    function testExecute()
282    {
283    }
284
285    /**
286     * SC_DbConn::reset() は未使用
287     */
288    function testReset()
289    {
290    }
291
292    function createTestTable()
293    {
294        $sql = "CREATE TABLE test_table ("
295            . "id SERIAL PRIMARY KEY,"
296            . "column1 numeric(9),"
297            . "column2 varchar(20),"
298            . "column3 char(1)"
299            . ")";
300
301        return $this->objDbConn->query($sql);
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->objDbConn->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.