home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / MDB_bugs_testcase.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  7.7 KB  |  182 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Paul Cooper                    |
  6. // | All rights reserved.                                                 |
  7. // +----------------------------------------------------------------------+
  8. // | MDB is a merge of PEAR DB and Metabases that provides a unified DB   |
  9. // | API as well as database abstraction for PHP applications.            |
  10. // | This LICENSE is in the BSD license style.                            |
  11. // |                                                                      |
  12. // | Redistribution and use in source and binary forms, with or without   |
  13. // | modification, are permitted provided that the following conditions   |
  14. // | are met:                                                             |
  15. // |                                                                      |
  16. // | Redistributions of source code must retain the above copyright       |
  17. // | notice, this list of conditions and the following disclaimer.        |
  18. // |                                                                      |
  19. // | Redistributions in binary form must reproduce the above copyright    |
  20. // | notice, this list of conditions and the following disclaimer in the  |
  21. // | documentation and/or other materials provided with the distribution. |
  22. // |                                                                      |
  23. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  24. // | Lukas Smith nor the names of his contributors may be used to endorse |
  25. // | or promote products derived from this software without specific prior|
  26. // | written permission.                                                  |
  27. // |                                                                      |
  28. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  29. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  30. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  31. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  32. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  33. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  34. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  35. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  36. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  37. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  38. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  39. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  40. // +----------------------------------------------------------------------+
  41. // | Author: Paul Cooper <pgc@ucecom.com>                                 |
  42. // +----------------------------------------------------------------------+
  43. //
  44. // $Id:
  45.  
  46. class MDB_Bugs_TestCase extends PHPUnit_TestCase {
  47.     //contains the dsn of the database we are testing
  48.     var $dsn;
  49.     //contains the options that should be used during testing
  50.     var $options;
  51.     //contains the name of the database we are testing
  52.     var $database;
  53.     //contains the MDB object of the db once we have connected
  54.     var $db;
  55.     // contains field names from the test table
  56.     var $fields;
  57.     // contains the types of the fields from the test table
  58.     var $types;
  59.  
  60.     function MDB_Bugs_TestCase($name) {
  61.         $this->PHPUnit_TestCase($name);
  62.     }
  63.  
  64.     function setUp() {
  65.         $this->dsn      = $GLOBALS['dsn'];
  66.         $this->options  = $GLOBALS['options'];
  67.         $this->database = $GLOBALS['database'];
  68.         $this->db =& MDB::connect($this->dsn, $this->options);
  69.         if (MDB::isError($this->db)) {
  70.             $this->assertTrue(false, 'Could not connect to database in setUp');
  71.             exit;
  72.         }
  73.         $this->db->setDatabase($this->database);
  74.         $this->fields = array(
  75.                         'user_name',
  76.                         'user_password',
  77.                         'subscribed',
  78.                         'user_id',
  79.                         'quota',
  80.                         'weight',
  81.                         'access_date',
  82.                         'access_time',
  83.                         'approved'
  84.                     );
  85.  
  86.         $this->types = array(
  87.                         'text',
  88.                         'text',
  89.                         'boolean',
  90.                         'integer',
  91.                         'decimal',
  92.                         'float',
  93.                         'date',
  94.                         'time',
  95.                         'timestamp'
  96.                     );
  97.         $this->clearTables();
  98.     }
  99.  
  100.     function tearDown() {
  101.         $this->clearTables();
  102.         unset($this->dsn);
  103.         if (!MDB::isError($this->db)) {
  104.             $this->db->disconnect();
  105.         }
  106.         unset($this->db);
  107.     }
  108.  
  109.     function clearTables() {
  110.         if (MDB::isError($this->db->query('DELETE FROM users'))) {
  111.             $this->assertTrue(false, 'Error deleting from table users');
  112.         }
  113.         if (MDB::isError($this->db->query('DELETE FROM files'))) {
  114.             $this->assertTrue(false, 'Error deleting from table users');
  115.         }
  116.     }
  117.  
  118.     function insertTestValues($prepared_query, &$data) {
  119.         for ($i = 0; $i < count($this->fields); $i++) {
  120.             $func = 'setParam'.$this->types[$i];
  121.             $this->db->$func($prepared_query, ($i + 1), $data[$this->fields[$i]]);
  122.         }
  123.     }
  124.  
  125.     /**
  126.      *
  127.      */
  128.     function testFetchModeBug() {
  129.         $data = array();
  130.         $total_rows = 3;
  131.  
  132.         $prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
  133.  
  134.         for ($row = 0; $row < $total_rows; $row++) {
  135.             $data[$row]['user_name'] = "user_$row";
  136.             $data[$row]['user_password'] = 'somepassword';
  137.             $data[$row]['subscribed'] = $row % 2;
  138.             $data[$row]['user_id'] = $row;
  139.             $data[$row]['quota'] = sprintf("%.2f",strval(1+($row+1)/100));
  140.             $data[$row]['weight'] = sqrt($row);
  141.             $data[$row]['access_date'] = MDB_Date::mdbToday();
  142.             $data[$row]['access_time'] = MDB_Date::mdbTime();
  143.             $data[$row]['approved'] = MDB_Date::mdbNow();
  144.  
  145.             $this->insertTestValues($prepared_query, $data[$row]);
  146.  
  147.             $result = $this->db->executeQuery($prepared_query);
  148.  
  149.             if (MDB::isError($result)) {
  150.                 $this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
  151.             }
  152.         }
  153.  
  154.         $this->db->freePreparedQuery($prepared_query);
  155.  
  156.         $result = $this->db->query('SELECT * FROM users ORDER BY user_name');
  157.  
  158.         if (MDB::isError($result)) {
  159.             $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
  160.         }
  161.  
  162.         $this->db->setFetchMode(MDB_FETCHMODE_ASSOC);
  163.  
  164.         $field = $this->db->fetchOne($result);
  165.         $this->assertEquals($field, $data[0]['user_name'], "The data returned ($field) does not match that expected (".$data[0]['user_name'].")");
  166.     }
  167.  
  168.     /**
  169.      * http://bugs.php.net/bug.php?id=22328
  170.      */
  171.     function testBug22328() {
  172.         $result = $this->db->query('SELECT * FROM users');
  173.         $this->db->pushErrorHandling(PEAR_ERROR_RETURN);
  174.         $result2 = $this->db->query('SELECT * FROM foo');
  175.  
  176.         $data = $this->db->fetchRow($result);
  177.         $this->db->popErrorHandling();
  178.         $this->assertEquals(false, MDB::isError($data), "Error messages for a query affect result reading of other queries");
  179.     }
  180. }
  181.  
  182. ?>