DayOfWeekModifiersTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 DayOfWeekModifiersTest extends AbstractTestCase
  15. {
  16. public function testGetWeekendDays()
  17. {
  18. $this->assertSame([Carbon::SATURDAY, Carbon::SUNDAY], Carbon::getWeekendDays());
  19. }
  20. public function testSetWeekendDays()
  21. {
  22. Carbon::setWeekendDays([Carbon::THURSDAY, Carbon::FRIDAY]);
  23. $this->assertSame([Carbon::THURSDAY, Carbon::FRIDAY], Carbon::getWeekendDays());
  24. $this->assertTrue(Carbon::createFromDate(2018, 2, 16)->isWeekend());
  25. Carbon::setWeekendDays([Carbon::SATURDAY, Carbon::SUNDAY]);
  26. $this->assertSame([Carbon::SATURDAY, Carbon::SUNDAY], Carbon::getWeekendDays());
  27. $this->assertFalse(Carbon::createFromDate(2018, 2, 16)->isWeekend());
  28. }
  29. public function testGetWeekEndsAt()
  30. {
  31. Carbon::setWeekEndsAt(Carbon::SATURDAY);
  32. $this->assertSame(Carbon::SATURDAY, Carbon::getWeekEndsAt());
  33. Carbon::setWeekEndsAt(Carbon::SUNDAY);
  34. }
  35. public function testGetWeekStartsAt()
  36. {
  37. Carbon::setWeekStartsAt(Carbon::TUESDAY);
  38. $this->assertSame(Carbon::TUESDAY, Carbon::getWeekStartsAt());
  39. Carbon::setWeekStartsAt(Carbon::MONDAY);
  40. }
  41. public function testStartOfWeek()
  42. {
  43. $d = Carbon::create(1980, 8, 7, 12, 11, 9)->startOfWeek();
  44. $this->assertCarbon($d, 1980, 8, 4, 0, 0, 0);
  45. }
  46. public function testStartOfWeekFromWeekStart()
  47. {
  48. $d = Carbon::createFromDate(1980, 8, 4)->startOfWeek();
  49. $this->assertCarbon($d, 1980, 8, 4, 0, 0, 0);
  50. }
  51. public function testStartOfWeekCrossingYearBoundary()
  52. {
  53. $d = Carbon::createFromDate(2013, 12, 31, 'GMT');
  54. $d2 = $d->startOfWeek();
  55. $this->assertCarbon($d, 2013, 12, 31);
  56. $this->assertCarbon($d2, 2013, 12, 30, 0, 0, 0);
  57. }
  58. public function testEndOfWeek()
  59. {
  60. $d = Carbon::create(1980, 8, 7, 11, 12, 13)->endOfWeek();
  61. $this->assertCarbon($d, 1980, 8, 10, 23, 59, 59);
  62. }
  63. public function testEndOfWeekFromWeekEnd()
  64. {
  65. $d = Carbon::createFromDate(1980, 8, 9)->endOfWeek();
  66. $this->assertCarbon($d, 1980, 8, 10, 23, 59, 59);
  67. }
  68. public function testEndOfWeekCrossingYearBoundary()
  69. {
  70. $d = Carbon::createFromDate(2013, 12, 31, 'GMT');
  71. $d2 = $d->endOfWeek();
  72. $this->assertCarbon($d, 2013, 12, 31);
  73. $this->assertCarbon($d2, 2014, 1, 5, 23, 59, 59);
  74. }
  75. /**
  76. * @see https://github.com/briannesbitt/Carbon/issues/735
  77. */
  78. public function testStartOrEndOfWeekFromWeekWithUTC()
  79. {
  80. $d = Carbon::create(2016, 7, 27, 17, 13, 7, 'UTC');
  81. $this->assertCarbon($d->copy()->startOfWeek(), 2016, 7, 25, 0, 0, 0);
  82. $this->assertCarbon($d->copy()->endOfWeek(), 2016, 7, 31, 23, 59, 59);
  83. }
  84. /**
  85. * @see https://github.com/briannesbitt/Carbon/issues/735
  86. */
  87. public function testStartOrEndOfWeekFromWeekWithOtherTimezone()
  88. {
  89. $d = Carbon::create(2016, 7, 27, 17, 13, 7, 'America/New_York');
  90. $this->assertCarbon($d->copy()->startOfWeek(), 2016, 7, 25, 0, 0, 0);
  91. $this->assertCarbon($d->copy()->endOfWeek(), 2016, 7, 31, 23, 59, 59);
  92. }
  93. public function testNext()
  94. {
  95. $d = Carbon::createFromDate(1975, 5, 21)->next();
  96. $this->assertCarbon($d, 1975, 5, 28, 0, 0, 0);
  97. }
  98. public function testNextMonday()
  99. {
  100. $d = Carbon::createFromDate(1975, 5, 21)->next(Carbon::MONDAY);
  101. $this->assertCarbon($d, 1975, 5, 26, 0, 0, 0);
  102. }
  103. public function testNextSaturday()
  104. {
  105. $d = Carbon::createFromDate(1975, 5, 21)->next(6);
  106. $this->assertCarbon($d, 1975, 5, 24, 0, 0, 0);
  107. }
  108. public function testNextTimestamp()
  109. {
  110. $d = Carbon::createFromDate(1975, 11, 14)->next();
  111. $this->assertCarbon($d, 1975, 11, 21, 0, 0, 0);
  112. }
  113. public function testPrevious()
  114. {
  115. $d = Carbon::createFromDate(1975, 5, 21)->previous();
  116. $this->assertCarbon($d, 1975, 5, 14, 0, 0, 0);
  117. }
  118. public function testPreviousMonday()
  119. {
  120. $d = Carbon::createFromDate(1975, 5, 21)->previous(Carbon::MONDAY);
  121. $this->assertCarbon($d, 1975, 5, 19, 0, 0, 0);
  122. }
  123. public function testPreviousSaturday()
  124. {
  125. $d = Carbon::createFromDate(1975, 5, 21)->previous(6);
  126. $this->assertCarbon($d, 1975, 5, 17, 0, 0, 0);
  127. }
  128. public function testPreviousTimestamp()
  129. {
  130. $d = Carbon::createFromDate(1975, 11, 28)->previous();
  131. $this->assertCarbon($d, 1975, 11, 21, 0, 0, 0);
  132. }
  133. public function testFirstDayOfMonth()
  134. {
  135. $d = Carbon::createFromDate(1975, 11, 21)->firstOfMonth();
  136. $this->assertCarbon($d, 1975, 11, 1, 0, 0, 0);
  137. }
  138. public function testFirstWednesdayOfMonth()
  139. {
  140. $d = Carbon::createFromDate(1975, 11, 21)->firstOfMonth(Carbon::WEDNESDAY);
  141. $this->assertCarbon($d, 1975, 11, 5, 0, 0, 0);
  142. }
  143. public function testFirstFridayOfMonth()
  144. {
  145. $d = Carbon::createFromDate(1975, 11, 21)->firstOfMonth(5);
  146. $this->assertCarbon($d, 1975, 11, 7, 0, 0, 0);
  147. }
  148. public function testLastDayOfMonth()
  149. {
  150. $d = Carbon::createFromDate(1975, 12, 5)->lastOfMonth();
  151. $this->assertCarbon($d, 1975, 12, 31, 0, 0, 0);
  152. }
  153. public function testLastTuesdayOfMonth()
  154. {
  155. $d = Carbon::createFromDate(1975, 12, 1)->lastOfMonth(Carbon::TUESDAY);
  156. $this->assertCarbon($d, 1975, 12, 30, 0, 0, 0);
  157. }
  158. public function testLastFridayOfMonth()
  159. {
  160. $d = Carbon::createFromDate(1975, 12, 5)->lastOfMonth(5);
  161. $this->assertCarbon($d, 1975, 12, 26, 0, 0, 0);
  162. }
  163. public function testNthOfMonthOutsideScope()
  164. {
  165. $this->assertFalse(Carbon::createFromDate(1975, 12, 5)->nthOfMonth(6, Carbon::MONDAY));
  166. }
  167. public function testNthOfMonthOutsideYear()
  168. {
  169. $this->assertFalse(Carbon::createFromDate(1975, 12, 5)->nthOfMonth(55, Carbon::MONDAY));
  170. }
  171. public function test2ndMondayOfMonth()
  172. {
  173. $d = Carbon::createFromDate(1975, 12, 5)->nthOfMonth(2, Carbon::MONDAY);
  174. $this->assertCarbon($d, 1975, 12, 8, 0, 0, 0);
  175. }
  176. public function test3rdWednesdayOfMonth()
  177. {
  178. $d = Carbon::createFromDate(1975, 12, 5)->nthOfMonth(3, 3);
  179. $this->assertCarbon($d, 1975, 12, 17, 0, 0, 0);
  180. }
  181. public function testFirstDayOfQuarter()
  182. {
  183. $d = Carbon::createFromDate(1975, 11, 21)->firstOfQuarter();
  184. $this->assertCarbon($d, 1975, 10, 1, 0, 0, 0);
  185. }
  186. public function testFirstWednesdayOfQuarter()
  187. {
  188. $d = Carbon::createFromDate(1975, 11, 21)->firstOfQuarter(Carbon::WEDNESDAY);
  189. $this->assertCarbon($d, 1975, 10, 1, 0, 0, 0);
  190. }
  191. public function testFirstFridayOfQuarter()
  192. {
  193. $d = Carbon::createFromDate(1975, 11, 21)->firstOfQuarter(5);
  194. $this->assertCarbon($d, 1975, 10, 3, 0, 0, 0);
  195. }
  196. public function testFirstOfQuarterFromADayThatWillNotExistInTheFirstMonth()
  197. {
  198. $d = Carbon::createFromDate(2014, 5, 31)->firstOfQuarter();
  199. $this->assertCarbon($d, 2014, 4, 1, 0, 0, 0);
  200. }
  201. public function testLastDayOfQuarter()
  202. {
  203. $d = Carbon::createFromDate(1975, 8, 5)->lastOfQuarter();
  204. $this->assertCarbon($d, 1975, 9, 30, 0, 0, 0);
  205. }
  206. public function testLastTuesdayOfQuarter()
  207. {
  208. $d = Carbon::createFromDate(1975, 8, 1)->lastOfQuarter(Carbon::TUESDAY);
  209. $this->assertCarbon($d, 1975, 9, 30, 0, 0, 0);
  210. }
  211. public function testLastFridayOfQuarter()
  212. {
  213. $d = Carbon::createFromDate(1975, 7, 5)->lastOfQuarter(5);
  214. $this->assertCarbon($d, 1975, 9, 26, 0, 0, 0);
  215. }
  216. public function testLastOfQuarterFromADayThatWillNotExistInTheLastMonth()
  217. {
  218. $d = Carbon::createFromDate(2014, 5, 31)->lastOfQuarter();
  219. $this->assertCarbon($d, 2014, 6, 30, 0, 0, 0);
  220. }
  221. public function testNthOfQuarterOutsideScope()
  222. {
  223. $this->assertFalse(Carbon::createFromDate(1975, 1, 5)->nthOfQuarter(20, Carbon::MONDAY));
  224. }
  225. public function testNthOfQuarterOutsideYear()
  226. {
  227. $this->assertFalse(Carbon::createFromDate(1975, 1, 5)->nthOfQuarter(55, Carbon::MONDAY));
  228. }
  229. public function testNthOfQuarterFromADayThatWillNotExistInTheFirstMonth()
  230. {
  231. $d = Carbon::createFromDate(2014, 5, 31)->nthOfQuarter(2, Carbon::MONDAY);
  232. $this->assertCarbon($d, 2014, 4, 14, 0, 0, 0);
  233. }
  234. public function test2ndMondayOfQuarter()
  235. {
  236. $d = Carbon::createFromDate(1975, 8, 5)->nthOfQuarter(2, Carbon::MONDAY);
  237. $this->assertCarbon($d, 1975, 7, 14, 0, 0, 0);
  238. }
  239. public function test3rdWednesdayOfQuarter()
  240. {
  241. $d = Carbon::createFromDate(1975, 8, 5)->nthOfQuarter(3, 3);
  242. $this->assertCarbon($d, 1975, 7, 16, 0, 0, 0);
  243. }
  244. public function testFirstDayOfYear()
  245. {
  246. $d = Carbon::createFromDate(1975, 11, 21)->firstOfYear();
  247. $this->assertCarbon($d, 1975, 1, 1, 0, 0, 0);
  248. }
  249. public function testFirstWednesdayOfYear()
  250. {
  251. $d = Carbon::createFromDate(1975, 11, 21)->firstOfYear(Carbon::WEDNESDAY);
  252. $this->assertCarbon($d, 1975, 1, 1, 0, 0, 0);
  253. }
  254. public function testFirstFridayOfYear()
  255. {
  256. $d = Carbon::createFromDate(1975, 11, 21)->firstOfYear(5);
  257. $this->assertCarbon($d, 1975, 1, 3, 0, 0, 0);
  258. }
  259. public function testLastDayOfYear()
  260. {
  261. $d = Carbon::createFromDate(1975, 8, 5)->lastOfYear();
  262. $this->assertCarbon($d, 1975, 12, 31, 0, 0, 0);
  263. }
  264. public function testLastTuesdayOfYear()
  265. {
  266. $d = Carbon::createFromDate(1975, 8, 1)->lastOfYear(Carbon::TUESDAY);
  267. $this->assertCarbon($d, 1975, 12, 30, 0, 0, 0);
  268. }
  269. public function testLastFridayOfYear()
  270. {
  271. $d = Carbon::createFromDate(1975, 7, 5)->lastOfYear(5);
  272. $this->assertCarbon($d, 1975, 12, 26, 0, 0, 0);
  273. }
  274. public function testNthOfYearOutsideScope()
  275. {
  276. $this->assertFalse(Carbon::createFromDate(1975, 1, 5)->nthOfYear(55, Carbon::MONDAY));
  277. }
  278. public function test2ndMondayOfYear()
  279. {
  280. $d = Carbon::createFromDate(1975, 8, 5)->nthOfYear(2, Carbon::MONDAY);
  281. $this->assertCarbon($d, 1975, 1, 13, 0, 0, 0);
  282. }
  283. public function test3rdWednesdayOfYear()
  284. {
  285. $d = Carbon::createFromDate(1975, 8, 5)->nthOfYear(3, 3);
  286. $this->assertCarbon($d, 1975, 1, 15, 0, 0, 0);
  287. }
  288. public function testNextWeekday()
  289. {
  290. // Friday to Monday
  291. $d = Carbon::create(2016, 7, 15)->nextWeekday();
  292. $this->assertCarbon($d, 2016, 7, 18);
  293. // Saturday to Monday
  294. $d = Carbon::create(2016, 7, 16)->nextWeekday();
  295. $this->assertCarbon($d, 2016, 7, 18);
  296. // Sunday to Monday
  297. $d = Carbon::create(2016, 7, 16)->nextWeekday();
  298. $this->assertCarbon($d, 2016, 7, 18);
  299. // Monday to Tuesday
  300. $d = Carbon::create(2016, 7, 17)->nextWeekday();
  301. $this->assertCarbon($d, 2016, 7, 18);
  302. }
  303. public function testPreviousWeekday()
  304. {
  305. // Tuesday to Monday
  306. $d = Carbon::create(2016, 7, 19)->previousWeekday();
  307. $this->assertCarbon($d, 2016, 7, 18);
  308. // Monday to Friday
  309. $d = Carbon::create(2016, 7, 18)->previousWeekday();
  310. $this->assertCarbon($d, 2016, 7, 15);
  311. // Sunday to Friday
  312. $d = Carbon::create(2016, 7, 17)->previousWeekday();
  313. $this->assertCarbon($d, 2016, 7, 15);
  314. // Saturday to Friday
  315. $d = Carbon::create(2016, 7, 16)->previousWeekday();
  316. $this->assertCarbon($d, 2016, 7, 15);
  317. }
  318. public function testNextWeekendDay()
  319. {
  320. // Thursday to Saturday
  321. $d = Carbon::create(2016, 7, 14)->nextWeekendDay();
  322. $this->assertCarbon($d, 2016, 7, 16);
  323. // Friday to Saturday
  324. $d = Carbon::create(2016, 7, 15)->nextWeekendDay();
  325. $this->assertCarbon($d, 2016, 7, 16);
  326. // Saturday to Sunday
  327. $d = Carbon::create(2016, 7, 16)->nextWeekendDay();
  328. $this->assertCarbon($d, 2016, 7, 17);
  329. // Sunday to Saturday
  330. $d = Carbon::create(2016, 7, 17)->nextWeekendDay();
  331. $this->assertCarbon($d, 2016, 7, 23);
  332. }
  333. public function testPreviousWeekendDay()
  334. {
  335. // Thursday to Sunday
  336. $d = Carbon::create(2016, 7, 14)->previousWeekendDay();
  337. $this->assertCarbon($d, 2016, 7, 10);
  338. // Friday to Sunday
  339. $d = Carbon::create(2016, 7, 15)->previousWeekendDay();
  340. $this->assertCarbon($d, 2016, 7, 10);
  341. // Saturday to Sunday
  342. $d = Carbon::create(2016, 7, 16)->previousWeekendDay();
  343. $this->assertCarbon($d, 2016, 7, 10);
  344. // Sunday to Saturday
  345. $d = Carbon::create(2016, 7, 17)->previousWeekendDay();
  346. $this->assertCarbon($d, 2016, 7, 16);
  347. }
  348. public function testWeekStartAndEndWithAutoMode()
  349. {
  350. $this->assertSame('Monday', Carbon::now()->startOfWeek()->dayName);
  351. Carbon::setWeekStartsAt('auto');
  352. $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName);
  353. Carbon::setLocale('en_UM');
  354. $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName);
  355. Carbon::setLocale('en_US');
  356. $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName);
  357. Carbon::setLocale('en');
  358. $this->assertSame('Sunday', Carbon::now()->startOfWeek()->dayName);
  359. Carbon::setLocale('es_US');
  360. $this->assertSame('domingo', Carbon::now()->startOfWeek()->dayName);
  361. Carbon::setLocale('en_GB');
  362. $this->assertSame('Monday', Carbon::now()->startOfWeek()->dayName);
  363. Carbon::setWeekEndsAt('auto');
  364. Carbon::setLocale('en_UM');
  365. $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName);
  366. Carbon::setLocale('en_US');
  367. $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName);
  368. Carbon::setLocale('en');
  369. $this->assertSame('Saturday', Carbon::now()->endOfWeek()->dayName);
  370. Carbon::setLocale('es_US');
  371. $this->assertSame('sábado', Carbon::now()->endOfWeek()->dayName);
  372. Carbon::setLocale('en_GB');
  373. $this->assertSame('Sunday', Carbon::now()->endOfWeek()->dayName);
  374. Carbon::setWeekStartsAt(Carbon::MONDAY);
  375. Carbon::setWeekEndsAt(Carbon::SUNDAY);
  376. }
  377. }