SettersTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonInterval;
  12. use BadMethodCallException;
  13. use Carbon\CarbonInterval;
  14. use InvalidArgumentException;
  15. use Tests\AbstractTestCase;
  16. class SettersTest extends AbstractTestCase
  17. {
  18. public function testSet()
  19. {
  20. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  21. $ci->set('seconds', 34);
  22. $this->assertSame(34, $ci->seconds);
  23. $ci->set([
  24. 'seconds' => 59,
  25. 'minutes' => 33,
  26. ]);
  27. $this->assertSame(59, $ci->seconds);
  28. $this->assertSame(33, $ci->minutes);
  29. }
  30. public function testYearsSetter()
  31. {
  32. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  33. $ci->years = 2;
  34. $this->assertSame(2, $ci->years);
  35. $ci->years(5);
  36. $this->assertSame(5, $ci->years);
  37. }
  38. public function testMonthsSetter()
  39. {
  40. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  41. $ci->months = 11;
  42. $this->assertSame(11, $ci->months);
  43. $ci->months(8);
  44. $this->assertSame(8, $ci->months);
  45. }
  46. public function testWeeksSetter()
  47. {
  48. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  49. $ci->weeks = 11;
  50. $this->assertSame(11, $ci->weeks);
  51. $this->assertSame(7 * 11, $ci->dayz);
  52. $ci->weeks(4);
  53. $this->assertSame(4, $ci->weeks);
  54. }
  55. public function testDayzSetter()
  56. {
  57. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  58. $ci->dayz = 11;
  59. $this->assertSame(11, $ci->dayz);
  60. $this->assertSame(1, $ci->weeks);
  61. $this->assertSame(4, $ci->dayzExcludeWeeks);
  62. $ci->days(1);
  63. $this->assertSame(1, $ci->dayz);
  64. $ci->day(3);
  65. $this->assertSame(3, $ci->dayz);
  66. }
  67. public function testHoursSetter()
  68. {
  69. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  70. $ci->hours = 12;
  71. $this->assertSame(12, $ci->hours);
  72. $ci->hours(0);
  73. $this->assertSame(0, $ci->hours);
  74. }
  75. public function testMinutesSetter()
  76. {
  77. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  78. $ci->minutes = 11;
  79. $this->assertSame(11, $ci->minutes);
  80. $ci->minutes(9);
  81. $this->assertSame(9, $ci->minutes);
  82. }
  83. public function testSecondsSetter()
  84. {
  85. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  86. $ci->seconds = 34;
  87. $this->assertSame(34, $ci->seconds);
  88. $ci->seconds(59);
  89. $this->assertSame(59, $ci->seconds);
  90. $ci->second(1);
  91. $this->assertSame(1, $ci->seconds);
  92. }
  93. public function testMillisecondsSetter()
  94. {
  95. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  96. $ci->milliseconds = 34;
  97. $this->assertSame(34, $ci->milliseconds);
  98. $ci->milliseconds(59);
  99. $this->assertSame(59, $ci->milliseconds);
  100. $ci->millisecond(1);
  101. $this->assertSame(1, $ci->milliseconds);
  102. }
  103. public function testMicrosecondsSetter()
  104. {
  105. $ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
  106. $ci->microseconds = 34;
  107. $this->assertSame(34, $ci->microseconds);
  108. $ci->microseconds(59);
  109. $this->assertSame(59, $ci->microseconds);
  110. $ci->microsecond(1);
  111. $this->assertSame(1, $ci->microseconds);
  112. }
  113. public function testFluentSetters()
  114. {
  115. $ci = CarbonInterval::years(4)->months(2)->dayz(5)->hours(3)->minute()->seconds(59);
  116. $this->assertInstanceOfCarbonInterval($ci);
  117. $this->assertCarbonInterval($ci, 4, 2, 5, 3, 1, 59);
  118. $ci = CarbonInterval::years(4)->months(2)->weeks(2)->hours(3)->minute()->seconds(59);
  119. $this->assertInstanceOfCarbonInterval($ci);
  120. $this->assertCarbonInterval($ci, 4, 2, 14, 3, 1, 59);
  121. }
  122. public function testFluentSettersDaysOverwritesWeeks()
  123. {
  124. $ci = CarbonInterval::weeks(3)->days(5);
  125. $this->assertCarbonInterval($ci, 0, 0, 5, 0, 0, 0);
  126. }
  127. public function testFluentSettersWeeksOverwritesDays()
  128. {
  129. $ci = CarbonInterval::days(5)->weeks(3);
  130. $this->assertCarbonInterval($ci, 0, 0, 3 * 7, 0, 0, 0);
  131. }
  132. public function testFluentSettersWeeksAndDaysIsCumulative()
  133. {
  134. $ci = CarbonInterval::year(5)->weeksAndDays(2, 6);
  135. $this->assertCarbonInterval($ci, 5, 0, 20, 0, 0, 0);
  136. $this->assertSame(20, $ci->dayz);
  137. $this->assertSame(2, $ci->weeks);
  138. $this->assertSame(6, $ci->dayzExcludeWeeks);
  139. }
  140. public function testInvert()
  141. {
  142. $ci = new CarbonInterval();
  143. $this->assertSame($ci, $ci->invert());
  144. $this->assertSame(1, $ci->invert);
  145. $this->assertSame($ci, $ci->invert());
  146. $this->assertSame(0, $ci->invert);
  147. $this->assertSame($ci, $ci->invert(true));
  148. $this->assertSame(1, $ci->invert);
  149. $this->assertSame($ci, $ci->invert(true));
  150. $this->assertSame(1, $ci->invert);
  151. $this->assertSame($ci, $ci->invert(false));
  152. $this->assertSame(0, $ci->invert);
  153. $this->assertSame($ci, $ci->invert(false));
  154. $this->assertSame(0, $ci->invert);
  155. }
  156. public function testInvalidSetter()
  157. {
  158. $this->expectExceptionObject(new InvalidArgumentException(
  159. 'Unknown setter \'doesNotExit\''
  160. ));
  161. /** @var mixed $ci */
  162. $ci = new CarbonInterval();
  163. $ci->doesNotExit = 123;
  164. }
  165. public function testInvalidFluentSetter()
  166. {
  167. $this->expectExceptionObject(new BadMethodCallException(
  168. 'Unknown fluent setter \'doesNotExit\''
  169. ));
  170. /** @var mixed $ci */
  171. $ci = new CarbonInterval();
  172. $ci->doesNotExit(123);
  173. }
  174. public function testInvalidStaticFluentSetter()
  175. {
  176. $this->expectExceptionObject(new BadMethodCallException(
  177. 'Unknown fluent constructor \'doesNotExit\''
  178. ));
  179. CarbonInterval::doesNotExit(123);
  180. }
  181. public function testLocale()
  182. {
  183. /** @var CarbonInterval $interval */
  184. $interval = CarbonInterval::hour()->locale('de');
  185. $this->assertSame('de', $interval->locale);
  186. }
  187. public function testTimezone()
  188. {
  189. $interval = CarbonInterval::hour()->shiftTimezone('America/Toronto');
  190. $this->assertSame('America/Toronto', $interval->getSettings()['timezone']);
  191. }
  192. }