home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / MDB_api_testcase.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  8.3 KB  |  213 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: MDB_api_testcase.php,v 1.13.4.6 2004/01/08 13:43:00 lsmith Exp $
  45.  
  46. class MDB_Api_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_Api_Test($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 - ' .$this->db->getMessage() . ' - ' .$this->db->getUserInfo());
  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.     }
  98.  
  99.     function tearDown() {
  100.         unset($this->dsn);
  101.         if (!MDB::isError($this->db)) {
  102.             $this->db->disconnect();
  103.         }
  104.         unset($this->db);
  105.     }
  106.  
  107.     function methodExists($name) {
  108.         if (array_key_exists(strtolower($name), array_flip(get_class_methods($this->db)))) {
  109.             return true;
  110.         }
  111.         $this->assertTrue(false, 'method '. $name.' not implemented in '.get_class($this->db));
  112.         return false;
  113.     }
  114.  
  115.     //test stuff in common.php
  116.     function testConnect() {
  117.         $db =& MDB::connect($this->dsn, $this->options);
  118.         if(MDB::isError($db)) {
  119.             $this->assertTrue(false, 'Connect failed bailing out - ' .$db->getMessage() . ' - ' .$db->getUserInfo());
  120.         }
  121.         if (MDB::isError($this->db)) {
  122.             exit;
  123.         }
  124.     }
  125.  
  126.     function testGetOption() {
  127.         if (!$this->methodExists('getOption')) {
  128.             return;
  129.         }
  130.         $option = $this->db->getOption('persistent');
  131.         $this->assertEquals($option, $this->db->options['persistent']);
  132.     }
  133.  
  134.     function testSetOption() {
  135.         if (!$this->methodExists('setOption')) {
  136.             return;
  137.         }
  138.         $option = $this->db->getOption('persistent');
  139.         $this->db->setOption('persistent', !$option);
  140.         $this->assertEquals(!$option, $this->db->getOption('persistent'));
  141.         $this->db->setOption('persistent', $option);
  142.     }
  143.  
  144. /*
  145. // incorrectly expects a specific escape character
  146.     function testGetTextValue() {
  147.         if (!$this->methodExists('getTextValue')) {
  148.             return;
  149.         }
  150.         $text = "Mr O'Leary";
  151.         $text = $this->db->getTextValue($text);
  152.         $this->assertEquals("'Mr O\'Leary'", $text);
  153.     }
  154. */
  155.  
  156.     function testLoadManager() {
  157.         if (!$this->methodExists('loadManager')) {
  158.             return;
  159.         }
  160.         $this->assertTrue(!MDB::isError($this->db->loadManager('Load Management Class')));
  161.     }
  162.  
  163.     // test of the driver
  164.     // helper function so that we don't have to write out a query a million times
  165.     function standardQuery() {
  166.         $query = 'SELECT * FROM users';
  167.         // run the query and get a result handler
  168.         if (!MDB::isError($this->db)) {
  169.             return $this->db->query($query);
  170.         }
  171.         return false;
  172.     }
  173.  
  174.     function testQuery() {
  175.         if (!$this->methodExists('query')) {
  176.             return;
  177.         }
  178.         $result = $this->standardQuery();
  179.         $this->assertTrue(is_resource($result), 'query: $result returned is not a resource');
  180.     }
  181.  
  182.     function testFetchInto() {
  183.         if (!$this->methodExists('fetch')) {
  184.             return;
  185.         }
  186.         $result = $this->standardQuery();
  187.         $err = $this->db->fetchInto($result);
  188.         if(MDB::isError($err)) {
  189.             $this->assertTrue(false, 'Error testFetch: '.$err->getMessage().' - '.$err->getUserInfo());
  190.         }
  191.         $this->assertNull($err);
  192.     }
  193.  
  194.     function testNumCols() {
  195.         if (!$this->methodExists('numCols')) {
  196.             return;
  197.         }
  198.         $result = $this->standardQuery();
  199.         $this->assertTrue((!MDB::isError($this->db->numCols($result))) && ($this->db->numCols($result) > 0));
  200.     }
  201.  
  202.     function testSingleton() {
  203.         $mdb =& MDB::singleton();
  204.         $this->assertTrue(MDB::isConnection($mdb));
  205.  
  206.         // should have a different database name set
  207.         $mdb =& MDB::singleton($this->dsn, $this->options);
  208.  
  209.         $this->assertTrue($mdb->database != $this->db->database);
  210.     }
  211. }
  212.  
  213. ?>