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

Source for file PMO_Dbms_Sqlite_Test.php

Documentation is available at PMO_Dbms_Sqlite_Test.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_Dbms_Sqlite 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 libraries we want to test
  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_Sqlite.php');
  52.  
  53. /**
  54.  * This class tests the PMO_Dbms_Sqlite Connection.
  55.  *
  56.  * these are just sanity checks. Real testing is done
  57.  * on PMO_MyDbms
  58.  */
  59. class PMO_Dbms_Sqlite_Connection_Test extends UnitTestCase 
  60. {
  61.    /** constructor calls parent contructor. */
  62.    {
  63.       $this->UnitTestCase();
  64.    }
  65.  
  66.    function setUp()
  67.    {
  68.    }
  69.  
  70.    function tearDown()
  71.    {
  72.    }
  73.  
  74.    function test_connect_with_bad_path()
  75.    {
  76.       $db new PMO_Dbms_sqlite();
  77.       $file dirname(__FILE__).DS.'BAD_DIR'.DS.'test.db';
  78.       $authdb array('dsn' => 'sqlite:'.$file);
  79.  
  80.       try {
  81.          $db->connect($authdb);
  82.          $this->fail(file_exists($file)"The path $file does not exist. This should have thrown an exception!");
  83.       }
  84.       catch (PDOException $e{
  85.          $this->pass($e->getMessage().'. file $file does not exist. Connect threw a exception');
  86.       }
  87.    }
  88.  
  89.  
  90.    {
  91.       $db new PMO_Dbms_sqlite();
  92.       $file dirname(__FILE__)DS.'test.db';
  93.       $authdb array('dsn' => 'sqlite:'.$file);
  94.  
  95.       try {
  96.          $db->connect($authdb);
  97.          $this->assertTrue(file_exists($file)"The test database $file now exists");
  98.       }
  99.       catch (PDOException $e{
  100.          $this->fail($e->getMessage());
  101.       }
  102.       @unlink($file);
  103.    }
  104.  
  105. }
  106.  
  107. /**
  108.  * This tests the PMO_Dbms_Sqlite class.
  109.  *
  110.  * these are just sanity checks. Real testing is done
  111.  * on PMO_MyDbms
  112.  */
  113. class PMO_Dbms_Sqlite_Test extends UnitTestCase 
  114. {
  115.    /** constructor calls parent contructor. */
  116.    function PMO_Dbms_Sqlite_Test()
  117.    {
  118.       $this->UnitTestCase();
  119.       $this->setSql();
  120.    }
  121.  
  122.    function setUp()
  123.     {
  124.       $this->dbfile dirname(__FILE__)DS .'test.db';
  125.       $this->authdb['dsn''sqlite:'.$this->dbfile;
  126.  
  127.       $this->db new PMO_Dbms_Sqlite();
  128.       $this->db->connect($this->authdb);
  129.         $this->db->query($this->drop);
  130.       $this->db->query($this->create);
  131.  
  132.       foreach ($this->inserts as $insert{
  133.          $sql 'INSERT INTO `t1` VALUES ' $insert;
  134.          $this->db->query($sql);
  135.       }
  136.    }
  137.  
  138.    function tearDown()
  139.    {
  140.         $this->db->query($this->drop);
  141.       unset($this->db);
  142.    }
  143.  
  144.    // helpers
  145.    function setSql()
  146.    {
  147.         $this->drop 'DROP TABLE IF EXISTS `t1`';
  148.       $this->create 'CREATE TABLE IF NOT EXISTS `t1` ('
  149.                 . '`id` INTEGER PRIMARY KEY AUTOINCREMENT,'
  150.                 . '`name` TEXT NOT NULL,'
  151.                 . '`group_id` INTEGER NOT NULL,'
  152.                 . '`description` TEXT DEFAULT NULL,'
  153.                 . '`start_date` DATE DEFAULT NULL'
  154.                 . ')' ;
  155.  
  156.       $this->inserts array(
  157.                    '(1, "one", 1, null, null)'
  158.                   ,'(2, "two", 1, null, null)'
  159.                   ,'(3, "three", 1, "description of three", "2008-02-29")'
  160.                   ,'(4, "four", 10, null, null)'
  161.                   ,'(5, "five", 10, null, null)'
  162.                   ,'(6, "six", 10, null, null)'
  163.                   ,'(7, "seven", 20, null, null)'
  164.                   ,'(8, "eight", 20, null, null)'
  165.                   ,'(9, "nine", 20, null, null)'
  166.                );
  167.  
  168.    }
  169.  
  170.  
  171.    function test_query()
  172.    {
  173.       $sql 'select * from t1';
  174.       $this->assertTrue($this->db->query($sql)'select data that exists');
  175.  
  176.       try {
  177.          $sql2 $sql ' where groups = "40"';
  178.          $this->db->query($sql2);
  179.          $this->fail('should have caught the exception since the column do not exist');
  180.       }
  181.       catch (Exception $e{
  182.          $this->assertWantedPattern('/no such column: groups/'$e->getMessage()$e->getMessage());
  183.       }
  184.  
  185.       $sql2 $sql ' where group_id = 40';
  186.       $this->assertTrue($this->db->query($sql2)'a select with nonexistent group_id');
  187.    }
  188.  
  189.    function test_fetchArray()
  190.    {
  191.       $sql 'select * from t1 where group_id = 1';
  192.       $this->assertTrue($this->db->query($sql)"query [$sql] succeeded");
  193.  
  194.       $row $this->db->fetchArray();
  195.       $this->assertEqual(count($row)5'the row does contain 5 columns');
  196.       $this->assertEqual($row['name']'one''the first row[name] does equal "one"');
  197.  
  198.       $row $this->db->fetchArray();
  199.       $this->assertEqual($row['name']'two''the second row[name] does equal "two"');
  200.  
  201.       $row $this->db->fetchArray();
  202.       $this->assertEqual($row['id']3'the third row[id] does equal 3');
  203.       $this->assertEqual($row['name']'three''the third row[name] does equal "three"');
  204.       $this->assertEqual($row['group_id']1'the third row[group_id] does equal 1');
  205.       $this->assertEqual($row['description']'description of three''the third row[description] does equal "description of three"');
  206.       $this->assertEqual($row['start_date']'2008-02-29''the third row[start_date] does equal "2008-02-29"');
  207.  
  208.       $row $this->db->fetchArray();
  209.       $this->assertFalse($row'there are no more rows');
  210.    }
  211.  
  212.    function test_getTableDesc()
  213.    {
  214.       $arr $this->db->getTableDesc('t1');
  215.       $this->assertTrue(is_array($arr)'getTableDesc(t1) returned an array');
  216.       $this->assertEqual(count($arr)5'and returned 5 columns as expected');
  217.       $this->assertEqual($arr[0]['Field']'id''Field value is "id" as expected');
  218.       $this->assertEqual($arr[0]['Null']'NO''Null equals "NO" as expected');
  219.       $this->assertEqual($arr[0]['Key']'PRI''Key contains "PRI" as expected');
  220.       $this->assertEqual($arr[0]['Default']'''Default is empty as expected');
  221.       $this->assertEqual($arr[0]['Extra']'auto_increment''Extra contains "auto_increment" as expected');
  222.       $this->assertEqual($arr[0]['Perm']'rw''Perm contains "rw" as expected');
  223.    }
  224.  
  225.    function test_getLastId()
  226.    {
  227.       $now date('Y-m-d H:i:s');
  228.       $sql "insert into `t1` values (null, 'last one', 99, 'a short description', '$now')";
  229.       $this->db->query($sql);
  230.       $this->assertEqual($this->db->getLastId()10'New id equals 10 as expected');
  231.    }
  232.  
  233.         $insert98 'INSERT INTO `t1` VALUES (98, "quatre-vingt dix-huit", 1,null,null)';
  234.         $select98 'SELECT * FROM `t1` WHERE id = 98';
  235.         $insert99 'INSERT INTO `t1` VALUES (99, "quatre-vingt dix-neuf", 1,null,null)';
  236.         $select99 'SELECT * FROM `t1` WHERE id = 99';
  237.  
  238.         $this->db->beginTransaction();
  239.         $this->db->query($insert98);
  240.         $this->db->commit();
  241.         $this->db->query($select98);
  242.         $res $this->db->fetchArray();
  243.         $this->assertEqual($res['id']98);
  244.  
  245.         $this->db->beginTransaction();
  246.         $this->db->query($insert99);
  247.         $this->db->rollback();
  248.         $this->db->query($select99);
  249.         $res $this->db->fetchArray();
  250.         $this->assertEqual($res['id'],NULL);
  251.     }
  252. }
  253.  
  254. // run the tests if called individually
  255. if (!defined('PMO_TEST_SUITE')) {
  256.     $extended '';
  257.     if (isset($_GET['e']))
  258.         $extended $_GET['e'];
  259.  
  260.     $test new TestSuite('Sqlite Tests');
  261.     $test->add(new PMO_Dbms_Sqlite_Connection_Test);
  262.     $test->add(new PMO_Dbms_Sqlite_Test);
  263.     $test->run(new PMO_HTMLReporter($extended));
  264. }

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