PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/test/akelos/active_record/cases/type_casting.php

https://github.com/bermi/akelos
PHP | 92 lines | 69 code | 18 blank | 5 comment | 0 complexity | 461b1d6287058125860d56aaef464943 MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__).'/../config.php');
  3. class TypeCasting_TestCase extends ActiveRecordUnitTest
  4. {
  5. public function test_start() {
  6. $this->installAndIncludeModels(array('Post', 'User', 'Tagging', 'Tag'));
  7. }
  8. // Ticket #21
  9. public function _test_should_store_zero_strings_as_intergers() {
  10. $Tag = new Tag(array('name'=>'Ticket #21'));
  11. $this->assertTrue($Tag->save());
  12. $this->assertEqual($Tag->get('score'), 100);
  13. $Tag->setAttributes(array('score' => '0'));
  14. $this->assertTrue($Tag->save());
  15. $Tag = $Tag->find($Tag->id);
  16. $this->assertIdentical($Tag->get('score'), 0);
  17. }
  18. // Ticket #36
  19. public function test_should_update_dates_correctly() {
  20. $params = array(
  21. //'title' => 'Hello',
  22. //'body' => 'Hello world!',
  23. 'posted_on(1i)' => '2005',
  24. 'posted_on(2i)' => '6',
  25. 'posted_on(3i)' => '16');
  26. $Post = new Post();
  27. $Post->setAttributes($params);
  28. $this->assertTrue($Post->save());
  29. $Post->reload();
  30. $this->assertEqual($Post->get('posted_on'), '2005-06-16');
  31. }
  32. // Ticket #76
  33. public function test_should_update_datetime_correctly() {
  34. $params = array(
  35. 'title' => 'Expiring salutation',
  36. 'body' => 'Expiring Hello world!',
  37. 'expires_at(1i)' => '2007',
  38. 'expires_at(2i)' => '10',
  39. 'expires_at(3i)' => '15',
  40. 'expires_at(4i)' => '17',
  41. 'expires_at(5i)' => '30'
  42. );
  43. $Post = new Post();
  44. $Post->setAttributes($params);
  45. $this->assertTrue($Post->save());
  46. $Post->reload();
  47. $this->assertEqual($Post->get('expires_at'), '2007-10-15 17:30:00');
  48. }
  49. public function test_should_handle_empty_date_as_null() {
  50. $this->installAndIncludeModels(array('Post'));
  51. $params = array('title'=>'An empty date is a null date','posted_on(1i)'=>'','posted_on(2i)'=>'','posted_on(3i)'=>'');
  52. $MyPost = $this->Post->create($params);
  53. $MyPost->reload();
  54. $this->assertNull($MyPost->posted_on);
  55. }
  56. public function test_cast_date_parameters() {
  57. $params = array('posted_on(1i)'=>'','posted_on(2i)'=>'','posted_on(3i)'=>'');
  58. $this->Post->setAttributes($params);
  59. $this->assertEqual('', $this->Post->get('posted_on'));
  60. $params = array('posted_on(1i)'=>'2008','posted_on(2i)'=>'10','posted_on(3i)'=>'');
  61. $this->Post->setAttributes($params);
  62. $this->assertEqual('2008-10', $this->Post->get('posted_on'));
  63. $this->assertEqual('2008',$this->Post->{"posted_on(1i)"});
  64. $this->assertEqual('10',$this->Post->{"posted_on(2i)"});
  65. $this->assertEqual('',$this->Post->{"posted_on(3i)"});
  66. }
  67. public function test_should_serialize_attributes() {
  68. $User = new User(array('preferences'=>array("background" => "black", "display" => 'large')));
  69. $User->save();
  70. $User = $User->find($User->getId());
  71. $this->assertEqual($User->get('preferences'), array("background" => "black", "display" => 'large'));
  72. }
  73. }
  74. ak_test_case('TypeCasting_TestCase');