| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2010 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 は, できるだけ単独で動くようにしたい |
|---|
| 26 | require_once(realpath(dirname(__FILE__)) . "/../require.php"); |
|---|
| 27 | require_once(realpath(dirname(__FILE__)) . "/../../data/class/SC_Query.php"); |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * SC_Query のテストケース. |
|---|
| 31 | * |
|---|
| 32 | * @author Kentaro Ohkouchi |
|---|
| 33 | * @version $Id$ |
|---|
| 34 | */ |
|---|
| 35 | class SC_Query_Test extends PHPUnit_Framework_TestCase { |
|---|
| 36 | |
|---|
| 37 | /** SC_Query インスタンス */ |
|---|
| 38 | var $objQuery; |
|---|
| 39 | |
|---|
| 40 | var $expected; |
|---|
| 41 | var $actual; |
|---|
| 42 | |
|---|
| 43 | function setUp() { |
|---|
| 44 | $this->objQuery = new SC_Query(); |
|---|
| 45 | $this->objQuery->begin(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | function tearDown() { |
|---|
| 49 | $this->objQuery->rollback(); |
|---|
| 50 | // MySQL では CREATE TABLE がロールバックされないので DROP TABLE を行う |
|---|
| 51 | $this->dropTestTable(); |
|---|
| 52 | $this->objQuery = null; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | function verify() { |
|---|
| 56 | $this->assertEquals($this->expected, $this->actual); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * インスタンスを取得するテストケース. |
|---|
| 61 | */ |
|---|
| 62 | function testGetInstance() { |
|---|
| 63 | $this->expected = true; |
|---|
| 64 | $this->actual = is_object($this->objQuery); |
|---|
| 65 | |
|---|
| 66 | $this->verify(); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * SC_Query::query() を使用して, CREATE TABLE を実行するテストケース. |
|---|
| 71 | */ |
|---|
| 72 | function testCreateTable() { |
|---|
| 73 | $result = $this->createTestTable(); |
|---|
| 74 | |
|---|
| 75 | $this->expected = false; |
|---|
| 76 | $this->actual = PEAR::isError($result); |
|---|
| 77 | |
|---|
| 78 | $this->verify(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * SC_Query::getAll() のテストケース. |
|---|
| 83 | */ |
|---|
| 84 | function testGetAll() { |
|---|
| 85 | $this->createTestTable(); |
|---|
| 86 | $result = $this->setTestData(1, "2", "f"); |
|---|
| 87 | |
|---|
| 88 | $this->expected = array(array("id" => "1", |
|---|
| 89 | "column1" => "1", |
|---|
| 90 | "column2" => "2", |
|---|
| 91 | "column3" => "f")); |
|---|
| 92 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table WHERE id = ?", array(1)); |
|---|
| 93 | |
|---|
| 94 | $this->verify(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * SC_Query::getOne() のテストケース. |
|---|
| 99 | */ |
|---|
| 100 | function testGetOne() { |
|---|
| 101 | $this->createTestTable(); |
|---|
| 102 | $this->setTestData(1, "2", "f"); |
|---|
| 103 | $this->setTestData(1, "2", "f"); |
|---|
| 104 | $this->setTestData(1, "2", "f"); |
|---|
| 105 | |
|---|
| 106 | $this->expected = 3; |
|---|
| 107 | $this->actual = $this->objQuery->getOne("SELECT COUNT(*) FROM test_table"); |
|---|
| 108 | |
|---|
| 109 | $this->verify(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * SC_Query::getRow() のテストケース. |
|---|
| 114 | */ |
|---|
| 115 | function testGetRow() { |
|---|
| 116 | $this->createTestTable(); |
|---|
| 117 | $this->setTestData(1, "1", "f"); |
|---|
| 118 | $this->setTestData(2, "2", "f"); |
|---|
| 119 | $this->setTestData(3, "3", "f"); |
|---|
| 120 | |
|---|
| 121 | $this->expected = array("column1" => 1, "column2" => 1); |
|---|
| 122 | $this->actual = $this->objQuery->getRow("test_table", "column1, column2", "id = ?", array(1)); |
|---|
| 123 | $this->verify(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * SC_Query::getCol() のテストケース. |
|---|
| 128 | */ |
|---|
| 129 | function testGetCol() { |
|---|
| 130 | $this->createTestTable(); |
|---|
| 131 | $this->setTestData(1, "1", "f"); |
|---|
| 132 | $this->setTestData(2, "2", "f"); |
|---|
| 133 | $this->setTestData(3, "3", "f"); |
|---|
| 134 | |
|---|
| 135 | $this->expected = array(1, 2); |
|---|
| 136 | $this->actual = $this->objQuery->getCol("test_table", "column1", "id < ?", array(3)); |
|---|
| 137 | |
|---|
| 138 | $this->verify(); |
|---|
| 139 | |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * SC_Query::query() で INSERT を実行するテストケース. |
|---|
| 144 | */ |
|---|
| 145 | function testQuery1() { |
|---|
| 146 | $this->createTestTable(); |
|---|
| 147 | $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)"; |
|---|
| 148 | $data = array("1", "1", "1", "f"); |
|---|
| 149 | |
|---|
| 150 | $this->objQuery->query($sql, $data); |
|---|
| 151 | |
|---|
| 152 | $this->expected = array(array("id" => "1", |
|---|
| 153 | "column1" => "1", |
|---|
| 154 | "column2" => "1", |
|---|
| 155 | "column3" => "f")); |
|---|
| 156 | |
|---|
| 157 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
|---|
| 158 | |
|---|
| 159 | $this->verify(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | function testInsert() { |
|---|
| 163 | $this->createTestTable(); |
|---|
| 164 | |
|---|
| 165 | $this->objQuery->insert("test_table", |
|---|
| 166 | array("id" => "1", |
|---|
| 167 | "column1" => "1", |
|---|
| 168 | "column2" => "1", |
|---|
| 169 | "column3" => "f")); |
|---|
| 170 | |
|---|
| 171 | $this->expected = array(array("id" => "1", |
|---|
| 172 | "column1" => "1", |
|---|
| 173 | "column2" => "1", |
|---|
| 174 | "column3" => "f")); |
|---|
| 175 | |
|---|
| 176 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
|---|
| 177 | |
|---|
| 178 | $this->verify(); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * SC_Query::query() で UPDATE を実行するテストケース. |
|---|
| 183 | */ |
|---|
| 184 | function testQuery2() { |
|---|
| 185 | $this->createTestTable(); |
|---|
| 186 | $this->setTestData(1, "2", "f"); |
|---|
| 187 | |
|---|
| 188 | $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?"; |
|---|
| 189 | $data = array("2", "2", "1"); |
|---|
| 190 | |
|---|
| 191 | $this->objQuery->query($sql, $data); |
|---|
| 192 | |
|---|
| 193 | $this->expected = array(array("id" => "1", |
|---|
| 194 | "column1" => "2", |
|---|
| 195 | "column2" => "2", |
|---|
| 196 | "column3" => "f")); |
|---|
| 197 | |
|---|
| 198 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
|---|
| 199 | |
|---|
| 200 | $this->verify(); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | function testUpdate() { |
|---|
| 204 | $this->createTestTable(); |
|---|
| 205 | $this->setTestData(1, "2", "f"); |
|---|
| 206 | |
|---|
| 207 | $this->objQuery->update("test_table", |
|---|
| 208 | array("id" => "1", |
|---|
| 209 | "column1" => "2", |
|---|
| 210 | "column2" => "2", |
|---|
| 211 | "column3" => "f"), |
|---|
| 212 | "id = ?", array(1)); |
|---|
| 213 | $this->expected = array(array("id" => "1", |
|---|
| 214 | "column1" => "2", |
|---|
| 215 | "column2" => "2", |
|---|
| 216 | "column3" => "f")); |
|---|
| 217 | |
|---|
| 218 | $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); |
|---|
| 219 | |
|---|
| 220 | $this->verify(); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | function testListTables() { |
|---|
| 224 | $tables = $this->objQuery->listTables(); |
|---|
| 225 | $this->assertTrue(in_array("mtb_zip", $tables)); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | function testListSequences() { |
|---|
| 229 | $sequences = $this->objQuery->listSequences(); |
|---|
| 230 | $this->assertTrue(in_array("dtb_products_product_id", $sequences)); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | function testListTableFields() { |
|---|
| 234 | $this->expected = array("id", "name", "rank", "remarks"); |
|---|
| 235 | $this->actual = $this->objQuery->listTableFields("mtb_constants"); |
|---|
| 236 | $this->verify(); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | function testListTableIndexes() { |
|---|
| 240 | $indexes = $this->objQuery->listTableIndexes("dtb_mobile_kara_mail"); |
|---|
| 241 | $this->assertTrue(in_array("dtb_mobile_kara_mail_create_date_key", $indexes)); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | function createTestTable() { |
|---|
| 245 | $sql = "CREATE TABLE test_table (" |
|---|
| 246 | . "id SERIAL PRIMARY KEY," |
|---|
| 247 | . "column1 numeric(9)," |
|---|
| 248 | . "column2 varchar(20)," |
|---|
| 249 | . "column3 char(1)" |
|---|
| 250 | . ")"; |
|---|
| 251 | return $this->objQuery->query($sql); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | function dropTestTable() { |
|---|
| 255 | return $this->objQuery->query("DROP TABLE test_table"); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | function setTestData($column1, $column2, $column3) { |
|---|
| 259 | $fields_values = array($column1, $column2, $column3); |
|---|
| 260 | $sql = "INSERT INTO test_table (column1, column2, column3) VALUES (?, ?, ?)"; |
|---|
| 261 | $result = $this->objQuery->query($sql, $fields_values); |
|---|
| 262 | if (PEAR::isError($result)) { |
|---|
| 263 | var_dump($result); |
|---|
| 264 | } |
|---|
| 265 | return $result; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | ?> |
|---|