mdb_usage_testcase.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,121 行 · 第 1/4 页

PHP
1,121
字号
<?php// +----------------------------------------------------------------------+// | PHP Version 4                                                        |// +----------------------------------------------------------------------+// | Copyright (c) 1998-2004 Manuel Lemos, Paul Cooper                    |// | All rights reserved.                                                 |// +----------------------------------------------------------------------+// | MDB 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.                                          |// +----------------------------------------------------------------------+// | Author: Paul Cooper <pgc@ucecom.com>                                 |// +----------------------------------------------------------------------+//// $Id: MDB_usage_testcase.php,v 1.28.4.13 2004/03/31 16:01:56 lsmith Exp $class MDB_Usage_TestCase extends PHPUnit_TestCase {    //contains the dsn of the database we are testing    var $dsn;    //contains the options that should be used during testing    var $options;    //contains the name of the database we are testing    var $database;    //contains the MDB object of the db once we have connected    var $db;    // contains field names from the test table    var $fields;    // contains the types of the fields from the test table    var $types;    function MDB_Usage_TestCase($name) {        $this->PHPUnit_TestCase($name);    }    function setUp() {        $this->dsn      = $GLOBALS['dsn'];        $this->options  = $GLOBALS['options'];        $this->database = $GLOBALS['database'];        $this->db =& MDB::connect($this->dsn, $this->options);        if (MDB::isError($this->db)) {            $this->assertTrue(false, 'Could not connect to database in setUp');            exit;        }        $this->db->setDatabase($this->database);        $this->fields = array(                        'user_name',                        'user_password',                        'subscribed',                        'user_id',                        'quota',                        'weight',                        'access_date',                        'access_time',                        'approved'                    );        $this->types = array(                        'text',                        'text',                        'boolean',                        'integer',                        'decimal',                        'float',                        'date',                        'time',                        'timestamp'                    );        $this->clearTables();    }    function tearDown() {        $this->clearTables();        unset($this->dsn);        if (!MDB::isError($this->db)) {            $this->db->disconnect();        }        unset($this->db);    }    function methodExists($name) {        if (array_key_exists(strtolower($name), array_change_key_case(array_flip(get_class_methods($this->db))))) {            return true;        }        $this->assertTrue(false, 'method '. $name.' not implemented in '.get_class($this->db));        return false;    }    function clearTables() {        if (MDB::isError($this->db->query('DELETE FROM users'))) {            $this->assertTrue(false, 'Error deleting from table users');        }        if (MDB::isError($this->db->query('DELETE FROM files'))) {            $this->assertTrue(false, 'Error deleting from table users');        }    }    function supported($feature) {        if (!$this->db->support($feature)) {            $this->assertTrue(false, 'This database does not support '.$feature);            return false;        }        return true;    }    function insertTestValues($prepared_query, &$data) {        for ($i = 0; $i < count($this->fields); $i++) {            $func = 'setParam'.$this->types[$i];            $this->db->$func($prepared_query, ($i + 1), $data[$this->fields[$i]]);        }    }    function verifyFetchedValues(&$result, $rownum, &$data) {        $row = $this->db->fetchInto($result, $rownum);        for ($i = 0; $i < count($this->fields); $i++) {            $type = $this->types[$i];            if ($this->types[$i] == 'float') {                $delta = 0.0000000001;            } else {                $delta = 0;            }            $value = $row[$i];            $field = $this->fields[$i];            $this->assertEquals($data[$field], $value, "the value retrieved for field \"$field\" ($value) using type $type doesn't match what was stored ($data[$field]).", $delta);        }    }    /**     * Test typed data storage and retrieval     *     * This tests typed data storage and retrieval by executing a single     * prepared query and then selecting the data back from the database     * and comparing the results     */    function testStorage() {        $row = 1234;        $data = array();        $data['user_name'] = "user_$row";        $data['user_password'] = 'somepassword';        $data['subscribed'] = (boolean)($row % 2);        $data['user_id'] = $row;        $data['quota'] = strval($row/100);        $data['weight'] = sqrt($row);        $data['access_date'] = MDB_Date::mdbToday();        $data['access_time'] = MDB_Date::mdbTime();        $data['approved'] = MDB_Date::mdbNow();        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);        $this->insertTestValues($prepared_query, $data);        $result = $this->db->executeQuery($prepared_query);        $this->db->freePreparedQuery($prepared_query);        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());        }        $result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users', $this->types);        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());        }        $this->verifyFetchedValues($result, 0, $data);    }    /**     * Test bulk fetch     *     * This test bulk fetching of result data by using a prepared query to     * insert an number of rows of data and then retrieving the data columns     * one by one     */    function testBulkFetch() {        $data = array();        $total_rows = 5;        $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);        for ($row = 0; $row < $total_rows; $row++) {            $data[$row]['user_name'] = "user_$row";            $data[$row]['user_password'] = 'somepassword';            $data[$row]['subscribed'] = (boolean)($row % 2);            $data[$row]['user_id'] = $row;            $data[$row]['quota'] = sprintf("%.2f",strval(1+($row+1)/100));            $data[$row]['weight'] = sqrt($row);            $data[$row]['access_date'] = MDB_Date::mdbToday();            $data[$row]['access_time'] = MDB_Date::mdbTime();            $data[$row]['approved'] = MDB_Date::mdbNow();            $this->insertTestValues($prepared_query, $data[$row]);            $result = $this->db->executeQuery($prepared_query, $this->types);            if (MDB::isError($result)) {                $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());            }        }        $this->db->freePreparedQuery($prepared_query);        $total_fields =  count($this->fields);        for ($i = 0; $i < $total_fields; $i++) {            $field = $this->fields[$i];            for ($row = 0; $row < $total_rows; $row++) {                $value = $this->db->queryOne('SELECT '.$field.' FROM users WHERE user_id='.$row, $this->types[$i]);                if (MDB::isError($value)) {                    $this->assertTrue(false, 'Error fetching row '.$row.' for field '.$field.' of type '.$this->types[$i]);                } else {                    $this->assertEquals(strval(trim($value)), strval($data[$row][$field]), 'the query field '.$field.' of type '.$this->types[$i].' for row '.$row.' was returned in "'.$value.'" unlike "'.$data[$row][$field].'" as expected');                }            }        }    }    /**     * Test prepared queries     *     * Tests prepared queries, making sure they correctly deal with ?, !, and '     */    function testPreparedQueries() {        $question_value = $this->db->getTextValue('Does this work?');        $prepared_query = $this->db->prepareQuery("INSERT INTO users (user_name, user_password, user_id) VALUES (?, $question_value, 1)", array('text'));        $this->db->setParamText($prepared_query, 1, 'Sure!');        $result = $this->db->executeQuery($prepared_query);        $this->db->freePreparedQuery($prepared_query);        if (MDB::isError($result)) {            $error = $result->getMessage();        }        $this->assertTrue(!MDB::isError($result), 'Could not execute prepared query with a text value with a question mark. Error: ');        $question_value = $this->db->getTextValue("Wouldn't it be great if this worked too?");        $prepared_query = $this->db->prepareQuery("INSERT INTO users (user_name, user_password, user_id) VALUES (?, $question_value, 2)", array('text'));        $this->db->setParamText($prepared_query, 1, 'For Sure!');        $result = $this->db->executeQuery($prepared_query);        $this->db->freePreparedQuery($prepared_query);        if (MDB::isError($result)) {            $error = $result->getMessage();        }        $this->assertTrue(!MDB::isError($result), 'Could not execute prepared query with a text value with a quote character before a question mark. Error: ');

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?