[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/tests/cases/non-driver/ -> lmbSimpleDbTest.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
  10  lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
  11  
  12  class lmbSimpleDbTest extends UnitTestCase
  13  {
  14    var $db = null;
  15    var $conn = null;
  16  
  17    function setUp()
  18    {
  19      $toolkit = lmbToolkit :: instance();
  20      $this->conn = $toolkit->getDefaultDbConnection();
  21      $this->db = new lmbSimpleDb($this->conn);
  22  
  23      $this->_cleanUp();
  24    }
  25  
  26    function tearDown()
  27    {
  28      $this->_cleanUp();
  29    }
  30  
  31    function _cleanUp()
  32    {
  33      $stmt = $this->conn->newStatement('DELETE FROM test_db_table');
  34      $stmt->execute();
  35    }
  36  
  37    function testGetType()
  38    {
  39      $this->assertEqual($this->db->getType(), $this->conn->getType());
  40      $this->assertNotNull($this->db->getType());
  41    }
  42  
  43    function testInsert()
  44    {
  45      $id = $this->db->insert('test_db_table', array('title' =>  'wow',
  46                                                     'description' => 'wow!'));
  47  
  48      $stmt = $this->conn->newStatement("SELECT * FROM test_db_table");
  49      $record = $stmt->getOneRecord();
  50  
  51      $this->assertEqual($record->get('title'), 'wow');
  52      $this->assertEqual($record->get('description'), 'wow!');
  53      $this->assertEqual($record->get('id'), $id);
  54    }
  55  
  56    //we test sequence based fields here

  57    function testInsertPrimaryKeyValue()
  58    {
  59      $id = $this->db->insert('test_db_table', array('id' => 20,
  60                                                     'title' =>  'wow',
  61                                                     'description' => 'wow!'));
  62  
  63      $stmt = $this->conn->newStatement("SELECT * FROM test_db_table");
  64      $record = $stmt->getOneRecord();
  65  
  66      $this->assertEqual($record->get('title'), 'wow');
  67      $this->assertEqual($record->get('description'), 'wow!');
  68      $this->assertEqual("$id", '20');
  69      $this->assertEqual($record->get('id'), $id);
  70    }
  71  
  72    function testUpdateAll()
  73    {
  74      $this->db->insert('test_db_table', array('title' =>  'wow', 'description' => 'description'));
  75      $this->db->insert('test_db_table', array('title' =>  'wow', 'description' => 'description2'));
  76  
  77      $this->assertEqual($this->db->update('test_db_table', array('description' =>  'new_description')), 2);
  78  
  79      $stmt = $this->conn->newStatement("SELECT * FROM test_db_table");
  80      $records = $stmt->getRecordSet();
  81  
  82      $records->rewind();
  83      $record = $records->current();
  84      $this->assertEqual($record->get('description'), 'new_description');
  85  
  86      $records->next();
  87      $record = $records->current();
  88      $this->assertEqual($record->get('description'), 'new_description');
  89    }
  90  
  91    function testUpdateByCondition()
  92    {
  93      $this->db->insert('test_db_table', array('title' =>  'wow', 'description' => 'description'));
  94      $this->db->insert('test_db_table', array('title' =>  'wow', 'description' => 'description2'));
  95      $this->db->insert('test_db_table', array('title' =>  'yo', 'description' => 'description3'));
  96  
  97      $res = $this->db->update('test_db_table',
  98                                array('description' =>  'new_description', 'title' => 'wow2'),
  99                                new lmbSQLFieldCriteria('title', 'wow'));
 100  
 101      $this->assertEqual($res, 2);
 102  
 103      $stmt = $this->conn->newStatement("SELECT * FROM test_db_table ORDER BY id");
 104      $records = $stmt->getRecordSet();
 105  
 106      $records->rewind();
 107      $record = $records->current();
 108      $this->assertEqual($record->get('description'), 'new_description');
 109      $this->assertEqual($record->get('title'), 'wow2');
 110  
 111      $records->next();
 112      $record = $records->current();
 113      $this->assertEqual($record->get('description'), 'new_description');
 114      $this->assertEqual($record->get('title'), 'wow2');
 115    }
 116  
 117    function testSelectAll()
 118    {
 119      $data = array(
 120        0 => array('title' =>  'wow', 'description' => 'description'),
 121        1 => array('title' =>  'wow', 'description' => 'description2')
 122      );
 123  
 124      $this->db->insert('test_db_table', $data[0]);
 125      $this->db->insert('test_db_table', $data[1]);
 126  
 127      $result = $this->db->select('test_db_table');
 128  
 129      $this->assertEqual($result->count(), 2);
 130  
 131      $result->rewind();
 132      $record = $result->current();
 133      $this->assertEqual($record->get('description'), 'description');
 134  
 135      $result->next();
 136      $record = $result->current();
 137      $this->assertEqual($record->get('description'), 'description2');
 138    }
 139  
 140    function testDeleteAll()
 141    {
 142      $data = array(
 143        0 => array('title' =>  'wow', 'description' => 'description'),
 144        1 => array('title' =>  'wow!', 'description' => 'description2')
 145      );
 146  
 147      $this->db->insert('test_db_table', $data[0]);
 148      $this->db->insert('test_db_table', $data[1]);
 149  
 150      $this->assertEqual($this->db->delete('test_db_table'), 2);
 151  
 152      $stmt = $this->conn->newStatement("SELECT * FROM test_db_table");
 153      $records = $stmt->getRecordSet();
 154  
 155      $this->assertEqual($records->count(), 0);
 156    }
 157  
 158    function testDeleteByStringCondition()
 159    {
 160      $data = array(
 161        0 => array('title' =>  'wow', 'description' => 'description'),
 162        1 => array('title' =>  'wow!', 'description' => 'description2')
 163      );
 164  
 165      $this->db->insert('test_db_table', $data[0]);
 166      $this->db->insert('test_db_table', $data[1]);
 167  
 168      $this->assertEqual($this->db->delete('test_db_table',
 169                                           new lmbSQLFieldCriteria('description', 'description')), 1);
 170  
 171      $stmt = $this->conn->newStatement("SELECT * FROM test_db_table");
 172      $records = $stmt->getRecordSet();
 173  
 174      $this->assertEqual($records->count(), 1);
 175    }
 176  
 177  }
 178  ?>


Generated: Mon Sep 8 04:35:41 2008 Cross-referenced by PHPXref 0.7