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

Source for file PMO_Dbms_Pdo_Test.php

Documentation is available at PMO_Dbms_Pdo_Test.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_Dbms_Pdo driver class.
  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            Louis Lapointe <laplix@gmail.com>
  29.  * @link                http://pmo.developpez.com/
  30.  * @since            PhpMyObject v0.15
  31.  * @version            $Revision: 400 $
  32.  * @copyright        Copyright (c) 2007-2008 Nicolas Boiteux
  33.  * @copyright        Copyright (c) 2008 Louis Lapointe
  34.  * @license            GPLv3 {@link http://www.gnu.org/licenses/gpl}
  35.  * @filesource
  36.  */ 
  37.  
  38. /**
  39.  * setup the test cases 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.  * requires the classes to be tested
  48.  */
  49. require_once(PMO_CORE DS 'PMO_MyDbms.php');
  50. require_once(PMO_CORE DS 'PMO_dbms_pdo.php');
  51. require_once(PMO_CORE DS 'PMO_dbms_mysql.php');
  52. require_once(PMO_CORE DS 'PMO_dbms_sqlite.php');
  53.  
  54. /**
  55.  * this tests the PMO_Dbms_Pdo connections.
  56.  *
  57.  * This is just a sanity check. Real testing is going to be
  58.  * done with de PMO_MyDbms class.
  59.  */
  60. class PMO_Dbms_Pdo_Connection_Test extends UnitTestCase
  61. {
  62.     /** constructor calls parent contructor. */
  63.     function PMO_Dbms_Pdo_Connection_Test()
  64.     {
  65.         $this->UnitTestCase();
  66.         $this->sqliteDatabase dirname(__FILE__).DS.'test.db';
  67.     }
  68.  
  69.     function setUp()
  70.     {
  71.         $this->authdb array(
  72.             'driver' => 'pdo',
  73.             'pdodriver' => '',
  74.             'dsn' => 'sqlite:'.$this->sqliteDatabase,
  75.             'host' => 'localhost',
  76.             'user' => 'pmo',
  77.             'pass' => 'pmo',
  78.             'base' => 'test'
  79.         );
  80.     }
  81.  
  82.     function tearDown({
  83.         if ($this->authdb['pdodriver'== 'sqlite'{
  84.             @unlink($this->sqliteDatabase);
  85.         }
  86.     }
  87.  
  88.     {
  89.         $db new PMO_Dbms_Pdo();
  90.         $this->authdb['pdodriver''mysql';
  91.         $this->authdb['host''qwerty';
  92.  
  93.         try {
  94.             $db->connect($this->authdb);
  95.             $this->fail('Should have catched the exception.');
  96.         }
  97.         catch(Exception $e{
  98.             $this->assertWantedPattern('/Unknown MySQL server host/'$e->getMessage()$e->getMessage());
  99.         }
  100.     }
  101.  
  102.     {
  103.         $db new PMO_Dbms_Pdo();
  104.         $this->authdb['pdodriver''mysql';
  105.         $this->authdb['user''qwerty';
  106.         
  107.         try {
  108.             $db->connect($this->authdb);
  109.             $this->fail('Should have catched the exception.');
  110.         }
  111.         catch(PDOException $e{
  112.             $this->assertWantedPattern('/Access denied for user \'qwerty\'/'$e->getMessage()$e->getMessage());
  113.         }
  114.     }
  115.  
  116.     {
  117.         $db new PMO_Dbms_Pdo();
  118.         $this->authdb['pdodriver''mysql';
  119.         $this->authdb['pass''qwerty';
  120.  
  121.         try {
  122.             $db->connect($this->authdb);
  123.             $this->fail('Should have catched the exception.');
  124.         }
  125.         catch(PDOException $e{
  126.             $this->assertWantedPattern('/Access denied for user \'pmo\'/'$e->getMessage()$e->getMessage());
  127.         }
  128.     }
  129.  
  130.     {
  131.         $db new PMO_Dbms_Pdo();
  132.         $this->authdb['pdodriver''mysql';
  133.         $this->authdb['base''qwerty';
  134.  
  135.         try {
  136.             $db->connect($this->authdb);
  137.             $this->fail('Should have catched the exception.');
  138.         }
  139.         catch(Exception $e{
  140.             $this->assertWantedPattern('/database \'qwerty\'/'$e->getMessage()$e->getMessage());
  141.         }
  142.     }
  143.  
  144.     {
  145.         $db new PMO_Dbms_Pdo();
  146.         $this->authdb['pdodriver''mysql';
  147.  
  148.         try {
  149.             $db->connect($this->authdb);
  150.             $this->pass('Connected');
  151.         }
  152.         catch (Exception $e{
  153.             $this->fail($e->getMessage());
  154.         }
  155.     }
  156.  
  157. }
  158.  
  159.  
  160. /**
  161.  * This tests the PMO_dbms_pdo class.
  162.  *
  163.  * This is just a sanity check. Real testing is going to be
  164.  * done with de PMO_MyDbms class.
  165.  */
  166. class PMO_Dbms_Pdo_Test extends UnitTestCase 
  167. {
  168.     /** constructor calls parent contructor. */
  169.     function PMO_Dbms_Pdo_Test()
  170.     {
  171.         $this->UnitTestCase();
  172.         $this->sqliteDatabase dirname(__FILE__).'/test.db';
  173.         $this->setSql();
  174.     }
  175.  
  176.     /**
  177.      * setup is called before each test is performed
  178.      */
  179.     function setUp()
  180.     {
  181.         $this->authdb array(
  182.             'driver' => 'pdo',
  183.             'pdodriver' => 'mysql',
  184.             'host' => 'localhost',
  185.             'user' => 'pmo',
  186.             'pass' => 'pmo',
  187.             'base' => 'test',
  188.             'dsn' => 'sqlite:',$this->sqliteDatabase
  189.         );
  190.  
  191.         $this->db new PMO_Dbms_Pdo();
  192.         $this->db->connect($this->authdb);
  193.         $this->db->query($this->drop);
  194.         $this->db->query($this->create);
  195.         foreach ($this->inserts as $insert{
  196.             $sql 'INSERT INTO `t1` VALUES ' $insert;
  197.             $this->db->query($sql);
  198.         }
  199.     }
  200.  
  201.     /**
  202.      * tearDown is called after each test has been performed
  203.      */
  204.     function tearDown()
  205.     {
  206.         $this->db->query($this->drop);
  207.         unset($this->db);
  208.     }
  209.  
  210.     // helpers
  211.     function setSql()
  212.     {
  213.         // we need to drop instead of truncate because we need to know
  214.         // the last inserted id.
  215.         $this->drop 'DROP TABLE IF EXISTS `t1`';
  216.         $this->create 'CREATE TABLE `t1` ('
  217.                      . '`id` INT(11) NOT NULL AUTO_INCREMENT,'
  218.                      . '`name` VARCHAR(50) NOT NULL DEFAULT \'\','
  219.                      . '`group_id` INT(11) NOT NULL DEFAULT \'0\','
  220.                      . '`description` VARCHAR(255) DEFAULT NULL,'
  221.                      . '`start_date` DATE DEFAULT NULL,'
  222.                      . 'PRIMARY KEY  (`id`)'
  223.                      . ') ENGINE=InnoDB DEFAULT CHARSET=latin1;' ;
  224.  
  225.         $this->inserts array(
  226.                          '(1, "one", 1, null, null)'
  227.                         ,'(2, "two", 1, null, null)'
  228.                         ,'(3, "three", 1, "description of four", "2008-02-29")'
  229.                         ,'(4, "four", 10, null, null)'
  230.                         ,'(5, "five", 10, null, null)'
  231.                         ,'(6, "six", 10, null, null)'
  232.                         ,'(7, "seven", 10, null, null)'
  233.                         ,'(8, "eight", 20, null, null)'
  234.                         ,'(9, "nine", 20, null, null)'
  235.                     );
  236.     }
  237.  
  238.  
  239.     function test_query()
  240.     {
  241.         $sql 'select * from t1';
  242.         $this->assertTrue($this->db->query($sql)'select data that exists');
  243.  
  244.         try {
  245.             $sql2 $sql ' where groups = "40"';
  246.             $this->db->query($sql2);
  247.             $this->fail('should have caught the exception since the column "groups" does not exist');
  248.         }
  249.         catch (Exception $e{
  250.             $this->assertWantedPattern('/Unknown column \'groups\'/'$e->getMessage())//, $e->getMessage());
  251.         }
  252.  
  253.         $sql2 $sql ' where group_id = 40';
  254.         $this->assertTrue($this->db->query($sql2)'a select with nonexistent group_id');
  255.  
  256.     }
  257.  
  258.     function test_fetchArray()
  259.     {
  260.         $sql 'select * from t1 where group_id = 1';
  261.         $this->assertTrue($this->db->query($sql)"query [$sql] succeeded");
  262.  
  263.         $row $this->db->fetchArray();
  264.         $this->assertEqual(count($row)5'the row does contain 5 columns');
  265.         $this->assertEqual($row['name']'one''the first row[name] does equal "one"');
  266.  
  267.         $row $this->db->fetchArray();
  268.         $this->assertEqual($row['name']'two''the second row[name] does equal "two"');
  269.  
  270.         $row $this->db->fetchArray();
  271.         $this->assertEqual($row['id']3'the third row[id] does equal 3');
  272.         $this->assertEqual($row['name']'three''the third row[name] does equal "three"');
  273.         $this->assertEqual($row['group_id']1'the third row[group_id] does equal 1');
  274.         $this->assertEqual($row['description']'description of four''the third row[description] does equal "description of four"');
  275.         $this->assertEqual($row['start_date']'2008-02-29''the third row[start_date] does equal "2008-02-29"');
  276.  
  277.         $row $this->db->fetchArray();
  278.         $this->assertFalse($row'there are no more rows');
  279.     }
  280.  
  281.     function test_getTableDesc()
  282.     {
  283.         $arr $this->db->getTableDesc('t1');
  284.         $this->assertTrue(is_array($arr)'getTableDesc(t1) return an array');
  285.         $this->assertEqual(count($arr)5'and returned 5 columns as expected');
  286.         $this->assertEqual($arr[0]['Field']'id''Field value is "id" as expected');
  287.         $this->assertEqual($arr[0]['Null']'NO''Null equals NO as expected');
  288.         $this->assertEqual($arr[0]['Key']'PRI''Key contains "PRI" as expected');
  289.         $this->assertEqual($arr[0]['Default']'''Default is empty as expected');
  290.         $this->assertEqual($arr[0]['Extra']'auto_increment''Extra contains "auto_increment" as expected');
  291.         $this->assertEqual($arr[0]['Perm']'rw''Perm contains "rw" as expected');
  292.     }
  293.  
  294.     function test_getLastId()
  295.     {
  296.         $sql "insert into `t1` values (0, 'last one', 99, 'a short description', now())";
  297.         $this->db->query($sql);
  298.         $this->assertEqual($this->db->getLastId()10'New id equals 10 as expected');
  299.     }
  300.     
  301.         $insert98 'INSERT INTO `t1` VALUES (98, "quatre-vingt dix-huit", 1,null,null)';
  302.         $select98 'SELECT * FROM `t1` WHERE id = 98';
  303.         $insert99 'INSERT INTO `t1` VALUES (99, "quatre-vingt dix-neuf", 1,null,null)';
  304.         $select99 'SELECT * FROM `t1` WHERE id = 99';
  305.  
  306.         $this->db->beginTransaction();
  307.         $this->db->query($insert98);
  308.         $this->db->commit();
  309.         $this->db->query($select98);
  310.         $res $this->db->fetchArray();
  311.         $this->assertEqual($res['id']98);
  312.  
  313.         $this->db->beginTransaction();
  314.         $this->db->query($insert99);
  315.         $this->db->rollback();
  316.         $this->db->query($select99);
  317.         $res $this->db->fetchArray();
  318.         $this->assertEqual($res['id'],NULL);
  319.  
  320.     }
  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('PMO_Dbms_Pdo Tests');
  331.     $test->add(new PMO_Dbms_Pdo_Connection_Test);
  332.     $test->add(new PMO_Dbms_Pdo_Test);
  333.     $test->run(new PMO_HTMLReporter($level));
  334. }

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