SettersTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 SettersTest extends TestFixture
  12. {
  13. public function testYearSetter()
  14. {
  15. $d = Carbon::now();
  16. $d->year = 1995;
  17. $this->assertSame(1995, $d->year);
  18. }
  19. public function testMonthSetter()
  20. {
  21. $d = Carbon::now();
  22. $d->month = 3;
  23. $this->assertSame(3, $d->month);
  24. }
  25. public function testMonthSetterWithWrap()
  26. {
  27. $d = Carbon::now();
  28. $d->month = 13;
  29. $this->assertSame(1, $d->month);
  30. }
  31. public function testDaySetter()
  32. {
  33. $d = Carbon::now();
  34. $d->day = 2;
  35. $this->assertSame(2, $d->day);
  36. }
  37. public function testDaySetterWithWrap()
  38. {
  39. $d = Carbon::createFromDate(2012, 8, 5);
  40. $d->day = 32;
  41. $this->assertSame(1, $d->day);
  42. }
  43. public function testHourSetter()
  44. {
  45. $d = Carbon::now();
  46. $d->hour = 2;
  47. $this->assertSame(2, $d->hour);
  48. }
  49. public function testHourSetterWithWrap()
  50. {
  51. $d = Carbon::now();
  52. $d->hour = 25;
  53. $this->assertSame(1, $d->hour);
  54. }
  55. public function testMinuteSetter()
  56. {
  57. $d = Carbon::now();
  58. $d->minute = 2;
  59. $this->assertSame(2, $d->minute);
  60. }
  61. public function testMinuteSetterWithWrap()
  62. {
  63. $d = Carbon::now();
  64. $d->minute = 65;
  65. $this->assertSame(5, $d->minute);
  66. }
  67. public function testSecondSetter()
  68. {
  69. $d = Carbon::now();
  70. $d->second = 2;
  71. $this->assertSame(2, $d->second);
  72. }
  73. public function testSecondSetterWithWrap()
  74. {
  75. $d = Carbon::now();
  76. $d->second = 65;
  77. $this->assertSame(5, $d->second);
  78. }
  79. public function testTimestampSetter()
  80. {
  81. $d = Carbon::now();
  82. $d->timestamp = 10;
  83. $this->assertSame(10, $d->timestamp);
  84. $d->setTimestamp(11);
  85. $this->assertSame(11, $d->timestamp);
  86. }
  87. public function testSetTimezoneWithInvalidTimezone()
  88. {
  89. $this->setExpectedException('InvalidArgumentException');
  90. $d = Carbon::now();
  91. $d->setTimezone('sdf');
  92. }
  93. public function testTimezoneWithInvalidTimezone()
  94. {
  95. $d = Carbon::now();
  96. try {
  97. $d->timezone = 'sdf';
  98. $this->fail('InvalidArgumentException was not been raised.');
  99. } catch (InvalidArgumentException $expected) {}
  100. try {
  101. $d->timezone('sdf');
  102. $this->fail('InvalidArgumentException was not been raised.');
  103. } catch (InvalidArgumentException $expected) {}
  104. }
  105. public function testTzWithInvalidTimezone()
  106. {
  107. $d = Carbon::now();
  108. try {
  109. $d->tz = 'sdf';
  110. $this->fail('InvalidArgumentException was not been raised.');
  111. } catch (InvalidArgumentException $expected) {}
  112. try {
  113. $d->tz('sdf');
  114. $this->fail('InvalidArgumentException was not been raised.');
  115. } catch (InvalidArgumentException $expected) {}
  116. }
  117. public function testSetTimezoneUsingString()
  118. {
  119. $d = Carbon::now();
  120. $d->setTimezone('America/Toronto');
  121. $this->assertSame('America/Toronto', $d->tzName);
  122. }
  123. public function testTimezoneUsingString()
  124. {
  125. $d = Carbon::now();
  126. $d->timezone = 'America/Toronto';
  127. $this->assertSame('America/Toronto', $d->tzName);
  128. $d->timezone('America/Vancouver');
  129. $this->assertSame('America/Vancouver', $d->tzName);
  130. }
  131. public function testTzUsingString()
  132. {
  133. $d = Carbon::now();
  134. $d->tz = 'America/Toronto';
  135. $this->assertSame('America/Toronto', $d->tzName);
  136. $d->tz('America/Vancouver');
  137. $this->assertSame('America/Vancouver', $d->tzName);
  138. }
  139. public function testSetTimezoneUsingDateTimeZone()
  140. {
  141. $d = Carbon::now();
  142. $d->setTimezone(new \DateTimeZone('America/Toronto'));
  143. $this->assertSame('America/Toronto', $d->tzName);
  144. }
  145. public function testTimezoneUsingDateTimeZone()
  146. {
  147. $d = Carbon::now();
  148. $d->timezone = new \DateTimeZone('America/Toronto');
  149. $this->assertSame('America/Toronto', $d->tzName);
  150. $d->timezone(new \DateTimeZone('America/Vancouver'));
  151. $this->assertSame('America/Vancouver', $d->tzName);
  152. }
  153. public function testTzUsingDateTimeZone()
  154. {
  155. $d = Carbon::now();
  156. $d->tz = new \DateTimeZone('America/Toronto');
  157. $this->assertSame('America/Toronto', $d->tzName);
  158. $d->tz(new \DateTimeZone('America/Vancouver'));
  159. $this->assertSame('America/Vancouver', $d->tzName);
  160. }
  161. public function testInvalidSetter()
  162. {
  163. $this->setExpectedException('InvalidArgumentException');
  164. $d = Carbon::now();
  165. $d->doesNotExit = 'bb';
  166. }
  167. }