AddMonthsTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\CarbonImmutable;
  12. use Carbon\CarbonImmutable as Carbon;
  13. use Tests\AbstractTestCase;
  14. class AddMonthsTest extends AbstractTestCase
  15. {
  16. /**
  17. * @var \Carbon\CarbonImmutable
  18. */
  19. private $carbon;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. /** @var Carbon $date */
  24. $date = Carbon::create(2016, 1, 31);
  25. $this->carbon = $date;
  26. }
  27. public static function dataForTestAddMonthNoOverflow()
  28. {
  29. return [
  30. [-2, 2015, 11, 30],
  31. [-1, 2015, 12, 31],
  32. [0, 2016, 1, 31],
  33. [1, 2016, 2, 29],
  34. [2, 2016, 3, 31],
  35. ];
  36. }
  37. /**
  38. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthNoOverflow
  39. *
  40. * @param int $months
  41. * @param int $y
  42. * @param int $m
  43. * @param int $d
  44. */
  45. public function testAddMonthNoOverflow($months, $y, $m, $d)
  46. {
  47. $this->assertCarbon($this->carbon->addMonthNoOverflow($months), $y, $m, $d);
  48. }
  49. /**
  50. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthNoOverflow
  51. *
  52. * @param int $months
  53. * @param int $y
  54. * @param int $m
  55. * @param int $d
  56. */
  57. public function testAddMonthsNoOverflow($months, $y, $m, $d)
  58. {
  59. $this->assertCarbon($this->carbon->addMonthsNoOverflow($months), $y, $m, $d);
  60. }
  61. public static function dataForTestSubMonthNoOverflow()
  62. {
  63. return [
  64. [-2, 2016, 3, 31],
  65. [-1, 2016, 2, 29],
  66. [0, 2016, 1, 31],
  67. [1, 2015, 12, 31],
  68. [2, 2015, 11, 30],
  69. ];
  70. }
  71. /**
  72. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthNoOverflow
  73. *
  74. * @param int $months
  75. * @param int $y
  76. * @param int $m
  77. * @param int $d
  78. */
  79. public function testSubMonthNoOverflow($months, $y, $m, $d)
  80. {
  81. $this->assertCarbon($this->carbon->subMonthNoOverflow($months), $y, $m, $d);
  82. }
  83. /**
  84. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthNoOverflow
  85. *
  86. * @param int $months
  87. * @param int $y
  88. * @param int $m
  89. * @param int $d
  90. */
  91. public function testSubMonthsNoOverflow($months, $y, $m, $d)
  92. {
  93. $this->assertCarbon($this->carbon->subMonthsNoOverflow($months), $y, $m, $d);
  94. }
  95. public static function dataForTestAddMonthWithOverflow()
  96. {
  97. return [
  98. [-2, 2015, 12, 1],
  99. [-1, 2015, 12, 31],
  100. [0, 2016, 1, 31],
  101. [1, 2016, 3, 2],
  102. [2, 2016, 3, 31],
  103. ];
  104. }
  105. /**
  106. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthWithOverflow
  107. *
  108. * @param int $months
  109. * @param int $y
  110. * @param int $m
  111. * @param int $d
  112. */
  113. public function testAddMonthWithOverflow($months, $y, $m, $d)
  114. {
  115. $this->assertCarbon($this->carbon->addMonthWithOverflow($months), $y, $m, $d);
  116. }
  117. /**
  118. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthWithOverflow
  119. *
  120. * @param int $months
  121. * @param int $y
  122. * @param int $m
  123. * @param int $d
  124. */
  125. public function testAddMonthsWithOverflow($months, $y, $m, $d)
  126. {
  127. $this->assertCarbon($this->carbon->addMonthsWithOverflow($months), $y, $m, $d);
  128. }
  129. public static function dataForTestSubMonthWithOverflow()
  130. {
  131. return [
  132. [-2, 2016, 3, 31],
  133. [-1, 2016, 3, 2],
  134. [0, 2016, 1, 31],
  135. [1, 2015, 12, 31],
  136. [2, 2015, 12, 1],
  137. ];
  138. }
  139. /**
  140. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthWithOverflow
  141. *
  142. * @param int $months
  143. * @param int $y
  144. * @param int $m
  145. * @param int $d
  146. */
  147. public function testSubMonthWithOverflow($months, $y, $m, $d)
  148. {
  149. $this->assertCarbon($this->carbon->subMonthWithOverflow($months), $y, $m, $d);
  150. }
  151. /**
  152. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthWithOverflow
  153. *
  154. * @param int $months
  155. * @param int $y
  156. * @param int $m
  157. * @param int $d
  158. */
  159. public function testSubMonthsWithOverflow($months, $y, $m, $d)
  160. {
  161. $this->assertCarbon($this->carbon->subMonthsWithOverflow($months), $y, $m, $d);
  162. }
  163. public function testSetOverflowIsTrue()
  164. {
  165. Carbon::useMonthsOverflow(true);
  166. $this->assertTrue(Carbon::shouldOverflowMonths());
  167. }
  168. public function testSetOverflowIsFalse()
  169. {
  170. Carbon::useMonthsOverflow(false);
  171. $this->assertFalse(Carbon::shouldOverflowMonths());
  172. }
  173. public function testSetOverflowIsResetInTests()
  174. {
  175. $this->assertTrue(Carbon::shouldOverflowMonths());
  176. }
  177. public function testSetOverflowIsReset()
  178. {
  179. Carbon::useMonthsOverflow(false);
  180. $this->assertFalse(Carbon::shouldOverflowMonths());
  181. Carbon::resetMonthsOverflow();
  182. $this->assertTrue(Carbon::shouldOverflowMonths());
  183. }
  184. /**
  185. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthWithOverflow
  186. *
  187. * @param int $months
  188. * @param int $y
  189. * @param int $m
  190. * @param int $d
  191. */
  192. public function testUseOverflowAddMonth($months, $y, $m, $d)
  193. {
  194. Carbon::useMonthsOverflow(true);
  195. $this->assertCarbon($this->carbon->addMonth($months), $y, $m, $d);
  196. }
  197. /**
  198. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthWithOverflow
  199. *
  200. * @param int $months
  201. * @param int $y
  202. * @param int $m
  203. * @param int $d
  204. */
  205. public function testUseOverflowAddMonths($months, $y, $m, $d)
  206. {
  207. Carbon::useMonthsOverflow(true);
  208. $this->assertCarbon($this->carbon->addMonths($months), $y, $m, $d);
  209. }
  210. /**
  211. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthWithOverflow
  212. *
  213. * @param int $months
  214. * @param int $y
  215. * @param int $m
  216. * @param int $d
  217. */
  218. public function testUseOverflowSubMonth($months, $y, $m, $d)
  219. {
  220. Carbon::useMonthsOverflow(true);
  221. $this->assertCarbon($this->carbon->subMonth($months), $y, $m, $d);
  222. }
  223. /**
  224. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthWithOverflow
  225. *
  226. * @param int $months
  227. * @param int $y
  228. * @param int $m
  229. * @param int $d
  230. */
  231. public function testUseOverflowSubMonths($months, $y, $m, $d)
  232. {
  233. Carbon::useMonthsOverflow(true);
  234. $this->assertCarbon($this->carbon->subMonths($months), $y, $m, $d);
  235. }
  236. /**
  237. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthNoOverflow
  238. *
  239. * @param int $months
  240. * @param int $y
  241. * @param int $m
  242. * @param int $d
  243. */
  244. public function testSkipOverflowAddMonth($months, $y, $m, $d)
  245. {
  246. Carbon::useMonthsOverflow(false);
  247. $this->assertCarbon($this->carbon->addMonth($months), $y, $m, $d);
  248. }
  249. /**
  250. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestAddMonthNoOverflow
  251. *
  252. * @param int $months
  253. * @param int $y
  254. * @param int $m
  255. * @param int $d
  256. */
  257. public function testSkipOverflowAddMonths($months, $y, $m, $d)
  258. {
  259. Carbon::useMonthsOverflow(false);
  260. $this->assertCarbon($this->carbon->addMonths($months), $y, $m, $d);
  261. }
  262. /**
  263. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthNoOverflow
  264. *
  265. * @param int $months
  266. * @param int $y
  267. * @param int $m
  268. * @param int $d
  269. */
  270. public function testSkipOverflowSubMonth($months, $y, $m, $d)
  271. {
  272. Carbon::useMonthsOverflow(false);
  273. $this->assertCarbon($this->carbon->subMonth($months), $y, $m, $d);
  274. }
  275. /**
  276. * @dataProvider \Tests\CarbonImmutable\AddMonthsTest::dataForTestSubMonthNoOverflow
  277. *
  278. * @param int $months
  279. * @param int $y
  280. * @param int $m
  281. * @param int $d
  282. */
  283. public function testSkipOverflowSubMonths($months, $y, $m, $d)
  284. {
  285. Carbon::useMonthsOverflow(false);
  286. $this->assertCarbon($this->carbon->subMonths($months), $y, $m, $d);
  287. }
  288. }