a tiny mvc framework for php using php-activerecord
at v1 123 lines 2.9 kB view raw
1<?php 2include 'helpers/config.php'; 3use ActiveRecord\DateTime as DateTime; 4 5class 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 13 public function tear_down() 14 { 15 DateTime::$DEFAULT_FORMAT = $this->original_format; 16 } 17 18 private function assert_dirtifies($method /*, method params, ...*/) 19 { 20 $model = new Author(); 21 $datetime = new DateTime(); 22 $datetime->attribute_of($model,'some_date'); 23 24 $args = func_get_args(); 25 array_shift($args); 26 27 call_user_func_array(array($datetime,$method),$args); 28 $this->assert_has_keys('some_date', $model->dirty_attributes()); 29 } 30 31 public function test_should_flag_the_attribute_dirty() 32 { 33 $this->assert_dirtifies('setDate',2001,1,1); 34 $this->assert_dirtifies('setISODate',2001,1); 35 $this->assert_dirtifies('setTime',1,1); 36 $this->assert_dirtifies('setTimestamp',1); 37 } 38 39 public function test_set_iso_date() 40 { 41 $a = new \DateTime(); 42 $a->setISODate(2001,1); 43 44 $b = new DateTime(); 45 $b->setISODate(2001,1); 46 47 $this->assert_datetime_equals($a,$b); 48 } 49 50 public function test_set_time() 51 { 52 $a = new \DateTime(); 53 $a->setTime(1,1); 54 55 $b = new DateTime(); 56 $b->setTime(1,1); 57 58 $this->assert_datetime_equals($a,$b); 59 } 60 61 public function test_get_format_with_friendly() 62 { 63 $this->assert_equals('Y-m-d H:i:s', DateTime::get_format('db')); 64 } 65 66 public function test_get_format_with_format() 67 { 68 $this->assert_equals('Y-m-d', DateTime::get_format('Y-m-d')); 69 } 70 71 public function test_get_format_with_null() 72 { 73 $this->assert_equals(\DateTime::RFC2822, DateTime::get_format()); 74 } 75 76 public function test_format() 77 { 78 $this->assert_true(is_string($this->date->format())); 79 $this->assert_true(is_string($this->date->format('Y-m-d'))); 80 } 81 82 public function test_format_by_friendly_name() 83 { 84 $d = date(DateTime::get_format('db')); 85 $this->assert_equals($d, $this->date->format('db')); 86 } 87 88 public function test_format_by_custom_format() 89 { 90 $format = 'Y/m/d'; 91 $this->assert_equals(date($format), $this->date->format($format)); 92 } 93 94 public function test_format_uses_default() 95 { 96 $d = date(DateTime::$FORMATS[DateTime::$DEFAULT_FORMAT]); 97 $this->assert_equals($d, $this->date->format()); 98 } 99 100 public function test_all_formats() 101 { 102 foreach (DateTime::$FORMATS as $name => $format) 103 $this->assert_equals(date($format), $this->date->format($name)); 104 } 105 106 public function test_change_default_format_to_format_string() 107 { 108 DateTime::$DEFAULT_FORMAT = 'H:i:s'; 109 $this->assert_equals(date(DateTime::$DEFAULT_FORMAT), $this->date->format()); 110 } 111 112 public function test_change_default_format_to_friently() 113 { 114 DateTime::$DEFAULT_FORMAT = 'short'; 115 $this->assert_equals(date(DateTime::$FORMATS['short']), $this->date->format()); 116 } 117 118 public function test_to_string() 119 { 120 $this->assert_equals(date(DateTime::get_format()), "" . $this->date); 121 } 122} 123?>