GettersTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 testMonthGetter()
  24. {
  25. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  26. $this->assertSame(5, $d->month);
  27. }
  28. public function testDayGetter()
  29. {
  30. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  31. $this->assertSame(6, $d->day);
  32. }
  33. public function testHourGetter()
  34. {
  35. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  36. $this->assertSame(7, $d->hour);
  37. }
  38. public function testMinuteGetter()
  39. {
  40. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  41. $this->assertSame(8, $d->minute);
  42. }
  43. public function testSecondGetter()
  44. {
  45. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  46. $this->assertSame(9, $d->second);
  47. }
  48. public function testDayOfWeeGetter()
  49. {
  50. $d = Carbon::create(2012, 5, 7, 7, 8, 9);
  51. $this->assertSame(Carbon::MONDAY, $d->dayOfWeek);
  52. }
  53. public function testDayOfYearGetter()
  54. {
  55. $d = Carbon::createFromDate(2012, 5, 7);
  56. $this->assertSame(127, $d->dayOfYear);
  57. }
  58. public function testDaysInMonthGetter()
  59. {
  60. $d = Carbon::createFromDate(2012, 5, 7);
  61. $this->assertSame(31, $d->daysInMonth);
  62. }
  63. public function testTimestampGetter()
  64. {
  65. $d = Carbon::create();
  66. $d->setTimezone('GMT');
  67. $this->assertSame(0, $d->setDateTime(1970, 1, 1, 0, 0, 0)->timestamp);
  68. }
  69. public function testGetAge()
  70. {
  71. $d = Carbon::now();
  72. $this->assertSame(0, $d->age);
  73. }
  74. public function testGetAgeWithRealAge()
  75. {
  76. $d = Carbon::createFromDate(1975, 5, 21);
  77. $age = intval(substr(date('Ymd') - date('Ymd', $d->timestamp), 0, -4));
  78. $this->assertSame($age, $d->age);
  79. }
  80. public function testGetQuarterFirst()
  81. {
  82. $d = Carbon::createFromDate(2012, 1, 1);
  83. $this->assertSame(1, $d->quarter);
  84. }
  85. public function testGetQuarterFirstEnd()
  86. {
  87. $d = Carbon::createFromDate(2012, 3, 31);
  88. $this->assertSame(1, $d->quarter);
  89. }
  90. public function testGetQuarterSecond()
  91. {
  92. $d = Carbon::createFromDate(2012, 4, 1);
  93. $this->assertSame(2, $d->quarter);
  94. }
  95. public function testGetQuarterThird()
  96. {
  97. $d = Carbon::createFromDate(2012, 7, 1);
  98. $this->assertSame(3, $d->quarter);
  99. }
  100. public function testGetQuarterFourth()
  101. {
  102. $d = Carbon::createFromDate(2012, 10, 1);
  103. $this->assertSame(4, $d->quarter);
  104. }
  105. public function testGetQuarterFirstLast()
  106. {
  107. $d = Carbon::createFromDate(2012, 12, 31);
  108. $this->assertSame(4, $d->quarter);
  109. }
  110. public function testGetDstFalse()
  111. {
  112. $this->assertFalse(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->dst);
  113. }
  114. public function testGetDstTrue()
  115. {
  116. $this->assertTrue(Carbon::createFromDate(2012, 7, 1, 'America/Toronto')->dst);
  117. }
  118. public function testOffsetForTorontoWithDST()
  119. {
  120. $this->assertSame(-18000, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offset);
  121. }
  122. public function testOffsetForTorontoNoDST()
  123. {
  124. $this->assertSame(-14400, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offset);
  125. }
  126. public function testOffsetForGMT()
  127. {
  128. $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offset);
  129. }
  130. public function testOffsetHoursForTorontoWithDST()
  131. {
  132. $this->assertSame(-5, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offsetHours);
  133. }
  134. public function testOffsetHoursForTorontoNoDST()
  135. {
  136. $this->assertSame(-4, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offsetHours);
  137. }
  138. public function testOffsetHoursForGMT()
  139. {
  140. $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offsetHours);
  141. }
  142. public function testIsLeapYearTrue()
  143. {
  144. $this->assertTrue(Carbon::createFromDate(2012, 1, 1)->isLeapYear());
  145. }
  146. public function testIsLeapYearFalse()
  147. {
  148. $this->assertFalse(Carbon::createFromDate(2011, 1, 1)->isLeapYear());
  149. }
  150. public function testWeekOfYearFirstWeek()
  151. {
  152. $this->assertSame(52, Carbon::createFromDate(2012, 1, 1)->weekOfYear);
  153. $this->assertSame(1, Carbon::createFromDate(2012, 1, 2)->weekOfYear);
  154. }
  155. public function testWeekOfYearLastWeek()
  156. {
  157. $this->assertSame(52, Carbon::createFromDate(2012, 12, 30)->weekOfYear);
  158. $this->assertSame(1, Carbon::createFromDate(2012, 12, 31)->weekOfYear);
  159. }
  160. public function testGetTimezone()
  161. {
  162. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  163. $this->assertSame('America/Toronto', $dt->timezone->getName());
  164. }
  165. public function testGetTz()
  166. {
  167. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  168. $this->assertSame('America/Toronto', $dt->tz->getName());
  169. }
  170. public function testGetTimezoneName()
  171. {
  172. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  173. $this->assertSame('America/Toronto', $dt->timezoneName);
  174. }
  175. public function testGetTzName()
  176. {
  177. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  178. $this->assertSame('America/Toronto', $dt->tzName);
  179. }
  180. public function testInvalidGetter()
  181. {
  182. $this->setExpectedException('InvalidArgumentException');
  183. $d = Carbon::now();
  184. $bb = $d->doesNotExit;
  185. }
  186. }