ActiveRecordFindTest.php 14 KB

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