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

Revision 22857, 8.3 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     * SC_DbConn::autoExecute() で INSERT を実行するテストケース.
185     */
186    /*
187    function testAutoExecuteOfInsert()
188    {
189        $this->createTestTable();
190        $result = $this->setTestData(1, '2', 'f');
191
192        $this->expected =  array(array('id' => '1',
193                                       'column1' => '1',
194                                       'column2' => '2',
195                                       'column3' => 'f'));
196        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
197
198        //$this->assertEquals(1, $result);
199        $this->verify();
200    }
201    */
202    /**
203     * SC_DbConn::autoExecute() で UPDATE を実行するテストケース.
204     */
205    /*
206    function testAutoExecuteOfUpdate()
207    {
208        $this->createTestTable();
209        $this->setTestData(1, '2', 'f');
210
211        $data = array('id' => '1',
212                      'column1' => '2',
213                      'column2' => '3',
214                      'column3' => 't');
215
216        $result = $this->objDbConn->autoExecute('test_table', $data, "id = 1");
217
218        $this->expected =  array($data);
219        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
220
221        $this->assertEquals(1, $result);
222        $this->verify();
223    }
224    */
225
226    /**
227     * SC_DbConn::query() で INSERT を実行するテストケース.
228     */
229    function testQuery1()
230    {
231        $this->createTestTable();
232        $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)";
233        $data = array('1', '1', '1', 'f');
234
235        $this->objDbConn->query($sql, $data);
236
237        $this->expected =  array(array('id' => '1',
238                                       'column1' => '1',
239                                       'column2' => '1',
240                                       'column3' => 'f'));
241
242        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
243
244        $this->verify();
245    }
246
247    /**
248     * SC_DbConn::query() で UPDATE を実行するテストケース.
249     */
250    function testQuery2()
251    {
252        $this->createTestTable();
253        $this->setTestData(1, '2', 'f');
254
255        $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?";
256        $data = array('2', '2', '1');
257
258        $this->objDbConn->query($sql, $data);
259
260        $this->expected =  array(array('id' => '1',
261                                       'column1' => '2',
262                                       'column2' => '2',
263                                       'column3' => 'f'));
264
265        $this->actual = $this->objDbConn->getAll("SELECT * FROM test_table");
266
267        $this->verify();
268    }
269
270    /**
271     * SC_DbConn::prepare() は未使用
272     */
273    function testPrepare()
274    {
275    }
276
277    /**
278     * SC_DbConn::execute() は未使用
279     */
280    function testExecute()
281    {
282    }
283
284    /**
285     * SC_DbConn::reset() は未使用
286     */
287    function testReset()
288    {
289    }
290
291    function createTestTable()
292    {
293        $sql = "CREATE TABLE test_table ("
294            . "id SERIAL PRIMARY KEY,"
295            . "column1 numeric(9),"
296            . "column2 varchar(20),"
297            . "column3 char(1)"
298            . ")";
299
300        return $this->objDbConn->query($sql);
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->objDbConn->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.