DateTimeTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. include 'helpers/config.php';
  3. use ActiveRecord\DateTime as DateTime;
  4. use ActiveRecord\DatabaseException;
  5. class DateTimeTest extends SnakeCase_PHPUnit_Framework_TestCase
  6. {
  7. public function set_up()
  8. {
  9. $this->date = new DateTime();
  10. $this->original_format = DateTime::$DEFAULT_FORMAT;
  11. }
  12. public function tear_down()
  13. {
  14. DateTime::$DEFAULT_FORMAT = $this->original_format;
  15. }
  16. private function assert_dirtifies($method /*, method params, ...*/)
  17. {
  18. try {
  19. $model = new Author();
  20. } catch (DatabaseException $e) {
  21. $this->mark_test_skipped('failed to connect. '.$e->getMessage());
  22. }
  23. $datetime = new DateTime();
  24. $datetime->attribute_of($model,'some_date');
  25. $args = func_get_args();
  26. array_shift($args);
  27. call_user_func_array(array($datetime,$method),$args);
  28. $this->assert_has_keys('some_date', $model->dirty_attributes());
  29. }
  30. public function test_should_flag_the_attribute_dirty()
  31. {
  32. $this->assert_dirtifies('setDate',2001,1,1);
  33. $this->assert_dirtifies('setISODate',2001,1);
  34. $this->assert_dirtifies('setTime',1,1);
  35. $this->assert_dirtifies('setTimestamp',1);
  36. }
  37. public function test_set_iso_date()
  38. {
  39. $a = new \DateTime();
  40. $a->setISODate(2001,1);
  41. $b = new DateTime();
  42. $b->setISODate(2001,1);
  43. $this->assert_datetime_equals($a,$b);
  44. }
  45. public function test_set_time()
  46. {
  47. $a = new \DateTime();
  48. $a->setTime(1,1);
  49. $b = new DateTime();
  50. $b->setTime(1,1);
  51. $this->assert_datetime_equals($a,$b);
  52. }
  53. public function test_get_format_with_friendly()
  54. {
  55. $this->assert_equals('Y-m-d H:i:s', DateTime::get_format('db'));
  56. }
  57. public function test_get_format_with_format()
  58. {
  59. $this->assert_equals('Y-m-d', DateTime::get_format('Y-m-d'));
  60. }
  61. public function test_get_format_with_null()
  62. {
  63. $this->assert_equals(\DateTime::RFC2822, DateTime::get_format());
  64. }
  65. public function test_format()
  66. {
  67. $this->assert_true(is_string($this->date->format()));
  68. $this->assert_true(is_string($this->date->format('Y-m-d')));
  69. }
  70. public function test_format_by_friendly_name()
  71. {
  72. $d = date(DateTime::get_format('db'));
  73. $this->assert_equals($d, $this->date->format('db'));
  74. }
  75. public function test_format_by_custom_format()
  76. {
  77. $format = 'Y/m/d';
  78. $this->assert_equals(date($format), $this->date->format($format));
  79. }
  80. public function test_format_uses_default()
  81. {
  82. $d = date(DateTime::$FORMATS[DateTime::$DEFAULT_FORMAT]);
  83. $this->assert_equals($d, $this->date->format());
  84. }
  85. public function test_all_formats()
  86. {
  87. foreach (DateTime::$FORMATS as $name => $format)
  88. $this->assert_equals(date($format), $this->date->format($name));
  89. }
  90. public function test_change_default_format_to_format_string()
  91. {
  92. DateTime::$DEFAULT_FORMAT = 'H:i:s';
  93. $this->assert_equals(date(DateTime::$DEFAULT_FORMAT), $this->date->format());
  94. }
  95. public function test_change_default_format_to_friently()
  96. {
  97. DateTime::$DEFAULT_FORMAT = 'short';
  98. $this->assert_equals(date(DateTime::$FORMATS['short']), $this->date->format());
  99. }
  100. public function test_to_string()
  101. {
  102. $this->assert_equals(date(DateTime::get_format()), "" . $this->date);
  103. }
  104. }
  105. ?>