Няма описание

GettersTest.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /*
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Carbon\Carbon;
  11. class GettersTest extends TestFixture
  12. {
  13. public function testGettersThrowExceptionOnUnknownGetter()
  14. {
  15. $this->setExpectedException('InvalidArgumentException');
  16. Carbon::create(1234, 5, 6, 7, 8, 9)->sdfsdfss;
  17. }
  18. public function testYearGetter()
  19. {
  20. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  21. $this->assertSame(1234, $d->year);
  22. }
  23. public function testYearIsoGetter()
  24. {
  25. $d = Carbon::createFromDate(2012, 12, 31);
  26. $this->assertSame(2013, $d->yearIso);
  27. }
  28. public function testMonthGetter()
  29. {
  30. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  31. $this->assertSame(5, $d->month);
  32. }
  33. public function testDayGetter()
  34. {
  35. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  36. $this->assertSame(6, $d->day);
  37. }
  38. public function testHourGetter()
  39. {
  40. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  41. $this->assertSame(7, $d->hour);
  42. }
  43. public function testMinuteGetter()
  44. {
  45. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  46. $this->assertSame(8, $d->minute);
  47. }
  48. public function testSecondGetter()
  49. {
  50. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  51. $this->assertSame(9, $d->second);
  52. }
  53. public function testMicroGetter()
  54. {
  55. $micro = 345678;
  56. $d = Carbon::parse('2014-01-05 12:34:11.'.$micro);
  57. $this->assertSame($micro, $d->micro);
  58. }
  59. public function testMicroGetterWithDefaultNow()
  60. {
  61. $d = Carbon::now();
  62. $this->assertSame(0, $d->micro);
  63. }
  64. public function testDayOfWeeGetter()
  65. {
  66. $d = Carbon::create(2012, 5, 7, 7, 8, 9);
  67. $this->assertSame(Carbon::MONDAY, $d->dayOfWeek);
  68. }
  69. public function testDayOfYearGetter()
  70. {
  71. $d = Carbon::createFromDate(2012, 5, 7);
  72. $this->assertSame(127, $d->dayOfYear);
  73. }
  74. public function testDaysInMonthGetter()
  75. {
  76. $d = Carbon::createFromDate(2012, 5, 7);
  77. $this->assertSame(31, $d->daysInMonth);
  78. }
  79. public function testTimestampGetter()
  80. {
  81. $d = Carbon::create();
  82. $d->setTimezone('GMT');
  83. $this->assertSame(0, $d->setDateTime(1970, 1, 1, 0, 0, 0)->timestamp);
  84. }
  85. public function testGetAge()
  86. {
  87. $d = Carbon::now();
  88. $this->assertSame(0, $d->age);
  89. }
  90. public function testGetAgeWithRealAge()
  91. {
  92. $d = Carbon::createFromDate(1975, 5, 21);
  93. $age = intval(substr(date('Ymd') - date('Ymd', $d->timestamp), 0, -4));
  94. $this->assertSame($age, $d->age);
  95. }
  96. public function testGetQuarterFirst()
  97. {
  98. $d = Carbon::createFromDate(2012, 1, 1);
  99. $this->assertSame(1, $d->quarter);
  100. }
  101. public function testGetQuarterFirstEnd()
  102. {
  103. $d = Carbon::createFromDate(2012, 3, 31);
  104. $this->assertSame(1, $d->quarter);
  105. }
  106. public function testGetQuarterSecond()
  107. {
  108. $d = Carbon::createFromDate(2012, 4, 1);
  109. $this->assertSame(2, $d->quarter);
  110. }
  111. public function testGetQuarterThird()
  112. {
  113. $d = Carbon::createFromDate(2012, 7, 1);
  114. $this->assertSame(3, $d->quarter);
  115. }
  116. public function testGetQuarterFourth()
  117. {
  118. $d = Carbon::createFromDate(2012, 10, 1);
  119. $this->assertSame(4, $d->quarter);
  120. }
  121. public function testGetQuarterFirstLast()
  122. {
  123. $d = Carbon::createFromDate(2012, 12, 31);
  124. $this->assertSame(4, $d->quarter);
  125. }
  126. public function testGetLocalTrue()
  127. {
  128. // Default timezone has been set to America/Toronto in TestFixture.php
  129. // @see : http://en.wikipedia.org/wiki/List_of_UTC_time_offsets
  130. $this->assertTrue(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->local);
  131. $this->assertTrue(Carbon::createFromDate(2012, 1, 1, 'America/New_York')->local);
  132. }
  133. public function testGetLocalFalse()
  134. {
  135. $this->assertFalse(Carbon::createFromDate(2012, 7, 1, 'UTC')->local);
  136. $this->assertFalse(Carbon::createFromDate(2012, 7, 1, 'Europe/London')->local);
  137. }
  138. public function testGetUtcFalse()
  139. {
  140. $this->assertFalse(Carbon::createFromDate(2013, 1, 1, 'America/Toronto')->utc);
  141. $this->assertFalse(Carbon::createFromDate(2013, 1, 1, 'Europe/Paris')->utc);
  142. }
  143. public function testGetUtcTrue()
  144. {
  145. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Atlantic/Reykjavik')->utc);
  146. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/Lisbon')->utc);
  147. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Africa/Casablanca')->utc);
  148. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Africa/Dakar')->utc);
  149. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/Dublin')->utc);
  150. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/London')->utc);
  151. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'UTC')->utc);
  152. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'GMT')->utc);
  153. }
  154. public function testGetDstFalse()
  155. {
  156. $this->assertFalse(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->dst);
  157. }
  158. public function testGetDstTrue()
  159. {
  160. $this->assertTrue(Carbon::createFromDate(2012, 7, 1, 'America/Toronto')->dst);
  161. }
  162. public function testOffsetForTorontoWithDST()
  163. {
  164. $this->assertSame(-18000, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offset);
  165. }
  166. public function testOffsetForTorontoNoDST()
  167. {
  168. $this->assertSame(-14400, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offset);
  169. }
  170. public function testOffsetForGMT()
  171. {
  172. $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offset);
  173. }
  174. public function testOffsetHoursForTorontoWithDST()
  175. {
  176. $this->assertSame(-5, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offsetHours);
  177. }
  178. public function testOffsetHoursForTorontoNoDST()
  179. {
  180. $this->assertSame(-4, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offsetHours);
  181. }
  182. public function testOffsetHoursForGMT()
  183. {
  184. $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offsetHours);
  185. }
  186. public function testIsLeapYearTrue()
  187. {
  188. $this->assertTrue(Carbon::createFromDate(2012, 1, 1)->isLeapYear());
  189. }
  190. public function testIsLeapYearFalse()
  191. {
  192. $this->assertFalse(Carbon::createFromDate(2011, 1, 1)->isLeapYear());
  193. }
  194. public function testWeekOfMonth()
  195. {
  196. $this->assertSame(5, Carbon::createFromDate(2012, 9, 30)->weekOfMonth);
  197. $this->assertSame(4, Carbon::createFromDate(2012, 9, 28)->weekOfMonth);
  198. $this->assertSame(3, Carbon::createFromDate(2012, 9, 20)->weekOfMonth);
  199. $this->assertSame(2, Carbon::createFromDate(2012, 9, 8)->weekOfMonth);
  200. $this->assertSame(1, Carbon::createFromDate(2012, 9, 1)->weekOfMonth);
  201. }
  202. public function testWeekOfYearFirstWeek()
  203. {
  204. $this->assertSame(52, Carbon::createFromDate(2012, 1, 1)->weekOfYear);
  205. $this->assertSame(1, Carbon::createFromDate(2012, 1, 2)->weekOfYear);
  206. }
  207. public function testWeekOfYearLastWeek()
  208. {
  209. $this->assertSame(52, Carbon::createFromDate(2012, 12, 30)->weekOfYear);
  210. $this->assertSame(1, Carbon::createFromDate(2012, 12, 31)->weekOfYear);
  211. }
  212. public function testGetTimezone()
  213. {
  214. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  215. $this->assertSame('America/Toronto', $dt->timezone->getName());
  216. }
  217. public function testGetTz()
  218. {
  219. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  220. $this->assertSame('America/Toronto', $dt->tz->getName());
  221. }
  222. public function testGetTimezoneName()
  223. {
  224. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  225. $this->assertSame('America/Toronto', $dt->timezoneName);
  226. }
  227. public function testGetTzName()
  228. {
  229. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  230. $this->assertSame('America/Toronto', $dt->tzName);
  231. }
  232. public function testInvalidGetter()
  233. {
  234. $this->setExpectedException('InvalidArgumentException');
  235. $d = Carbon::now();
  236. $bb = $d->doesNotExit;
  237. }
  238. }