PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_Dbms_Mysqli_Test.php

Documentation is available at PMO_Dbms_Mysqli_Test.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_Dbms_Mysqli driver tests.
  4.  *
  5.  * This file is part of the PhpMyObject project,
  6.  * an Object-Relational Mapping (ORM) system.
  7.  * 
  8.  * For questions, help, comments, discussion, etc., please join our
  9.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  10.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  11.  *
  12.  * PhpMyObject is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program.  If not, see {@link http://www.gnu.org/licenses/}.
  24.  *
  25.  * @package            PhpMyObject
  26.  * @subpackage        PMO_Tests
  27.  * @author            Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  28.  * @author            Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  29.  * @link                http://pmo.developpez.com/
  30.  * @since            PhpMyObject v0.15
  31.  * @version            $Revision: 400 $
  32.  * @copyright        Copyright (C) 2007-2008 Nicolas Boiteux
  33.  * @license            GPLv3 {@link http://www.gnu.org/licenses/gpl}
  34.  * @filesource
  35.  *
  36.  */ 
  37.  
  38. /**
  39.  * setup this test case if called individually
  40.  */
  41. if (!defined('PMO_TEST_SUITE')) {
  42.     require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'config.php');
  43.     require_once(SIMPLETEST.DS.'autorun.php');
  44. }
  45.  
  46.  
  47. /**
  48.  * requires the PMO_Dbms_Mysql driver
  49.  */
  50. require_once(PMO_CORE DS 'PMO_MyDbms.php');
  51. require_once(PMO_CORE DS 'PMO_dbms_mysql.php');
  52.  
  53. /**
  54.  * this tests the PMO_Dbms_Mysqli connections.
  55.  *
  56.  * This is just a sanity check. Real testing is going to be
  57.  * done with de PMO_MyDbms class.
  58.  */
  59. class PMO_Dbms_Mysqli_Connection_Test extends UnitTestCase
  60. {
  61.     /** constructor calls parent contructor. */
  62.     function PMO_Dbms_Mysqli_Connection_Test({
  63.         $this->UnitTestCase('PMO_Dbms_Mysql Connection Tests (from '.basename(__FILE__).')');
  64.     }
  65.  
  66.     function setUp({
  67.         $this->authdb array(
  68.             'driver' => 'mysql',
  69.             'pdodriver' => '',
  70.             'host' => 'localhost',
  71.             'user' => 'pmo',
  72.             'pass' => 'pmo',
  73.             'base' => 'test',
  74.             'dsn' => ''
  75.         );
  76.     }
  77.  
  78.     function tearDown({
  79.     }
  80.  
  81.     /***********************
  82.      * sait pas encore pourquoi mais j'arrive pas à catcher les errors mysql
  83.      * TODO WORK THIS OUT!!!
  84.      */
  85.     function test_connect_with_bad_host({
  86.  
  87.         $db new PMO_Dbms_Mysql();
  88.         $this->authdb['host''qwerty';
  89.  
  90.         try {
  91.             @$db->connect($this->authdb);
  92.             $this->fail('Should have catched the exception.');
  93.         }
  94.         catch(Exception $e{
  95.             $this->assertWantedPattern('/Unknown MySQL server host/'$e->getMessage()$e->getMessage());
  96.         }
  97.     }
  98.  
  99.     function test_connect_with_bad_user({
  100.  
  101.         $db new PMO_Dbms_mysql();
  102.         $this->authdb['user''qwerty';
  103.         
  104.         try {
  105.             @$db->connect($this->authdb);
  106.             $this->fail('Should have catched the exception.');
  107.         }
  108.         catch(Exception $e{
  109.             $this->assertWantedPattern('/Access denied for user \'qwerty\'/'$e->getMessage()$e->getMessage());
  110.         }
  111.     }
  112.  
  113.     function test_connect_with_bad_pass({
  114.         // for the connection tests, we won't use $this->db
  115.         $db new PMO_Dbms_mysql();
  116.         $this->authdb['pass''qwerty';
  117.  
  118.         try {
  119.             @$db->connect($this->authdb);
  120.             $this->fail('Should have catched the exception.');
  121.         }
  122.         catch(Exception $e{
  123.             $this->assertWantedPattern('/Access denied for user \'pmo\'/'$e->getMessage()$e->getMessage());
  124.         }
  125.     }
  126.  
  127.     function test_connect_with_bad_base({
  128.         // for the connection tests, we won't use $this->db
  129.         $db new PMO_Dbms_mysql();
  130.         $this->authdb['base''qwerty';
  131.  
  132.         try {
  133.             @$db->connect($this->authdb);
  134.             $this->fail('Should have catched the exception.');
  135.         }
  136.         catch(Exception $e{
  137.             $this->assertWantedPattern('/database \'qwerty\'/'$e->getMessage()$e->getMessage());
  138.         }
  139.     }
  140.  
  141.     function test_connect_good_connection({
  142.         // for the connection tests, we won't use $this->db
  143.         $db new PMO_Dbms_mysql();
  144.  
  145.         try {
  146.             $db->connect($this->authdb);
  147.             $this->pass('Connected');
  148.         }
  149.         catch (Exception $e{
  150.             $this->fail($e->getMessage());
  151.         }
  152.     }
  153.  
  154. }
  155.  
  156. /**
  157.  * Tests the PMO_Dbms_Mysqli class
  158.  *
  159.  * This is just a sanity check. Real testing is going to be
  160.  * done with de PMO_MyDbms class.
  161.  */
  162. class PMO_Dbms_Mysqli_Test extends UnitTestCase
  163. {
  164.     /** constructor calls parent contructor. */
  165.     function PMO_Dbms_Mysqli_Test({
  166.         $this->UnitTestCase();
  167.     }
  168.  
  169.     function setUp({
  170.         $this->authdb array(
  171.             'driver' => 'mysql',
  172.             'pdodriver' => '',
  173.             'host' => 'localhost',
  174.             'user' => 'pmo',
  175.             'pass' => 'pmo',
  176.             'base' => 'test',
  177.             'dsn' => ''
  178.         );
  179.         $this->setSql();
  180.         $this->db new PMO_Dbms_Mysql();
  181.         $this->db->connect($this->authdb);
  182.         $this->db->query($this->drop);
  183.         $this->db->query($this->create);
  184.  
  185.       foreach ($this->inserts as $insert{
  186.          $sql 'INSERT INTO `t1` VALUES ' $insert;
  187.          $this->db->query($sql);
  188.       }
  189.     }
  190.  
  191.     function tearDown({
  192.         $this->db->query($this->drop);
  193.         unset($this->db);
  194.     }
  195.  
  196.     // helpers
  197.     function setSql({
  198.         // we need to drop instead of truncate because we need to know
  199.         // the last inserted id in advance.
  200.         $this->drop 'DROP TABLE IF EXISTS `t1`';
  201.         $this->create 'CREATE TABLE `t1` ('
  202.                      . '`id` INT(11) NOT NULL AUTO_INCREMENT,'
  203.                      . '`name` VARCHAR(50) NOT NULL DEFAULT \'\','
  204.                      . '`group_id` INT(11) NOT NULL DEFAULT \'0\','
  205.                      . '`description` VARCHAR(255) DEFAULT NULL,'
  206.                      . '`start_date` DATE DEFAULT NULL,'
  207.                      . 'PRIMARY KEY  (`id`)'
  208.                      . ') ENGINE=InnoDB DEFAULT CHARSET=latin1;' ;
  209.       $this->inserts array(
  210.                    '(1, "one", 1, null, null)'
  211.                   ,'(2, "two", 1, null, null)'
  212.                   ,'(3, "three", 1, "description of three", "2008-02-29")'
  213.                   ,'(4, "four", 10, null, null)'
  214.                   ,'(5, "five", 10, null, null)'
  215.                   ,'(6, "six", 10, null, null)'
  216.                   ,'(7, "seven", 20, null, null)'
  217.                   ,'(8, "eight", 20, null, null)'
  218.                   ,'(9, "nine", 20, null, null)'
  219.                );
  220.     }
  221.  
  222.  
  223.     function test_query({
  224.         $sql 'select * from t1';
  225.         $this->assertTrue($this->db->query($sql)'select data that exists');
  226.  
  227.         try {
  228.             $sql2 $sql ' where groups = "40"';
  229.             $this->db->query($sql2);
  230.             $this->fail('should have caught the exception since the column "groups" does not exist');
  231.         }
  232.         catch (Exception $e{
  233.             $this->assertWantedPattern('/Unknown column \'groups\'/'$e->getMessage()$e->getMessage());
  234.         }
  235.  
  236.         $sql2 $sql ' where group_id = 40';
  237.         $this->assertTrue($this->db->query($sql2)'a select with nonexistent group_id');
  238.  
  239.     }
  240.  
  241.     function test_fetchArray({
  242.         $sql 'select * from t1 where group_id = 1';
  243.         $this->assertTrue($this->db->query($sql)"query [$sql] succeeded");
  244.  
  245.         $row $this->db->fetchArray();
  246.         $this->assertEqual(count($row)5'the row does contain 5 columns');
  247.         $this->assertEqual($row['name']'one''the first row[name] does equal "one"');
  248.  
  249.         $row $this->db->fetchArray();
  250.         $this->assertEqual($row['name']'two''the second row[name] does equal "two"');
  251.  
  252.         $row $this->db->fetchArray();
  253.         $this->assertEqual($row['id']3'the third row[id] does equal 3');
  254.         $this->assertEqual($row['name']'three''the third row[name] does equal "thre"');
  255.         $this->assertEqual($row['group_id']1'the third row[group_id] does equal 1');
  256.         $this->assertEqual($row['description']'description of three''the third row[description] does equal "description of three"');
  257.         $this->assertEqual($row['start_date']'2008-02-29''the third row[start_date] does equal "2008-02-29"');
  258.  
  259.         $row $this->db->fetchArray();
  260.         $this->assertFalse($row'there are no more rows');
  261.     }
  262.  
  263.     function test_getTableDesc({
  264.         $arr $this->db->getTableDesc('t1');
  265.         $this->assertTrue(is_array($arr)'getTableDesc(t1) return an array');
  266.         $this->assertEqual(count($arr)5'and returned 5 columns as expected');
  267.         $this->assertEqual($arr[0]['Field']'id''Field value is "id" as expected');
  268.         $this->assertEqual($arr[0]['Null']'NO''Null is empty as expected');
  269.         $this->assertEqual($arr[0]['Key']'PRI''Key contains "PRI" as expected');
  270.         $this->assertEqual($arr[0]['Default']'''Default is empty as expected');
  271.         $this->assertEqual($arr[0]['Extra']'auto_increment''Extra contains "auto_increment" as expected');
  272.         $this->assertEqual($arr[0]['Perm']'rw''Perm contains "rw" as expected');
  273.     }
  274.  
  275.     function test_getLastId({
  276.         $sql "insert into `t1` values (0, 'last one', 99, 'a short description', now())";
  277.         $this->db->query($sql);
  278.         $this->assertEqual($this->db->getLastId()10'New id equals 10 as expected');
  279.     }
  280.  
  281.     function test_transaction_and_rollback({
  282.         $insert98 'INSERT INTO `t1` VALUES (98, "quatre-vingt dix-huit", 1,null,null)';
  283.         $select98 'SELECT * FROM `t1` WHERE id = 98';
  284.         $insert99 'INSERT INTO `t1` VALUES (99, "quatre-vingt dix-neuf", 1,null,null)';
  285.         $select99 'SELECT * FROM `t1` WHERE id = 99';
  286.  
  287.         $this->db->beginTransaction();
  288.         $this->db->query($insert98);
  289.         $this->db->commit();
  290.         $this->db->query($select98);
  291.         $res $this->db->fetchArray();
  292.         $this->assertEqual($res['id']98);
  293.  
  294.         $this->db->beginTransaction();
  295.         $this->db->query($insert99);
  296.         $this->db->rollback();
  297.         $this->db->query($select99);
  298.         $res $this->db->fetchArray();
  299.         $this->assertEqual($res['id'],NULL);
  300.     }
  301. }
  302.  
  303. // run the tests if called individually
  304. if (!defined('PMO_TEST_SUITE')) {
  305.     $level '';
  306.     if (isset($_GET['level']))
  307.         $level $_GET['level'];
  308.  
  309.     $test new TestSuite('DBMB MySQLi Tests');
  310.     $test->add(new PMO_Dbms_Mysqli_Connection_Test);
  311.     $test->add(new PMO_Dbms_Mysqli_Test);
  312.     $test->run(new PMO_HTMLReporter($level));
  313. }

Documentation generated on Wed, 15 Oct 2008 09:15:54 -0400 by phpDocumentor 1.4.1