a tiny mvc framework for php using php-activerecord
at v1 457 lines 14 kB view raw
1<?php 2include 'helpers/config.php'; 3 4class ActiveRecordFindTest extends DatabaseTest 5{ 6 /** 7 * @expectedException ActiveRecord\RecordNotFound 8 */ 9 public function test_find_with_no_params() 10 { 11 Author::find(); 12 } 13 14 public function test_find_by_pk() 15 { 16 $author = Author::find(3); 17 $this->assert_equals(3,$author->id); 18 } 19 20 /** 21 * @expectedException ActiveRecord\RecordNotFound 22 */ 23 public function test_find_by_pkno_results() 24 { 25 Author::find(99999999); 26 } 27 28 public function test_find_by_multiple_pk_with_partial_match() 29 { 30 try 31 { 32 Author::find(1,999999999); 33 $this->fail(); 34 } 35 catch (ActiveRecord\RecordNotFound $e) 36 { 37 $this->assert_true(strpos($e->getMessage(),'found 1, but was looking for 2') !== false); 38 } 39 } 40 41 public function test_find_by_pk_with_options() 42 { 43 $author = Author::find(3,array('order' => 'name')); 44 $this->assert_equals(3,$author->id); 45 $this->assert_true(strpos(Author::table()->last_sql,'ORDER BY name') !== false); 46 } 47 48 public function test_find_by_pk_array() 49 { 50 $authors = Author::find(1,'2'); 51 $this->assert_equals(2, count($authors)); 52 $this->assert_equals(1, $authors[0]->id); 53 $this->assert_equals(2, $authors[1]->id); 54 } 55 56 public function test_find_by_pk_array_with_options() 57 { 58 $authors = Author::find(1,'2',array('order' => 'name')); 59 $this->assert_equals(2, count($authors)); 60 $this->assert_true(strpos(Author::table()->last_sql,'ORDER BY name') !== false); 61 } 62 63 /** 64 * @expectedException Exception 65 */ 66 public function test_find_nothing_with_sql_in_string() 67 { 68 Author::first('name = 123123123'); 69 } 70 71 public function test_find_all() 72 { 73 $authors = Author::find('all',array('conditions' => array('author_id IN(?)',array(1,2,3)))); 74 $this->assert_true(count($authors) >= 3); 75 } 76 77 public function test_find_all_with_no_bind_values() 78 { 79 $authors = Author::find('all',array('conditions' => array('author_id IN(1,2,3)'))); 80 $this->assert_equals(1,$authors[0]->author_id); 81 } 82 83 public function test_find_hash_using_alias() 84 { 85 $venues = Venue::all(array('conditions' => array('marquee' => 'Warner Theatre', 'city' => array('Washington','New York')))); 86 $this->assert_true(count($venues) >= 1); 87 } 88 89 public function test_find_hash_using_alias_with_null() 90 { 91 $venues = Venue::all(array('conditions' => array('marquee' => null))); 92 $this->assert_equals(0,count($venues)); 93 } 94 95 public function test_dynamic_finder_using_alias() 96 { 97 $this->assert_not_null(Venue::find_by_marquee('Warner Theatre')); 98 } 99 100 public function test_find_all_hash() 101 { 102 $books = Book::find('all',array('conditions' => array('author_id' => 1))); 103 $this->assert_true(count($books) > 0); 104 } 105 106 public function test_find_all_hash_with_order() 107 { 108 $books = Book::find('all',array('conditions' => array('author_id' => 1), 'order' => 'name DESC')); 109 $this->assert_true(count($books) > 0); 110 } 111 112 public function test_find_all_no_args() 113 { 114 $author = Author::all(); 115 $this->assert_true(count($author) > 1); 116 } 117 118 public function test_find_all_no_results() 119 { 120 $authors = Author::find('all',array('conditions' => array('author_id IN(11111111111,22222222222,333333333333)'))); 121 $this->assert_equals(array(),$authors); 122 } 123 124 public function test_find_first() 125 { 126 $author = Author::find('first',array('conditions' => array('author_id IN(?)', array(1,2,3)))); 127 $this->assert_equals(1,$author->author_id); 128 $this->assert_equals('Tito',$author->name); 129 } 130 131 public function test_find_first_no_results() 132 { 133 $this->assert_null(Author::find('first',array('conditions' => 'author_id=1111111'))); 134 } 135 136 public function test_find_first_using_pk() 137 { 138 $author = Author::find('first',3); 139 $this->assert_equals(3,$author->author_id); 140 } 141 142 public function test_find_first_with_conditions_as_string() 143 { 144 $author = Author::find('first',array('conditions' => 'author_id=3')); 145 $this->assert_equals(3,$author->author_id); 146 } 147 148 public function test_find_all_with_conditions_as_string() 149 { 150 $author = Author::find('all',array('conditions' => 'author_id in(2,3)')); 151 $this->assert_equals(2,count($author)); 152 } 153 154 public function test_find_by_sql() 155 { 156 $author = Author::find_by_sql("SELECT * FROM authors WHERE author_id in(1,2)"); 157 $this->assert_equals(1,$author[0]->author_id); 158 $this->assert_equals(2,count($author)); 159 } 160 161 public function test_find_by_sqltakes_values_array() 162 { 163 $author = Author::find_by_sql("SELECT * FROM authors WHERE author_id=?",array(1)); 164 $this->assert_not_null($author); 165 } 166 167 public function test_find_with_conditions() 168 { 169 $author = Author::find(array('conditions' => array('author_id=? and name=?', 1, 'Tito'))); 170 $this->assert_equals(1,$author->author_id); 171 } 172 173 public function test_find_last() 174 { 175 $author = Author::last(); 176 $this->assert_equals(4, $author->author_id); 177 $this->assert_equals('Uncle Bob',$author->name); 178 } 179 180 public function test_find_last_using_string_condition() 181 { 182 $author = Author::find('last', array('conditions' => 'author_id IN(1,2,3,4)')); 183 $this->assert_equals(4, $author->author_id); 184 $this->assert_equals('Uncle Bob',$author->name); 185 } 186 187 public function test_limit_before_order() 188 { 189 $authors = Author::all(array('limit' => 2, 'order' => 'author_id desc', 'conditions' => 'author_id in(1,2)')); 190 $this->assert_equals(2,$authors[0]->author_id); 191 $this->assert_equals(1,$authors[1]->author_id); 192 } 193 194 public function test_for_each() 195 { 196 $i = 0; 197 $res = Author::all(); 198 199 foreach ($res as $author) 200 { 201 $this->assert_true($author instanceof ActiveRecord\Model); 202 $i++; 203 } 204 $this->assert_true($i > 0); 205 } 206 207 public function test_fetch_all() 208 { 209 $i = 0; 210 211 foreach (Author::all() as $author) 212 { 213 $this->assert_true($author instanceof ActiveRecord\Model); 214 $i++; 215 } 216 $this->assert_true($i > 0); 217 } 218 219 public function test_count() 220 { 221 $this->assert_equals(1,Author::count(1)); 222 $this->assert_equals(2,Author::count(array(1,2))); 223 $this->assert_true(Author::count() > 1); 224 $this->assert_equals(0,Author::count(array('conditions' => 'author_id=99999999999999'))); 225 $this->assert_equals(2,Author::count(array('conditions' => 'author_id=1 or author_id=2'))); 226 $this->assert_equals(1,Author::count(array('name' => 'Tito', 'author_id' => 1))); 227 } 228 229 public function test_exists() 230 { 231 $this->assert_true(Author::exists(1)); 232 $this->assert_true(Author::exists(array('conditions' => 'author_id=1'))); 233 $this->assert_true(Author::exists(array('conditions' => array('author_id=? and name=?', 1, 'Tito')))); 234 $this->assert_false(Author::exists(9999999)); 235 $this->assert_false(Author::exists(array('conditions' => 'author_id=999999'))); 236 } 237 238 public function test_find_by_call_static() 239 { 240 $this->assert_equals('Tito',Author::find_by_name('Tito')->name); 241 $this->assert_equals('Tito',Author::find_by_author_id_and_name(1,'Tito')->name); 242 $this->assert_equals('George W. Bush',Author::find_by_author_id_or_name(2,'Tito',array('order' => 'author_id desc'))->name); 243 $this->assert_equals('Tito',Author::find_by_name(array('Tito','George W. Bush'),array('order' => 'name desc'))->name); 244 } 245 246 public function test_find_by_call_static_no_results() 247 { 248 $this->assert_null(Author::find_by_name('SHARKS WIT LASERZ')); 249 $this->assert_null(Author::find_by_name_or_author_id()); 250 } 251 252 /** 253 * @expectedException ActiveRecord\DatabaseException 254 */ 255 public function test_find_by_call_static_invalid_column_name() 256 { 257 Author::find_by_sharks(); 258 } 259 260 public function test_find_all_by_call_static() 261 { 262 $x = Author::find_all_by_name('Tito'); 263 $this->assert_equals('Tito',$x[0]->name); 264 $this->assert_equals(1,count($x)); 265 266 $x = Author::find_all_by_author_id_or_name(2,'Tito',array('order' => 'name asc')); 267 $this->assert_equals(2,count($x)); 268 $this->assert_equals('George W. Bush',$x[0]->name); 269 } 270 271 public function test_find_all_by_call_static_no_results() 272 { 273 $x = Author::find_all_by_name('SHARKSSSSSSS'); 274 $this->assert_equals(0,count($x)); 275 } 276 277 public function test_find_all_by_call_static_with_array_values_and_options() 278 { 279 $author = Author::find_all_by_name(array('Tito','Bill Clinton'),array('order' => 'name desc')); 280 $this->assert_equals('Tito',$author[0]->name); 281 $this->assert_equals('Bill Clinton',$author[1]->name); 282 } 283 284 /** 285 * @expectedException ActiveRecord\ActiveRecordException 286 */ 287 public function test_find_all_by_call_static_undefined_method() 288 { 289 Author::find_sharks('Tito'); 290 } 291 292 public function test_find_all_takes_limit_options() 293 { 294 $authors = Author::all(array('limit' => 1, 'offset' => 2, 'order' => 'name desc')); 295 $this->assert_equals('George W. Bush',$authors[0]->name); 296 } 297 298 /** 299 * @expectedException ActiveRecord\ActiveRecordException 300 */ 301 public function test_find_by_call_static_with_invalid_field_name() 302 { 303 Author::find_by_some_invalid_field_name('Tito'); 304 } 305 306 public function test_find_with_select() 307 { 308 $author = Author::first(array('select' => 'name, 123 as bubba', 'order' => 'name desc')); 309 $this->assert_equals('Uncle Bob',$author->name); 310 $this->assert_equals(123,$author->bubba); 311 } 312 313 public function test_find_with_select_non_selected_fields_should_not_have_attributes() 314 { 315 $author = Author::first(array('select' => 'name, 123 as bubba')); 316 try { 317 $author->id; 318 $this->fail('expected ActiveRecord\UndefinedPropertyExecption'); 319 } catch (ActiveRecord\UndefinedPropertyException $e) { 320 ; 321 } 322 } 323 324 public function test_joins_on_model_with_association_and_explicit_joins() 325 { 326 JoinBook::$belongs_to = array(array('author')); 327 JoinBook::first(array('joins' => array('author','LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)'))); 328 $this->assert_sql_has('INNER JOIN authors ON(books.author_id = authors.author_id)',JoinBook::table()->last_sql); 329 $this->assert_sql_has('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)',JoinBook::table()->last_sql); 330 } 331 332 public function test_joins_on_model_with_explicit_joins() 333 { 334 JoinBook::first(array('joins' => array('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)'))); 335 $this->assert_sql_has('LEFT JOIN authors a ON(books.secondary_author_id=a.author_id)',JoinBook::table()->last_sql); 336 } 337 338 public function test_group() 339 { 340 $venues = Venue::all(array('select' => 'state', 'group' => 'state')); 341 $this->assert_true(count($venues) > 0); 342 $this->assert_sql_has('GROUP BY state',ActiveRecord\Table::load('Venue')->last_sql); 343 } 344 345 public function test_group_with_order_and_limit_and_having() 346 { 347 $venues = Venue::all(array('select' => 'state', 'group' => 'state', 'having' => 'length(state) = 2', 'order' => 'state', 'limit' => 2)); 348 $this->assert_true(count($venues) > 0); 349 $this->assert_sql_has($this->conn->limit('SELECT state FROM venues GROUP BY state HAVING length(state) = 2 ORDER BY state',null,2),Venue::table()->last_sql); 350 } 351 352 public function test_escape_quotes() 353 { 354 $author = Author::find_by_name("Tito's"); 355 $this->assert_not_equals("Tito's",Author::table()->last_sql); 356 } 357 358 public function test_from() 359 { 360 $author = Author::find('first', array('from' => 'books', 'order' => 'author_id asc')); 361 $this->assert_true($author instanceof Author); 362 $this->assert_not_null($author->book_id); 363 364 $author = Author::find('first', array('from' => 'authors', 'order' => 'author_id asc')); 365 $this->assert_true($author instanceof Author); 366 $this->assert_equals(1, $author->id); 367 } 368 369 public function test_having() 370 { 371 if ($this->conn instanceof ActiveRecord\OciAdapter) 372 { 373 $author = Author::first(array( 374 'select' => 'to_char(created_at,\'YYYY-MM-DD\') as created_at', 375 'group' => 'to_char(created_at,\'YYYY-MM-DD\')', 376 'having' => "to_char(created_at,'YYYY-MM-DD') > '2009-01-01'")); 377 $this->assert_sql_has("GROUP BY to_char(created_at,'YYYY-MM-DD') HAVING to_char(created_at,'YYYY-MM-DD') > '2009-01-01'",Author::table()->last_sql); 378 } 379 else 380 { 381 $author = Author::first(array( 382 'select' => 'date(created_at) as created_at', 383 'group' => 'date(created_at)', 384 'having' => "date(created_at) > '2009-01-01'")); 385 $this->assert_sql_has("GROUP BY date(created_at) HAVING date(created_at) > '2009-01-01'",Author::table()->last_sql); 386 } 387 } 388 389 /** 390 * @expectedException ActiveRecord\DatabaseException 391 */ 392 public function test_from_with_invalid_table() 393 { 394 $author = Author::find('first', array('from' => 'wrong_authors_table')); 395 } 396 397 public function test_find_with_hash() 398 { 399 $this->assert_not_null(Author::find(array('name' => 'Tito'))); 400 $this->assert_not_null(Author::find('first',array('name' => 'Tito'))); 401 $this->assert_equals(1,count(Author::find('all',array('name' => 'Tito')))); 402 $this->assert_equals(1,count(Author::all(array('name' => 'Tito')))); 403 } 404 405 public function test_find_or_create_by_on_existing_record() 406 { 407 $this->assert_not_null(Author::find_or_create_by_name('Tito')); 408 } 409 410 public function test_find_or_create_by_creates_new_record() 411 { 412 $author = Author::find_or_create_by_name_and_encrypted_password('New Guy','pencil'); 413 $this->assert_true($author->author_id > 0); 414 $this->assert_equals('pencil',$author->encrypted_password); 415 } 416 417 /** 418 * @expectedException ActiveRecord\ActiveRecordException 419 */ 420 public function test_find_or_create_by_throws_exception_when_using_or() 421 { 422 Author::find_or_create_by_name_or_encrypted_password('New Guy','pencil'); 423 } 424 425 /** 426 * @expectedException ActiveRecord\RecordNotFound 427 */ 428 public function test_find_by_zero() 429 { 430 Author::find(0); 431 } 432 433 public function test_count_by() 434 { 435 $this->assert_equals(2,Venue::count_by_state('VA')); 436 $this->assert_equals(3,Venue::count_by_state_or_name('VA','Warner Theatre')); 437 $this->assert_equals(0,Venue::count_by_state_and_name('VA','zzzzzzzzzzzzz')); 438 } 439 440 public function test_find_by_pk_should_not_use_limit() 441 { 442 Author::find(1); 443 $this->assert_sql_has('SELECT * FROM authors WHERE author_id=?',Author::table()->last_sql); 444 } 445 446 public function test_find_by_datetime() 447 { 448 $now = new DateTime(); 449 $arnow = new ActiveRecord\DateTime(); 450 $arnow->setTimestamp($now->getTimestamp()); 451 452 Author::find(1)->update_attribute('created_at',$now); 453 $this->assert_not_null(Author::find_by_created_at($now)); 454 $this->assert_not_null(Author::find_by_created_at($arnow)); 455 } 456}; 457?>