ActiveRecordTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. include 'helpers/config.php';
  3. class ActiveRecordTest extends DatabaseTest
  4. {
  5. public function set_up($connection_name=null)
  6. {
  7. parent::set_up($connection_name);
  8. $this->options = array('conditions' => 'blah', 'order' => 'blah');
  9. }
  10. public function test_options_is_not()
  11. {
  12. $this->assert_false(Author::is_options_hash(null));
  13. $this->assert_false(Author::is_options_hash(''));
  14. $this->assert_false(Author::is_options_hash('tito'));
  15. $this->assert_false(Author::is_options_hash(array()));
  16. $this->assert_false(Author::is_options_hash(array(1,2,3)));
  17. }
  18. /**
  19. * @expectedException ActiveRecord\ActiveRecordException
  20. */
  21. public function test_options_hash_with_unknown_keys() {
  22. $this->assert_false(Author::is_options_hash(array('conditions' => 'blah', 'sharks' => 'laserz', 'dubya' => 'bush')));
  23. }
  24. public function test_options_is_hash()
  25. {
  26. $this->assert_true(Author::is_options_hash($this->options));
  27. }
  28. public function test_extract_and_validate_options() {
  29. $args = array('first',$this->options);
  30. $this->assert_equals($this->options,Author::extract_and_validate_options($args));
  31. $this->assert_equals(array('first'),$args);
  32. }
  33. public function test_extract_and_validate_options_with_array_in_args() {
  34. $args = array('first',array(1,2),$this->options);
  35. $this->assert_equals($this->options,Author::extract_and_validate_options($args));
  36. }
  37. public function test_extract_and_validate_options_removes_options_hash() {
  38. $args = array('first',$this->options);
  39. Author::extract_and_validate_options($args);
  40. $this->assert_equals(array('first'),$args);
  41. }
  42. public function test_extract_and_validate_options_nope() {
  43. $args = array('first');
  44. $this->assert_equals(array(),Author::extract_and_validate_options($args));
  45. $this->assert_equals(array('first'),$args);
  46. }
  47. public function test_extract_and_validate_options_nope_because_wasnt_at_end() {
  48. $args = array('first',$this->options,array(1,2));
  49. $this->assert_equals(array(),Author::extract_and_validate_options($args));
  50. }
  51. /**
  52. * @expectedException ActiveRecord\UndefinedPropertyException
  53. */
  54. public function test_invalid_attribute()
  55. {
  56. $author = Author::find('first',array('conditions' => 'author_id=1'));
  57. $author->some_invalid_field_name;
  58. }
  59. public function test_invalid_attributes()
  60. {
  61. $book = Book::find(1);
  62. try {
  63. $book->update_attributes(array('name' => 'new name', 'invalid_attribute' => true , 'another_invalid_attribute' => 'something'));
  64. } catch (ActiveRecord\UndefinedPropertyException $e) {
  65. $exceptions = explode("\r\n", $e->getMessage());
  66. }
  67. $this->assert_equals(1, substr_count($exceptions[0], 'invalid_attribute'));
  68. $this->assert_equals(1, substr_count($exceptions[1], 'another_invalid_attribute'));
  69. }
  70. public function test_getter_undefined_property_exception_includes_model_name()
  71. {
  72. $this->assert_exception_message_contains("Author->this_better_not_exist",function()
  73. {
  74. $author = new Author();
  75. $author->this_better_not_exist;
  76. });
  77. }
  78. public function test_mass_assignment_undefined_property_exception_includes_model_name()
  79. {
  80. $this->assert_exception_message_contains("Author->this_better_not_exist",function()
  81. {
  82. new Author(array("this_better_not_exist" => "hi"));
  83. });
  84. }
  85. public function test_setter_undefined_property_exception_includes_model_name()
  86. {
  87. $this->assert_exception_message_contains("Author->this_better_not_exist",function()
  88. {
  89. $author = new Author();
  90. $author->this_better_not_exist = "hi";
  91. });
  92. }
  93. public function test_get_values_for()
  94. {
  95. $book = Book::find_by_name('Ancient Art of Main Tanking');
  96. $ret = $book->get_values_for(array('book_id','author_id'));
  97. $this->assert_equals(array('book_id','author_id'),array_keys($ret));
  98. $this->assert_equals(array(1,1),array_values($ret));
  99. }
  100. public function test_hyphenated_column_names_to_underscore()
  101. {
  102. if ($this->conn instanceof ActiveRecord\OciAdapter)
  103. return;
  104. $keys = array_keys(RmBldg::first()->attributes());
  105. $this->assert_true(in_array('rm_name',$keys));
  106. }
  107. public function test_column_names_with_spaces()
  108. {
  109. if ($this->conn instanceof ActiveRecord\OciAdapter)
  110. return;
  111. $keys = array_keys(RmBldg::first()->attributes());
  112. $this->assert_true(in_array('space_out',$keys));
  113. }
  114. public function test_mixed_case_column_name()
  115. {
  116. $keys = array_keys(Author::first()->attributes());
  117. $this->assert_true(in_array('mixedcasefield',$keys));
  118. }
  119. public function test_mixed_case_primary_key_save()
  120. {
  121. $venue = Venue::find(1);
  122. $venue->name = 'should not throw exception';
  123. $venue->save();
  124. $this->assert_equals($venue->name,Venue::find(1)->name);
  125. }
  126. public function test_reload()
  127. {
  128. $venue = Venue::find(1);
  129. $this->assert_equals('NY', $venue->state);
  130. $venue->state = 'VA';
  131. $this->assert_equals('VA', $venue->state);
  132. $venue->reload();
  133. $this->assert_equals('NY', $venue->state);
  134. }
  135. public function test_reload_protected_attribute()
  136. {
  137. $book = BookAttrAccessible::find(1);
  138. $book->name = "Should not stay";
  139. $book->reload();
  140. $this->assert_not_equals("Should not stay", $book->name);
  141. }
  142. public function test_active_record_model_home_not_set()
  143. {
  144. $home = ActiveRecord\Config::instance()->get_model_directory();
  145. ActiveRecord\Config::instance()->set_model_directory(__FILE__);
  146. $this->assert_equals(false,class_exists('TestAutoload'));
  147. ActiveRecord\Config::instance()->set_model_directory($home);
  148. }
  149. public function test_auto_load_with_namespaced_model()
  150. {
  151. $this->assert_true(class_exists('NamespaceTest\Book'));
  152. }
  153. public function test_namespace_gets_stripped_from_table_name()
  154. {
  155. $model = new NamespaceTest\Book();
  156. $this->assert_equals('books',$model->table()->table);
  157. }
  158. public function test_namespace_gets_stripped_from_inferred_foreign_key()
  159. {
  160. $model = new NamespaceTest\Book();
  161. $table = ActiveRecord\Table::load(get_class($model));
  162. $this->assert_equals($table->get_relationship('parent_book')->foreign_key[0], 'book_id');
  163. }
  164. public function test_should_have_all_column_attributes_when_initializing_with_array()
  165. {
  166. $author = new Author(array('name' => 'Tito'));
  167. $this->assert_true(count(array_keys($author->attributes())) >= 9);
  168. }
  169. public function test_defaults()
  170. {
  171. $author = new Author();
  172. $this->assert_equals('default_name',$author->name);
  173. }
  174. public function test_alias_attribute_getter()
  175. {
  176. $venue = Venue::find(1);
  177. $this->assert_equals($venue->marquee, $venue->name);
  178. $this->assert_equals($venue->mycity, $venue->city);
  179. }
  180. public function test_alias_attribute_setter()
  181. {
  182. $venue = Venue::find(1);
  183. $venue->marquee = 'new name';
  184. $this->assert_equals($venue->marquee, 'new name');
  185. $this->assert_equals($venue->marquee, $venue->name);
  186. $venue->name = 'another name';
  187. $this->assert_equals($venue->name, 'another name');
  188. $this->assert_equals($venue->marquee, $venue->name);
  189. }
  190. public function test_alias_from_mass_attributes()
  191. {
  192. $venue = new Venue(array('marquee' => 'meme', 'id' => 123));
  193. $this->assert_equals('meme',$venue->name);
  194. $this->assert_equals($venue->marquee,$venue->name);
  195. }
  196. public function test_gh18_isset_on_aliased_attribute()
  197. {
  198. $this->assert_true(isset(Venue::first()->marquee));
  199. }
  200. public function test_attr_accessible()
  201. {
  202. $book = new BookAttrAccessible(array('name' => 'should not be set', 'author_id' => 1));
  203. $this->assert_null($book->name);
  204. $this->assert_equals(1,$book->author_id);
  205. $book->name = 'test';
  206. $this->assert_equals('test', $book->name);
  207. }
  208. public function test_attr_protected()
  209. {
  210. $book = new BookAttrAccessible(array('book_id' => 999));
  211. $this->assert_null($book->book_id);
  212. $book->book_id = 999;
  213. $this->assert_equals(999, $book->book_id);
  214. }
  215. public function test_isset()
  216. {
  217. $book = new Book();
  218. $this->assert_true(isset($book->name));
  219. $this->assert_false(isset($book->sharks));
  220. }
  221. public function test_readonly_only_halt_on_write_method()
  222. {
  223. $book = Book::first(array('readonly' => true));
  224. $this->assert_true($book->is_readonly());
  225. try {
  226. $book->save();
  227. $this-fail('expected exception ActiveRecord\ReadonlyException');
  228. } catch (ActiveRecord\ReadonlyException $e) {
  229. }
  230. $book->name = 'some new name';
  231. $this->assert_equals($book->name, 'some new name');
  232. }
  233. public function test_cast_when_using_setter()
  234. {
  235. $book = new Book();
  236. $book->book_id = '1';
  237. $this->assert_same(1,$book->book_id);
  238. }
  239. public function test_cast_when_loading()
  240. {
  241. $book = Book::find(1);
  242. $this->assert_same(1,$book->book_id);
  243. $this->assert_same('Ancient Art of Main Tanking',$book->name);
  244. }
  245. public function test_cast_defaults()
  246. {
  247. $book = new Book();
  248. $this->assert_same(0.0,$book->special);
  249. }
  250. public function test_transaction_committed()
  251. {
  252. $original = Author::count();
  253. $ret = Author::transaction(function() { Author::create(array("name" => "blah")); });
  254. $this->assert_equals($original+1,Author::count());
  255. $this->assert_true($ret);
  256. }
  257. public function test_transaction_committed_when_returning_true()
  258. {
  259. $original = Author::count();
  260. $ret = Author::transaction(function() { Author::create(array("name" => "blah")); return true; });
  261. $this->assert_equals($original+1,Author::count());
  262. $this->assert_true($ret);
  263. }
  264. public function test_transaction_rolledback_by_returning_false()
  265. {
  266. $original = Author::count();
  267. $ret = Author::transaction(function()
  268. {
  269. Author::create(array("name" => "blah"));
  270. return false;
  271. });
  272. $this->assert_equals($original,Author::count());
  273. $this->assert_false($ret);
  274. }
  275. public function test_transaction_rolledback_by_throwing_exception()
  276. {
  277. $original = Author::count();
  278. $exception = null;
  279. try
  280. {
  281. Author::transaction(function()
  282. {
  283. Author::create(array("name" => "blah"));
  284. throw new Exception("blah");
  285. });
  286. }
  287. catch (Exception $e)
  288. {
  289. $exception = $e;
  290. }
  291. $this->assert_not_null($exception);
  292. $this->assert_equals($original,Author::count());
  293. }
  294. public function test_delegate()
  295. {
  296. $event = Event::first();
  297. $this->assert_equals($event->venue->state,$event->state);
  298. $this->assert_equals($event->venue->address,$event->address);
  299. }
  300. public function test_delegate_prefix()
  301. {
  302. $event = Event::first();
  303. $this->assert_equals($event->host->name,$event->woot_name);
  304. }
  305. public function test_delegate_returns_null_if_relationship_does_not_exist()
  306. {
  307. $event = new Event();
  308. $this->assert_null($event->state);
  309. }
  310. public function test_delegate_set_attribute()
  311. {
  312. $event = Event::first();
  313. $event->state = 'MEXICO';
  314. $this->assert_equals('MEXICO',$event->venue->state);
  315. }
  316. public function test_delegate_getter_gh_98()
  317. {
  318. Venue::$use_custom_get_state_getter = true;
  319. $event = Event::first();
  320. $this->assert_equals('ny', $event->venue->state);
  321. $this->assert_equals('ny', $event->state);
  322. Venue::$use_custom_get_state_getter = false;
  323. }
  324. public function test_delegate_setter_gh_98()
  325. {
  326. Venue::$use_custom_set_state_setter = true;
  327. $event = Event::first();
  328. $event->state = 'MEXICO';
  329. $this->assert_equals('MEXICO#',$event->venue->state);
  330. Venue::$use_custom_set_state_setter = false;
  331. }
  332. public function test_table_name_with_underscores()
  333. {
  334. $this->assert_not_null(AwesomePerson::first());
  335. }
  336. public function test_model_should_default_as_new_record()
  337. {
  338. $author = new Author();
  339. $this->assert_true($author->is_new_record());
  340. }
  341. public function test_setter()
  342. {
  343. $author = new Author();
  344. $author->password = 'plaintext';
  345. $this->assert_equals(md5('plaintext'),$author->encrypted_password);
  346. }
  347. public function test_setter_with_same_name_as_an_attribute()
  348. {
  349. $author = new Author();
  350. $author->name = 'bob';
  351. $this->assert_equals('BOB',$author->name);
  352. }
  353. public function test_getter()
  354. {
  355. $book = Book::first();
  356. $this->assert_equals(strtoupper($book->name), $book->upper_name);
  357. }
  358. public function test_getter_with_same_name_as_an_attribute()
  359. {
  360. Book::$use_custom_get_name_getter = true;
  361. $book = new Book;
  362. $book->name = 'bob';
  363. $this->assert_equals('BOB', $book->name);
  364. Book::$use_custom_get_name_getter = false;
  365. }
  366. public function test_setting_invalid_date_should_set_date_to_null()
  367. {
  368. $author = new Author();
  369. $author->created_at = 'CURRENT_TIMESTAMP';
  370. $this->assertNull($author->created_at);
  371. }
  372. public function test_table_name()
  373. {
  374. $this->assert_equals('authors',Author::table_name());
  375. }
  376. /**
  377. * @expectedException ActiveRecord\ActiveRecordException
  378. */
  379. public function test_undefined_instance_method()
  380. {
  381. Author::first()->find_by_name('sdf');
  382. }
  383. public function test_clear_cache_for_specific_class()
  384. {
  385. $book_table1 = ActiveRecord\Table::load('Book');
  386. $book_table2 = ActiveRecord\Table::load('Book');
  387. ActiveRecord\Table::clear_cache('Book');
  388. $book_table3 = ActiveRecord\Table::load('Book');
  389. $this->assert_true($book_table1 === $book_table2);
  390. $this->assert_true($book_table1 !== $book_table3);
  391. }
  392. public function test_flag_dirty()
  393. {
  394. $author = new Author();
  395. $author->flag_dirty('some_date');
  396. $this->assert_has_keys('some_date', $author->dirty_attributes());
  397. $this->assert_true($author->attribute_is_dirty('some_date'));
  398. $author->save();
  399. $this->assert_false($author->attribute_is_dirty('some_date'));
  400. }
  401. public function test_flag_dirty_attribute()
  402. {
  403. $author = new Author();
  404. $author->flag_dirty('some_inexistant_property');
  405. $this->assert_null($author->dirty_attributes());
  406. $this->assert_false($author->attribute_is_dirty('some_inexistant_property'));
  407. }
  408. public function test_assigning_php_datetime_gets_converted_to_ar_datetime()
  409. {
  410. $author = new Author();
  411. $author->created_at = $now = new \DateTime();
  412. $this->assert_is_a("ActiveRecord\\DateTime",$author->created_at);
  413. $this->assert_datetime_equals($now,$author->created_at);
  414. }
  415. public function test_assigning_from_mass_assignment_php_datetime_gets_converted_to_ar_datetime()
  416. {
  417. $author = new Author(array('created_at' => new \DateTime()));
  418. $this->assert_is_a("ActiveRecord\\DateTime",$author->created_at);
  419. }
  420. public function test_get_real_attribute_name()
  421. {
  422. $venue = new Venue();
  423. $this->assert_equals('name', $venue->get_real_attribute_name('name'));
  424. $this->assert_equals('name', $venue->get_real_attribute_name('marquee'));
  425. $this->assert_equals(null, $venue->get_real_attribute_name('invalid_field'));
  426. }
  427. public function test_id_setter_works_with_table_without_pk_named_attribute()
  428. {
  429. $author = new Author(array('id' => 123));
  430. $this->assert_equals(123,$author->author_id);
  431. }
  432. public function test_query()
  433. {
  434. $row = Author::query('SELECT COUNT(*) AS n FROM authors',null)->fetch();
  435. $this->assert_true($row['n'] > 1);
  436. $row = Author::query('SELECT COUNT(*) AS n FROM authors WHERE name=?',array('Tito'))->fetch();
  437. $this->assert_equals(array('n' => 1), $row);
  438. }
  439. };
  440. ?>