No Description

SettersTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 testTimeSetter()
  74. {
  75. $d = Carbon::now();
  76. $d->setTime(1, 1, 1);
  77. $this->assertSame(1, $d->second);
  78. $d->setTime(1, 1);
  79. $this->assertSame(0, $d->second);
  80. }
  81. public function testTimeSetterWithChaining()
  82. {
  83. $d = Carbon::now();
  84. $d->setTime(2, 2, 2)->setTime(1, 1, 1);
  85. $this->assertInstanceOf('Carbon\Carbon', $d);
  86. $this->assertSame(1, $d->second);
  87. $d->setTime(2, 2, 2)->setTime(1, 1);
  88. $this->assertInstanceOf('Carbon\Carbon', $d);
  89. $this->assertSame(0, $d->second);
  90. }
  91. public function testTimeSetterWithZero()
  92. {
  93. $d = Carbon::now();
  94. $d->setTime(1, 1);
  95. $this->assertSame(0, $d->second);
  96. }
  97. public function testDateTimeSetter()
  98. {
  99. $d = Carbon::now();
  100. $d->setDateTime($d->year, $d->month, $d->day, 1, 1, 1);
  101. $this->assertSame(1, $d->second);
  102. }
  103. public function testDateTimeSetterWithZero()
  104. {
  105. $d = Carbon::now();
  106. $d->setDateTime($d->year, $d->month, $d->day, 1, 1);
  107. $this->assertSame(0, $d->second);
  108. }
  109. public function testDateTimeSetterWithChaining()
  110. {
  111. $d = Carbon::now();
  112. $d->setDateTime(2013, 9, 24, 17, 4, 29);
  113. $this->assertInstanceOf('Carbon\Carbon', $d);
  114. $d->setDateTime(2014, 10, 25, 18, 5, 30);
  115. $this->assertInstanceOf('Carbon\Carbon', $d);
  116. $this->assertCarbon($d, 2014, 10, 25, 18, 5, 30);
  117. }
  118. public function testSecondSetterWithWrap()
  119. {
  120. $d = Carbon::now();
  121. $d->second = 65;
  122. $this->assertSame(5, $d->second);
  123. }
  124. public function testTimestampSetter()
  125. {
  126. $d = Carbon::now();
  127. $d->timestamp = 10;
  128. $this->assertSame(10, $d->timestamp);
  129. $d->setTimestamp(11);
  130. $this->assertSame(11, $d->timestamp);
  131. }
  132. public function testSetTimezoneWithInvalidTimezone()
  133. {
  134. $this->setExpectedException('InvalidArgumentException');
  135. $d = Carbon::now();
  136. $d->setTimezone('sdf');
  137. }
  138. public function testTimezoneWithInvalidTimezone()
  139. {
  140. $d = Carbon::now();
  141. try {
  142. $d->timezone = 'sdf';
  143. $this->fail('InvalidArgumentException was not been raised.');
  144. } catch (InvalidArgumentException $expected) {
  145. }
  146. try {
  147. $d->timezone('sdf');
  148. $this->fail('InvalidArgumentException was not been raised.');
  149. } catch (InvalidArgumentException $expected) {
  150. }
  151. }
  152. public function testTzWithInvalidTimezone()
  153. {
  154. $d = Carbon::now();
  155. try {
  156. $d->tz = 'sdf';
  157. $this->fail('InvalidArgumentException was not been raised.');
  158. } catch (InvalidArgumentException $expected) {
  159. }
  160. try {
  161. $d->tz('sdf');
  162. $this->fail('InvalidArgumentException was not been raised.');
  163. } catch (InvalidArgumentException $expected) {
  164. }
  165. }
  166. public function testSetTimezoneUsingString()
  167. {
  168. $d = Carbon::now();
  169. $d->setTimezone('America/Toronto');
  170. $this->assertSame('America/Toronto', $d->tzName);
  171. }
  172. public function testTimezoneUsingString()
  173. {
  174. $d = Carbon::now();
  175. $d->timezone = 'America/Toronto';
  176. $this->assertSame('America/Toronto', $d->tzName);
  177. $d->timezone('America/Vancouver');
  178. $this->assertSame('America/Vancouver', $d->tzName);
  179. }
  180. public function testTzUsingString()
  181. {
  182. $d = Carbon::now();
  183. $d->tz = 'America/Toronto';
  184. $this->assertSame('America/Toronto', $d->tzName);
  185. $d->tz('America/Vancouver');
  186. $this->assertSame('America/Vancouver', $d->tzName);
  187. }
  188. public function testSetTimezoneUsingDateTimeZone()
  189. {
  190. $d = Carbon::now();
  191. $d->setTimezone(new \DateTimeZone('America/Toronto'));
  192. $this->assertSame('America/Toronto', $d->tzName);
  193. }
  194. public function testTimezoneUsingDateTimeZone()
  195. {
  196. $d = Carbon::now();
  197. $d->timezone = new \DateTimeZone('America/Toronto');
  198. $this->assertSame('America/Toronto', $d->tzName);
  199. $d->timezone(new \DateTimeZone('America/Vancouver'));
  200. $this->assertSame('America/Vancouver', $d->tzName);
  201. }
  202. public function testTzUsingDateTimeZone()
  203. {
  204. $d = Carbon::now();
  205. $d->tz = new \DateTimeZone('America/Toronto');
  206. $this->assertSame('America/Toronto', $d->tzName);
  207. $d->tz(new \DateTimeZone('America/Vancouver'));
  208. $this->assertSame('America/Vancouver', $d->tzName);
  209. }
  210. public function testInvalidSetter()
  211. {
  212. $this->setExpectedException('InvalidArgumentException');
  213. $d = Carbon::now();
  214. $d->doesNotExit = 'bb';
  215. }
  216. }