| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 require_once('limb/dbal/src/drivers/lmbDbTypeInfo.class.php'); 10 lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php'); 11 lmb_require('limb/dbal/src/lmbTableGateway.class.php'); 12 13 class lmbTableGatewayTest extends UnitTestCase 14 { 15 var $conn = null; 16 var $db_table_test = null; 17 18 function setUp() 19 { 20 $toolkit = lmbToolkit :: save(); 21 $this->conn = $toolkit->getDefaultDbConnection(); 22 $this->db_table_test = new lmbTableGateway('test_db_table', $this->conn); 23 24 $this->_cleanUp(); 25 } 26 27 function tearDown() 28 { 29 $this->_cleanUp(); 30 31 lmbToolkit :: restore(); 32 } 33 34 function _cleanUp() 35 { 36 $stmt = $this->conn->newStatement('DELETE FROM test_db_table'); 37 $stmt->execute(); 38 } 39 40 function testCorrectTableProperties() 41 { 42 $this->assertEqual($this->db_table_test->getTableName(), 'test_db_table'); 43 $this->assertEqual($this->db_table_test->getPrimaryKeyName(), 'id'); 44 $this->assertEqual($this->db_table_test->getColumnType('id'), LIMB_DB_TYPE_INTEGER); 45 $this->assertIdentical($this->db_table_test->getColumnType('no_column'), false); 46 $this->assertTrue($this->db_table_test->hasColumn('id')); 47 $this->assertTrue($this->db_table_test->hasColumn('description')); 48 $this->assertTrue($this->db_table_test->hasColumn('title')); 49 $this->assertFalse($this->db_table_test->hasColumn('no_such_a_field')); 50 } 51 52 function testInsert() 53 { 54 $id = $this->db_table_test->insert(array('title' => 'wow', 55 'description' => 'wow!', 56 'junk!!!' => 'junk!!!')); 57 58 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table"); 59 $record = $stmt->getOneRecord(); 60 61 $this->assertEqual($record->get('title'), 'wow'); 62 $this->assertEqual($record->get('description'), 'wow!'); 63 $this->assertEqual($record->get('id'), $id); 64 } 65 66 function testInsertUpdatesSequenceIfAutoIncrementFieldWasSet() 67 { 68 $id = $this->db_table_test->insert(array('id' => 4, 'title' => 'wow', 'description' => 'wow!')); 69 $this->assertEqual($id, 4); 70 } 71 72 function testInsertThrowsExceptionIfAllFieldsWereFiltered() 73 { 74 try 75 { 76 $this->db_table_test->insert(array('junk!!!' => 'junk!!!')); 77 $this->assertTrue(false); 78 } 79 catch(lmbException $e){} 80 } 81 82 function testUpdateAll() 83 { 84 $this->db_table_test->insert(array('title' => 'wow', 'description' => 'description')); 85 $this->db_table_test->insert(array('title' => 'wow', 'description' => 'description2')); 86 87 $this->db_table_test->update(array('description' => 'new_description', 88 'junk!!!' => 'junk!!!')); 89 90 $this->assertEqual($this->db_table_test->getAffectedRowCount(), 2); 91 92 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table"); 93 $records = $stmt->getRecordSet(); 94 95 $records->rewind(); 96 $record = $records->current(); 97 $this->assertEqual($record->get('description'), 'new_description'); 98 99 $records->next(); 100 $record = $records->current(); 101 $this->assertEqual($record->get('description'), 'new_description'); 102 } 103 104 function testUpdateAllWithRawSet() 105 { 106 $this->db_table_test->insert(array('ordr' => '1')); 107 $this->db_table_test->insert(array('ordr' => '10')); 108 109 $this->db_table_test->update('ordr=ordr+1'); 110 111 $this->assertEqual($this->db_table_test->getAffectedRowCount(), 2); 112 113 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table"); 114 $records = $stmt->getRecordSet(); 115 116 $records->rewind(); 117 $record = $records->current(); 118 $this->assertEqual($record->get('ordr'), '2'); 119 120 $records->next(); 121 $record = $records->current(); 122 $this->assertEqual($record->get('ordr'), '11'); 123 } 124 125 function testUpdateByCriteria() 126 { 127 $this->db_table_test->insert(array('title' => 'wow', 'description' => 'description'));//should be changed 128 $this->db_table_test->insert(array('title' => 'wow', 'description' => 'description2'));//should be changed 129 $this->db_table_test->insert(array('title' => 'yo', 'description' => 'description3')); 130 131 $this->db_table_test->update( 132 array('description' => 'new_description', 133 'title' => 'wow2', 134 'junk!!!' => 'junk!!!'), 135 new lmbSQLFieldCriteria('title', 'wow')); 136 137 $this->assertEqual($this->db_table_test->getAffectedRowCount(), 2); 138 139 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table ORDER BY id"); 140 $records = $stmt->getRecordSet(); 141 142 $records->rewind(); 143 $record = $records->current(); 144 $this->assertEqual($record->get('description'), 'new_description'); 145 $this->assertEqual($record->get('title'), 'wow2'); 146 147 $records->next(); 148 $record = $records->current(); 149 $this->assertEqual($record->get('description'), 'new_description'); 150 $this->assertEqual($record->get('title'), 'wow2'); 151 } 152 153 function testUpdateById() 154 { 155 $id = $this->db_table_test->insert(array('id' => null, 'title' => 'wow', 'description' => 'description')); 156 $this->db_table_test->insert(array('id' => null, 'title' => 'wow2', 'description' => 'description2')); 157 158 $this->db_table_test->updateById($id, array('description' => 'new_description')); 159 160 $this->assertEqual($this->db_table_test->getAffectedRowCount(), 1); 161 162 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table WHERE id=$id"); 163 $records = $stmt->getRecordSet(); 164 $records->rewind(); 165 $record = $records->current(); 166 $this->assertEqual($record->get('description'), 'new_description'); 167 } 168 169 function testSelectAll() 170 { 171 $data = array( 172 0 => array('title' => 'wow', 'description' => 'description'), 173 1 => array('title' => 'wow', 'description' => 'description2') 174 ); 175 176 $this->db_table_test->insert($data[0]); 177 $this->db_table_test->insert($data[1]); 178 179 $result = $this->db_table_test->select(); 180 181 $this->assertEqual($result->count(), 2); 182 183 $result->rewind(); 184 $record = $result->current(); 185 $this->assertEqual($record->get('description'), 'description'); 186 187 $result->next(); 188 $record = $result->current(); 189 $this->assertEqual($record->get('description'), 'description2'); 190 } 191 192 function testSelectAllLimitFields() 193 { 194 $this->db_table_test->insert(array('title' => 'wow', 'description' => 'description')); 195 196 $result = $this->db_table_test->select(null, array(), array('title')); 197 198 $this->assertEqual($result->count(), 1); 199 200 $this->assertEqual($result->at(0)->get('title'), 'wow'); 201 $this->assertNull($result->at(0)->get('description')); 202 } 203 204 function testSelectRecordById() 205 { 206 $data = array( 207 0 => array('title' => 'wow', 'description' => 'description'), 208 1 => array('title' => 'wow!', 'description' => 'description2') 209 ); 210 211 $this->db_table_test->insert($data[0]); 212 $id = $this->db_table_test->insert($data[1]); 213 214 $record = $this->db_table_test->selectRecordById($id); 215 $this->assertEqual($record->get('description'), 'description2'); 216 } 217 218 function testSelectRecordByIdLimitFields() 219 { 220 $id = $this->db_table_test->insert(array('title' => 'wow', 'description' => 'description')); 221 222 $record = $this->db_table_test->selectRecordById($id, array('title')); 223 $this->assertEqual($record->get('title'), 'wow'); 224 $this->assertNull($record->get('description')); 225 } 226 227 function testSelectRecordByIdNotFound() 228 { 229 $this->assertNull($this->db_table_test->selectRecordById(1)); 230 } 231 232 function testSelectFirstRecord() 233 { 234 $data = array( 235 0 => array('title' => 'wow', 'description' => 'description'), 236 1 => array('title' => 'wow!', 'description' => 'description2') 237 ); 238 239 $this->db_table_test->insert($data[0]); 240 $this->db_table_test->insert($data[1]); 241 242 $record = $this->db_table_test->selectFirstRecord(); 243 $this->assertEqual($record->get('title'), 'wow'); 244 } 245 246 function testSelectFirstRecordLimitFields() 247 { 248 $id = $this->db_table_test->insert(array('title' => 'wow', 'description' => 'description')); 249 250 $record = $this->db_table_test->selectFirstRecord(null, array(), array('title')); 251 $this->assertEqual($record->get('title'), 'wow'); 252 $this->assertNull($record->get('description')); 253 } 254 255 function testSelectFirstRecordReturnNullIfNothingIsFound() 256 { 257 $this->assertNull($this->db_table_test->selectFirstRecord('id = -10000')); 258 } 259 260 function testDeleteAll() 261 { 262 $data = array( 263 0 => array('title' => 'wow', 'description' => 'description'), 264 1 => array('title' => 'wow!', 'description' => 'description2') 265 ); 266 267 $this->db_table_test->insert($data[0]); 268 $this->db_table_test->insert($data[1]); 269 270 $this->db_table_test->delete(); 271 272 $this->assertEqual($this->db_table_test->getAffectedRowCount(), 2); 273 274 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table"); 275 $records = $stmt->getRecordSet(); 276 277 $this->assertEqual($records->count(), 0); 278 } 279 280 function testDeleteByCriteria() 281 { 282 $data = array( 283 0 => array('title' => 'wow', 'description' => 'description'), 284 1 => array('title' => 'wow!', 'description' => 'description2') 285 ); 286 287 $this->db_table_test->insert($data[0]); 288 $this->db_table_test->insert($data[1]); 289 290 $this->db_table_test->delete(new lmbSQLFieldCriteria('title', 'hey')); 291 292 $this->assertEqual($this->db_table_test->getAffectedRowCount(), 0); 293 294 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table"); 295 $records = $stmt->getRecordSet(); 296 297 $this->assertEqual($records->count(), 2); 298 } 299 300 function testDeleteById() 301 { 302 $data = array( 303 0 => array('title' => 'wow', 'description' => 'description'), 304 1 => array('title' => 'wow!', 'description' => 'description2') 305 ); 306 307 $id = $this->db_table_test->insert($data[0]); 308 $this->db_table_test->insert($data[1]); 309 310 $this->db_table_test->deleteById($id); 311 312 $stmt = $this->conn->newStatement("SELECT * FROM test_db_table"); 313 $records = $stmt->getRecordSet(); 314 315 $this->assertEqual($records->count(), 1); 316 317 $records->rewind(); 318 319 $record = $records->current(); 320 $this->assertEqual($record->get('title'), 'wow!'); 321 } 322 323 function testGetColumnsForSelectDefaultName() 324 { 325 $this->assertEqual($this->db_table_test->getColumnsForSelect(), array('test_db_table.id' => 'id', 326 'test_db_table.description' => 'description', 327 'test_db_table.title' => 'title', 328 'test_db_table.ordr' => 'ordr')); 329 } 330 331 function testGetColumnsForSelectSpecificNameAndPrefix() 332 { 333 $this->assertEqual($this->db_table_test->getColumnsForSelect('tn', array(), '_'), 334 array('tn.id' => '_id', 335 'tn.description' => '_description', 336 'tn.title' => '_title', 337 'tn.ordr' => '_ordr')); 338 } 339 340 function testGetColumnsForSelectSpecificNameWithExcludes() 341 { 342 $this->assertEqual($this->db_table_test->getColumnsForSelect('tn', array('id', 'description')), 343 array('tn.title' => 'title', 'tn.ordr' => 'ordr')); 344 345 } 346 } 347 ?>