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