PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/php-activerecord/php-activerecord/test/ActiveRecordFindTest.php

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