1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
<?php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Paul Cooper |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Authors: Paul Cooper <pgc@ucecom.com> |
// | Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
//
// $Id: Abstract.php 322076 2012-01-11 15:51:09Z danielc $
abstract class Standard_Abstract extends PHPUnit_Framework_TestCase {
/**
* Should the tables be cleared in the setUp() and tearDown() methods?
* @var bool
*/
protected $clear_tables = true;
/**
* The database name currently being tested
* @var string
*/
public $database;
/**
* The MDB2 object being currently tested
* @var MDB2_Driver_Common
*/
public $db;
/**
* The DSN of the database that is currently being tested
* @var array
*/
public $dsn;
/**
* The unserialized value of MDB2_TEST_SERIALIZED_DSNS
* @var array
*/
protected static $dsns;
/**
* Field names of the test table
* @var array
*/
public $fields = array(
'user_name' => 'text',
'user_password' => 'text',
'subscribed' => 'boolean',
'user_id' => 'integer',
'quota' => 'decimal',
'weight' => 'float',
'access_date' => 'date',
'access_time' => 'time',
'approved' => 'timestamp',
);
/**
* Options to use on the current database run
* @var array
*/
public $options;
/**
* @var string the name of the users table
*/
public $table_users = 'mdb2_users';
/**
* @var string the name of the file table
*/
public $table_files = 'mdb2_files';
/**
* Override PHPUnit's default behavior so authentication data doesn't
* get broadcasted
*/
protected function getDataSetAsString($strict = true) {
return parent::getDataSetAsString(false);
}
public static function setUpBeforeClass() {
$dsns = unserialize(MDB2_TEST_SERIALIZED_DSNS);
self::$dsns = $dsns;
}
/**
* A PHPUnit dataProvider callback to supply the connection info for tests
* @uses mdb2_test_db_object_provider()
* @return array the $dsn and $options information for MDB2::factory()
*/
public function provider() {
return mdb2_test_db_object_provider();
}
/**
* Establishes the class properties for each test
*
* Can not use setUp() because we are using a dataProvider to get multiple
* MDB2 objects per test.
*
* @param array $ci an associative array with two elements. The "dsn"
* element must contain an array of DSN information.
* The "options" element must be an array of connection
* options.
*/
protected function manualSetUp($ci) {
$this->db = MDB2::factory($ci['dsn'], $ci['options']);
if (MDB2::isError($this->db)) {
$this->markTestSkipped($this->db->getMessage());
}
$this->dsn = self::$dsns[$this->db->phptype]['dsn'];
$this->options = self::$dsns[$this->db->phptype]['options'];
$this->database = $this->db->getDatabase();
$this->db->setDatabase($this->database);
if ($this->database == ':memory:') {
// Disable messages from other packages while building schema.
$prior = error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
build_schema($this->db);
error_reporting($prior);
}
$this->db->expectError(MDB2_ERROR_UNSUPPORTED);
$this->clearTables();
}
public function tearDown() {
if (!$this->db || MDB2::isError($this->db)) {
return;
}
$this->clearTables();
$this->db->disconnect();
$this->db->popExpect();
unset($this->db);
}
public function clearTables() {
if (!$this->clear_tables) {
return;
}
$this->db->exec('DELETE FROM ' . $this->table_users);
$this->db->exec('DELETE FROM ' . $this->table_files);
}
public function supported($feature) {
if (!$this->db->supports($feature)) {
return false;
}
return true;
}
/**
* Checks if a result is an MDB2 error and calls the
* appropriate PHPUnit method if it is
*
* + MDB2_ERROR_UNSUPPORTED: markTestSkipped(not supported)
* + MDB2_ERROR_NOT_CAPABLE: markTestSkipped(not supported)
* + MDB2_ERROR_NO_PERMISSION: markTestSkipped(lacks permission)
* + MDB2_ERROR_ACCESS_VIOLATION: markTestSkipped(lacks permission)
* + Other errors: fail(error details)
*
* NOTE: calling PHPUnit's skip and fail methods causes the current
* test to halt execution, so no conditional statements or other error
* handling are needed by this method or the test methods calling this
* method.
*
* @param mixed $result the query result to inspect
* @param string $action a description of what is being checked
* @return void
*/
public function checkResultForErrors($result, $action)
{
if (MDB2::isError($result)) {
if ($result->getCode() == MDB2_ERROR_UNSUPPORTED
|| $result->getCode() == MDB2_ERROR_NOT_CAPABLE) {
$this->markTestSkipped("$action not supported");
}
if ($result->getCode() == MDB2_ERROR_NO_PERMISSION
|| $result->getCode() == MDB2_ERROR_ACCESS_VIOLATION)
{
$this->markTestSkipped("User lacks permission to $action");
}
$this->fail("$action ERROR: ".$result->getUserInfo());
}
}
/**
* @param MDB2_Result_Common $result the query result to check
* @param type $rownum the row in the $result to check
* @param type $data the expected data
* @return bool
*/
public function verifyFetchedValues(&$result, $rownum, $data) {
$row = $result->fetchRow(MDB2_FETCHMODE_ASSOC, $rownum);
if (!is_array($row)) {
$result->free();
$this->fail('Error result row is not an array');
return;
}
foreach ($this->fields as $field => $type) {
$value = $row[$field];
if ($type == 'float') {
$delta = 0.0000000001;
} else {
$delta = 0;
}
$this->assertEquals($data[$field], $value, "the value retrieved for field \"$field\" doesn't match what was stored into the rownum $rownum", $delta);
}
}
public function getSampleData($row = 1) {
$data = array();
$data['user_name'] = 'user_' . $row;
$data['user_password'] = 'somepass';
$data['subscribed'] = $row % 2 ? true : false;
$data['user_id'] = $row;
$data['quota'] = strval($row/100);
$data['weight'] = sqrt($row);
$data['access_date'] = MDB2_Date::mdbToday();
$data['access_time'] = MDB2_Date::mdbTime();
$data['approved'] = MDB2_Date::mdbNow();
return $data;
}
/**
* Populates the user table with some data and then returns the data for
* later comparison
*
* @param int $rows the number for rows to insert
* @return array a multi-dimensional associative array of the data inserted
*/
public function populateUserData($rows = 1) {
$result = $this->db->loadModule('Extended');
if (MDB2::isError($result)) {
$this->fail('populateUserData() problem loading module: ' . $result->getUserInfo());
}
$this->db->loadModule('Extended');
$stmt = $this->db->extended->autoPrepare($this->table_users,
array_keys($this->fields),
MDB2_AUTOQUERY_INSERT, null, $this->fields);
if (MDB2::isError($stmt)) {
$this->fail('populateUserData() problem preparing statement: ' . $stmt->getUserInfo());
}
$data_save = array();
$data_return = array();
for ($i = 0; $i < $rows; $i++) {
$row = $this->getSampleData($i);
$data_save[] = array_values($row);
$data_return[] = $row;
}
$result = $this->db->extended->executeMultiple($stmt, $data_save);
if (MDB2::isError($result)) {
$this->fail('populateUserData() problem inserting the data: ' . $result->getUserInfo());
}
return $data_return;
}
public function methodExists(&$class, $name) {
if (is_object($class)
&& in_array(strtolower($name), array_map('strtolower', get_class_methods($class)))
) {
return true;
}
//$this->fail('method '. $name.' not implemented in '.get_class($class));
return false;
}
public function tableExists($table) {
$this->db->loadModule('Manager', null, true);
$tables = $this->db->manager->listTables();
if (MDB2::isError($tables)) {
//$this->fail('Cannot list tables: '. $tables->getUserInfo());
return false;
}
return in_array(strtolower($table), array_map('strtolower', $tables));
}
}
|