[ Index ] |
PHP Cross Reference of Web Application Component Toolkit |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package WACT_TESTS 4 * @version $Id: driver.inc.php,v 1.14 2004/06/30 10:18:53 harryf Exp $ 5 */ 6 7 require_once TEST_CASES . '/util/datasource.inc.php'; 8 9 SimpleTestOptions::ignore('DriverConnectionTestCase'); 10 SimpleTestOptions::ignore('DriverRecordTestCase'); 11 SimpleTestOptions::ignore('DriverRecordSetTestCase'); 12 13 // Template interface for mock object 14 class TestPager { 15 function setPagedDataSet(&$dataset) { 16 } 17 18 function getStartingItem() { 19 } 20 21 function getItemsPerPage() { 22 } 23 } 24 25 Mock::generate('TestPager', 'MockPager'); 26 27 28 /** 29 * @package WACT_TESTS 30 */ 31 class DriverConnectionTestCase extends UnitTestCase { 32 var $connection; 33 34 function tearDown() { 35 $this->connection->disconnect(); 36 unset($this->connection); 37 } 38 39 function DriverConnectionTestCase($name = 'DriverConnectionTestCase test cases') { 40 $this->UnitTestCase($name); 41 } 42 43 function testMakeLiteralNULL() { 44 $var = NULL; 45 $literal = $this->connection->makeLiteral($var); 46 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 47 if ( !method_exists($record,'get') ) { 48 $this->fail('Invalid return value [#1]'); 49 } else { 50 $this->assertIdentical($record->get('test'), $var); 51 } 52 } 53 54 function testMakeLiteralInteger() { 55 $var = 1; 56 $literal = $this->connection->makeLiteral($var); 57 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 58 if ( !method_exists($record,'get') ) { 59 $this->fail('Invalid return value [#2]'); 60 } else { 61 $this->assertEqual($record->get('test'), $var); 62 } 63 64 $var = 0; 65 $literal = $this->connection->makeLiteral($var, 'int'); 66 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 67 if ( !method_exists($record,'get') ) { 68 $this->fail('Invalid return value [#3]'); 69 } else { 70 $this->assertEqual($record->get('test'), $var); 71 } 72 73 $var = "2"; 74 $literal = $this->connection->makeLiteral($var, 'int'); 75 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 76 77 if ( !method_exists($record,'get') ) { 78 $this->fail('Invalid return value [#4]'); 79 } else { 80 $this->assertEqual($record->get('test'), 2); 81 } 82 83 $var = NULL; 84 $literal = $this->connection->makeLiteral($var, 'int'); 85 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 86 if ( !method_exists($record,'get') ) { 87 $this->fail('Invalid return value [#5]'); 88 } else { 89 $this->assertEqual($record->get('test'), $var); 90 } 91 } 92 93 function testMakeLiteralFloat() { 94 $var = 3.14; 95 $literal = $this->connection->makeLiteral($var); 96 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 97 if ( !method_exists($record,'get') ) { 98 $this->fail('Invalid return value [#6]'); 99 } else { 100 $this->assertEqual($record->get('test'), $var); 101 } 102 103 $var = 3.14; 104 $literal = $this->connection->makeLiteral($var, 'float'); 105 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 106 if ( !method_exists($record,'get') ) { 107 $this->fail('Invalid return value [#7]'); 108 } else { 109 $this->assertEqual($record->get('test'), $var); 110 } 111 112 $var = NULL; 113 $literal = $this->connection->makeLiteral($var, 'float'); 114 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 115 if ( !method_exists($record,'get') ) { 116 $this->fail('Invalid return value [#8]'); 117 } else { 118 $this->assertEqual($record->get('test'), $var); 119 } 120 } 121 122 function testMakeLiteralBoolean() { 123 $var = TRUE; 124 125 $literal = $this->connection->makeLiteral($var); 126 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 127 if ( !method_exists($record,'get') ) { 128 $this->fail('Invalid return value [#9]'); 129 } else { 130 $this->assertEqual($record->get('test'), (boolean) $var); 131 } 132 133 $literal = $this->connection->makeLiteral($var, 'boolean'); 134 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 135 if ( !method_exists($record,'get') ) { 136 $this->fail('Invalid return value [#10]'); 137 } else { 138 $this->assertEqual($record->get('test'), (boolean) $var); 139 } 140 141 $var = FALSE; 142 143 $literal = $this->connection->makeLiteral($var); 144 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 145 if ( !method_exists($record,'get') ) { 146 $this->fail('Invalid return value [#11]'); 147 } else { 148 $this->assertEqual($record->get('test'), (boolean) $var); 149 } 150 151 $literal = $this->connection->makeLiteral($var, 'boolean'); 152 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 153 if ( !method_exists($record,'get') ) { 154 $this->fail('Invalid return value [#12]'); 155 } else { 156 $this->assertEqual($record->get('test'), (boolean) $var); 157 } 158 159 $var = NULL; 160 $literal = $this->connection->makeLiteral($var, 'boolean'); 161 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 162 if ( !method_exists($record,'get') ) { 163 $this->fail('Invalid return value [#13]'); 164 } else { 165 $this->assertEqual($record->get('test'), $var); 166 } 167 } 168 169 function testMakeLiteralString() { 170 $StringList = array("Hello 'World!'", 171 '"', '\'', '\\', '\\"', '\\\'', '\\0', '\\1', 172 "%", "_", '&', '<', '>', '$', '`'); 173 foreach ($StringList as $str) { 174 $literal = $this->connection->makeLiteral($str); 175 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 176 if ( !method_exists($record,'get') ) { 177 $this->fail('Invalid return value [#14]'); 178 } else { 179 $this->assertEqual($record->get('test'), $str); 180 } 181 182 $literal = $this->connection->makeLiteral($str, 'string'); 183 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 184 if ( !method_exists($record,'get') ) { 185 $this->fail('Invalid return value [#15]'); 186 } else { 187 $this->assertEqual($record->get('test'), $str); 188 } 189 190 } 191 192 $var = NULL; 193 $literal = $this->connection->makeLiteral($var, 'string'); 194 $record =& $this->connection->FindRecord('SELECT ' . $literal . ' as test'); 195 if ( !method_exists($record,'get') ) { 196 $this->fail('Invalid return value [#16'); 197 } else { 198 $this->assertEqual($record->get('test'), $var); 199 } 200 } 201 202 function testFindRecord() { 203 $sql = "SELECT * FROM founding_fathers WHERE id = 1"; 204 $record =& $this->connection->findRecord($sql); 205 if ( !method_exists($record,'get') ) { 206 $this->fail('Invalid return value [#17]'); 207 } else { 208 $this->assertEqual($record->get('id'), 1); 209 $this->assertEqual($record->get('first'), 'George'); 210 $this->assertEqual($record->get('last'), 'Washington'); 211 } 212 } 213 214 function testGetOneValue() { 215 $sql = "SELECT first FROM founding_fathers"; 216 $this->assertEqual($this->connection->getOneValue($sql), 'George'); 217 } 218 219 function testGetOneColumnArray() { 220 $sql = "SELECT first FROM founding_fathers"; 221 $testarray = array('George', 'Alexander', 'Benjamin'); 222 $this->assertEqual($this->connection->getOneColumnArray($sql), $testarray); 223 } 224 225 function testGetTwoColumnArray() { 226 $sql = "SELECT id, first FROM founding_fathers"; 227 $testarray = array(1=>'George', 2=>'Alexander', 3=>'Benjamin'); 228 $this->assertEqual($this->connection->getTwoColumnArray($sql), $testarray); 229 } 230 231 } 232 233 /** 234 * @package WACT_TESTS 235 */ 236 class DriverRecordTestCase extends DataSourceTestCase { 237 var $record; 238 239 function DriverRecordTestCase($name = 'DriverRecord test cases') { 240 $this->DataSourceTestCase($name); 241 } 242 243 function setUp() { 244 $this->dataspace =& $this->connection->NewRecord(); 245 $this->record =& $this->dataspace; 246 $this->data = array('first' => 'Richard', 'last' => 'Nixon'); 247 $this->fields = array('first', 'last'); 248 } 249 250 function tearDown() { 251 $this->connection->disconnect(); 252 unset($this->connection); 253 unset($this->dataspace); 254 unset($this->record); 255 } 256 257 function checkRecord($id) { 258 $sql = "SELECT * FROM founding_fathers WHERE id = " . 259 $this->connection->makeLiteral($id); 260 $record =& $this->connection->findRecord($sql); 261 $this->assertNotNull($record); 262 if ($record) { 263 $this->assertEqual($record->get('id'), $id); 264 $this->assertEqual($record->get('first'), 'Richard'); 265 $this->assertEqual($record->get('last'), 'Nixon'); 266 } 267 } 268 269 function testInsert() { 270 $this->record->import($this->data); 271 $this->assertIdentical($this->record->insert('founding_fathers', $this->fields), TRUE); 272 $this->checkRecord(4); 273 } 274 275 function testInsertId() { 276 $this->record->import($this->data); 277 $id = $this->record->insertId('founding_fathers', $this->fields, 'id'); 278 $this->assertIdentical($id, 4); 279 $this->checkRecord(4); 280 } 281 282 function testUpdate() { 283 $this->record->import($this->data); 284 $result = $this->record->update('founding_fathers', $this->fields, 285 "id = " . $this->connection->makeLiteral(3)); 286 $this->assertIdentical($result, TRUE); 287 $this->checkRecord(3); 288 } 289 290 function testAffectedRowCount() { 291 $this->record->import($this->data); 292 $this->record->update('founding_fathers', $this->fields); 293 $this->assertEqual($this->record->getAffectedRowCount(), 3); 294 } 295 296 } 297 298 /** 299 * @package WACT_TESTS 300 */ 301 302 class DriverRecordSetTestCase extends DataSourceTestCase { 303 function DriverRecordSetTestCase($name = 'DriverRecordSet test cases') { 304 $this->DataSourceTestCase($name); 305 } 306 307 function setUp() { 308 $sql = "SELECT id, first FROM founding_fathers ORDER BY id"; 309 $this->dataspace =& $this->connection->NewRecordSet($sql); 310 $this->record =& $this->dataspace; 311 } 312 313 function tearDown() { 314 $this->connection->disconnect(); 315 unset($this->connection); 316 unset($this->dataspace); 317 unset($this->record); 318 } 319 320 function testPaginate() { 321 $mockPager = & new MockPager($this); 322 $mockPager->expectCallCount('setPagedDataSet', 1); 323 $this->record->paginate($mockPager); 324 } 325 326 function testReset() { 327 $this->assertTrue($this->record->reset()); 328 $this->assertTrue($this->record->next()); 329 $this->assertEqual($this->record->get('id'), 1); 330 $this->assertEqual($this->record->get('first'), 'George'); 331 $this->assertTrue($this->record->next()); 332 $this->assertTrue($this->record->next()); 333 $this->assertTrue($this->record->reset()); 334 $this->assertTrue($this->record->next()); 335 $this->assertEqual($this->record->get('id'), 1); 336 $this->assertEqual($this->record->get('first'), 'George'); 337 } 338 339 function testIteration() { 340 $this->record->reset(); 341 for ($i = 0; $this->record->next(); $i++); 342 $this->assertEqual($i, 3); 343 } 344 345 function testPagerIteration() { 346 $mockPager = & new MockPager($this); 347 $mockPager->expectCallCount('getStartingItem', 1); 348 $mockPager->expectCallCount('getItemsPerPage', 1); 349 $mockPager->setReturnValue('getStartingItem', 0); 350 $mockPager->setReturnValue('getItemsPerPage', 2); 351 $this->record->paginate($mockPager); 352 $this->record->reset(); 353 for ($i = 0; $this->record->next(); $i++); 354 $this->assertEqual($i, 2); 355 } 356 357 function testGetRowCount() { 358 $this->record->reset(); 359 $this->assertEqual($this->record->getRowCount(), 3); 360 } 361 362 function testGetTotalRowCount() { 363 $this->record->reset(); 364 $this->assertEqual($this->record->getTotalRowCount(), 3); 365 } 366 367 } 368 369 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 28 19:36:09 2004 | Cross-referenced by PHPXref 0.5 |