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