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

Source for file PMO_Dbms_Mysql_Test.php

Documentation is available at PMO_Dbms_Mysql_Test.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_Dbms_Mysql 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: 409 $
  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_Mysql 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_Mysql_Connection_Test extends UnitTestCase
  60. {
  61.     /**
  62.      * constructor calls parent contructor
  63.      */
  64.     function PMO_Dbms_Mysql_Connection_Test({
  65.         $this->UnitTestCase();
  66.     }
  67.  
  68.     /**
  69.      * this gets called before each test
  70.      */
  71.     function setUp({
  72.         $this->authdb array(
  73.             'driver' => 'mysql',
  74.             'pdodriver' => 'mysql',
  75.             'host' => 'localhost',
  76.             'user' => 'pmo',
  77.             'pass' => 'pmo',
  78.             'base' => 'test',
  79.             'dsn' => ''
  80.         );
  81.     }
  82.  
  83.     /**
  84.      * this method is called after each test
  85.      */
  86.     function tearDown(}
  87.  
  88.     /***********************
  89.      * sait pas encore pourquoi mais j'arrive pas à catcher les errors mysql
  90.      * TODO WORK THIS OUT!!!
  91.      */
  92.     function test_connect_with_bad_host({
  93.  
  94.         $db new PMO_Dbms_Mysql();
  95.         $this->authdb['host''qwerty';
  96.  
  97.         try {
  98.             // PHP is supposed to error on this bad connection
  99.             $this->expectError();
  100.             $db->connect($this->authdb);
  101.             $this->fail('Should have catched the exception.');
  102.         }
  103.         catch(Exception $e{
  104.             $this->assertFalse($db->getDb());
  105.  
  106.             // De Louis à Nicolas: Puisque connect() lance une exceptio avec le message d'erreur de
  107.             // mysql, je me serais attendu à voir ce message. mais le message est vide. On sait
  108.             // qu'une exception est lancée puisque l'assertFalse ci-haut est exécuté mais je
  109.             // n'arrive pas à la trapper. Cet assertPattern échoue!
  110.             //$this->assertPattern('/Unknown MySQL server host/', $e->getMessage(), $e->getMessage());
  111.         }
  112.     }
  113.  
  114.     function test_connect_with_bad_user({
  115.  
  116.         $db new PMO_Dbms_mysql();
  117.         $this->authdb['user''qwerty';
  118.         
  119.         try {
  120.             // PHP is supposed to error on this bad connection
  121.             $this->expectError();
  122.             $db->connect($this->authdb);
  123.             $this->fail('Should have catched the exception.');
  124.         }
  125.         catch(Exception $e{
  126.             $this->assertFalse($db->getDb());
  127.             //$this->assertPattern('/Access denied for user \'qwerty\'/', $e->getMessage(), $e->getMessage());
  128.         }
  129.     }
  130.  
  131.     function test_connect_with_bad_pass({
  132.         // for the connection tests, we won't use $this->db
  133.         $db new PMO_Dbms_mysql();
  134.         $this->authdb['pass''qwerty';
  135.  
  136.         try {
  137.             // PHP is supposed to error on this bad connection
  138.             $this->expectError();
  139.             $db->connect($this->authdb);
  140.             $this->fail('Should have catched the exception.');
  141.         }
  142.         catch(Exception $e{
  143.             $this->assertFalse($db->getDb());
  144.             //$this->assertPattern('/Access denied for user \'pmo\'/', $e->getMessage(), $e->getMessage());
  145.         }
  146.     }
  147.  
  148.     function test_connect_with_bad_base({
  149.         // for the connection tests, we won't use $this->db
  150.         $db new PMO_Dbms_mysql();
  151.         $this->authdb['base''qwerty';
  152.  
  153.         try {
  154.             $db->connect($this->authdb);
  155.             $this->fail('Should have catched the exception.');
  156.         }
  157.         catch(Exception $e{
  158.             $this->assertPattern('/ERROR 1049/'$e->getMessage()$e->getMessage());
  159.         }
  160.     }
  161.  
  162.     function test_connect_good_connection({
  163.         // for the connection tests, we won't use $this->db
  164.         $db new PMO_Dbms_mysql();
  165.  
  166.         try {
  167.             $db->connect($this->authdb);
  168.             $this->pass('Connected');
  169.         }
  170.         catch (Exception $e{
  171.             $this->fail($e->getMessage());
  172.         }
  173.     }
  174.  
  175. }
  176.  
  177. /**
  178.  * Tests the PMO_Dbms_Mysql class
  179.  *
  180.  * This is just a sanity check. Real testing is going to be
  181.  * done with de PMO_MyDbms class.
  182.  */
  183. class PMO_Dbms_Mysql_Test extends UnitTestCase
  184. {
  185.     /** constructor calls parent contructor. */
  186.     function PMO_Dbms_Mysql_Test({
  187.         $this->UnitTestCase();
  188.     }
  189.  
  190.     function setUp({
  191.         $this->authdb array(
  192.             'driver' => 'mysqli',
  193.             'pdodriver' => 'mysqli',
  194.             'host' => 'localhost',
  195.             'user' => 'pmo',
  196.             'pass' => 'pmo',
  197.             'base' => 'test',
  198.             'dsn' => ''
  199.         );
  200.         $this->setSql();
  201.         $this->db new PMO_Dbms_Mysql();
  202.         $this->db->connect($this->authdb);
  203.         $this->db->query($this->drop);
  204.         $this->db->query($this->create);
  205.  
  206.         foreach ($this->inserts as $insert{
  207.             $sql 'INSERT INTO `t1` VALUES ' $insert;
  208.             $this->db->query($sql);
  209.         }
  210.     }
  211.  
  212.     function tearDown({
  213.         $this->db->query($this->drop);
  214.         unset($this->db);
  215.     }
  216.  
  217.     // helpers
  218.     function setSql({
  219.         // we need to drop instead of truncate because we need to know
  220.         // the last inserted id in advance.
  221.         $this->drop 'DROP TABLE IF EXISTS `t1`';
  222.         $this->create 'CREATE TABLE `t1` ('
  223.                      . '`id` INT(11) NOT NULL AUTO_INCREMENT,'
  224.                      . '`name` VARCHAR(50) NOT NULL DEFAULT \'\','
  225.                      . '`group_id` INT(11) NOT NULL DEFAULT \'0\','
  226.                      . '`description` VARCHAR(255) DEFAULT NULL,'
  227.                      . '`start_date` DATE DEFAULT NULL,'
  228.                      . 'PRIMARY KEY  (`id`)'
  229.                      . ') ENGINE=InnoDB DEFAULT CHARSET=latin1;' ;
  230.         $this->inserts array(
  231.                          '(1, "one", 1, null, null)'
  232.                         ,'(2, "two", 1, null, null)'
  233.                         ,'(3, "three", 1, "description of three", "2008-02-29")'
  234.                         ,'(4, "four", 10, null, null)'
  235.                         ,'(5, "five", 10, null, null)'
  236.                         ,'(6, "six", 10, null, null)'
  237.                         ,'(7, "seven", 20, null, null)'
  238.                         ,'(8, "eight", 20, null, null)'
  239.                         ,'(9, "nine", 20, null, null)'
  240.                     );
  241.     }
  242.  
  243.  
  244.     function test_query({
  245.         $sql 'select * from t1';
  246.         $this->assertTrue($this->db->query($sql)'select data that exists');
  247.  
  248.         try {
  249.             $sql2 $sql ' where groups = "40"';
  250.             $this->db->query($sql2);
  251.             $this->fail('should have caught the exception since the column "groups" does not exist');
  252.         }
  253.         catch (Exception $e{
  254.             $this->assertPattern('/Unknown column \'groups\'/'$e->getMessage()$e->getMessage());
  255.         }
  256.  
  257.         $sql2 $sql ' where group_id = 40';
  258.         $this->assertTrue($this->db->query($sql2)'a select with nonexistent group_id');
  259.  
  260.     }
  261.  
  262.     function test_fetchArray({
  263.         $sql 'select * from t1 where group_id = 1';
  264.         $this->assertTrue($this->db->query($sql)"query [$sql] succeeded");
  265.  
  266.         $row $this->db->fetchArray();
  267.         $this->assertEqual(count($row)5'the row does contain 5 columns');
  268.         $this->assertEqual($row['name']'one''the first row[name] does equal "one"');
  269.  
  270.         $row $this->db->fetchArray();
  271.         $this->assertEqual($row['name']'two''the second row[name] does equal "two"');
  272.  
  273.         $row $this->db->fetchArray();
  274.         $this->assertEqual($row['id']3'the third row[id] does equal 3');
  275.         $this->assertEqual($row['name']'three''the third row[name] does equal "three"');
  276.         $this->assertEqual($row['group_id']1'the third row[group_id] does equal 1');
  277.         $this->assertEqual($row['description']'description of three''the third row[description] does equal "description of three"');
  278.         $this->assertEqual($row['start_date']'2008-02-29''the third row[start_date] does equal "2008-02-29"');
  279.  
  280.         $row $this->db->fetchArray();
  281.         $this->assertFalse($row'there are no more rows');
  282.     }
  283.  
  284.     function test_getTableDesc({
  285.         $arr $this->db->getTableDesc('t1');
  286.         $this->assertTrue(is_array($arr)'getTableDesc(t1) return an array');
  287.         $this->assertEqual(count($arr)5'and returned 5 columns as expected');
  288.         $this->assertEqual($arr[0]['Field']'id''Field value is "id" as expected');
  289.         $this->assertEqual($arr[0]['Null']'NO''Null is empty as expected');
  290.         $this->assertEqual($arr[0]['Key']'PRI''Key contains "PRI" as expected');
  291.         $this->assertEqual($arr[0]['Default']'''Default is empty as expected');
  292.         $this->assertEqual($arr[0]['Extra']'auto_increment''Extra contains "auto_increment" as expected');
  293.         $this->assertEqual($arr[0]['Perm']'rw''Perm contains "rw" as expected');
  294.     }
  295.  
  296.     function test_getLastId({
  297.         $sql "insert into `t1` values (0, 'last one', 99, 'a short description', now())";
  298.         $this->db->query($sql);
  299.         $this->assertEqual($this->db->getLastId()10'New id equals 10 as expected');
  300.     }
  301.  
  302.     function test_transaction_and_rollback({
  303.         $insert98 'INSERT INTO `t1` VALUES (98, "quatre-vingt dix-huit", 1,null,null)';
  304.         $select98 'SELECT * FROM `t1` WHERE id = 98';
  305.         $insert99 'INSERT INTO `t1` VALUES (99, "quatre-vingt dix-neuf", 1,null,null)';
  306.         $select99 'SELECT * FROM `t1` WHERE id = 99';
  307.  
  308.         $this->db->beginTransaction();
  309.         $this->db->query($insert98);
  310.         $this->db->commit();
  311.         $this->db->query($select98);
  312.         $res $this->db->fetchArray();
  313.         $this->assertEqual($res['id']98);
  314.  
  315.         $this->db->beginTransaction();
  316.         $this->db->query($insert99);
  317.         $this->db->rollback();
  318.         $this->db->query($select99);
  319.         $res $this->db->fetchArray();
  320.         $this->assertEqual($res['id'],NULL);
  321.     }
  322. }
  323.  
  324. // run the tests if called individually
  325. if (!defined('PMO_TEST_SUITE')) {
  326.     $level '';
  327.     if (isset($_GET['level']))
  328.         $level $_GET['level'];
  329.  
  330.     $test new TestSuite('DBMB MySQL Tests');
  331.     $test->add(new PMO_Dbms_Mysql_Connection_Test);
  332.     $test->add(new PMO_Dbms_Mysql_Test);
  333.     $test->run(new PMO_HTMLReporter($level));
  334. }

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