SettersTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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\Carbon;
  12. use Carbon\Carbon;
  13. use Carbon\Exceptions\UnitException;
  14. use DateTimeZone;
  15. use Exception;
  16. use Generator;
  17. use InvalidArgumentException;
  18. use Tests\AbstractTestCase;
  19. class SettersTest extends AbstractTestCase
  20. {
  21. public const SET_UNIT_NO_OVERFLOW_SAMPLE = 200;
  22. public function testSingularUnit()
  23. {
  24. $this->assertSame('year', Carbon::singularUnit('year'));
  25. $this->assertSame('year', Carbon::singularUnit('Years'));
  26. $this->assertSame('century', Carbon::singularUnit('centuries'));
  27. $this->assertSame('millennium', Carbon::singularUnit('Millennia'));
  28. $this->assertSame('millennium', Carbon::singularUnit('millenniums'));
  29. }
  30. public function testPluralUnit()
  31. {
  32. $this->assertSame('years', Carbon::pluralUnit('year'));
  33. $this->assertSame('years', Carbon::pluralUnit('Years'));
  34. $this->assertSame('centuries', Carbon::pluralUnit('century'));
  35. $this->assertSame('centuries', Carbon::pluralUnit('centuries'));
  36. $this->assertSame('millennia', Carbon::pluralUnit('Millennia'));
  37. $this->assertSame('millennia', Carbon::pluralUnit('millenniums'));
  38. $this->assertSame('millennia', Carbon::pluralUnit('millennium'));
  39. }
  40. public function testSet()
  41. {
  42. $d = Carbon::create(2000, 1, 12);
  43. $d->set([
  44. 'year' => 1995,
  45. 'month' => 4,
  46. ]);
  47. $this->assertSame(1995, $d->year);
  48. $this->assertSame(4, $d->month);
  49. $this->assertSame(12, $d->day);
  50. }
  51. public function testYearSetter()
  52. {
  53. $d = Carbon::now();
  54. $d->year = 1995;
  55. $this->assertSame(1995, $d->year);
  56. }
  57. public function testMonthSetter()
  58. {
  59. $d = Carbon::now();
  60. $d->month = 3;
  61. $this->assertSame(3, $d->month);
  62. }
  63. public function testMonthSetterWithWrap()
  64. {
  65. $d = Carbon::now();
  66. $d->month = 13;
  67. $this->assertSame(1, $d->month);
  68. }
  69. public function testDaySetter()
  70. {
  71. $d = Carbon::now();
  72. $d->day = 2;
  73. $this->assertSame(2, $d->day);
  74. }
  75. public function testDaySetterWithWrap()
  76. {
  77. $d = Carbon::createFromDate(2012, 8, 5);
  78. $d->day = 32;
  79. $this->assertSame(1, $d->day);
  80. }
  81. public function testHourSetter()
  82. {
  83. $d = Carbon::now();
  84. $d->hour = 2;
  85. $this->assertSame(2, $d->hour);
  86. }
  87. public function testHourSetterWithWrap()
  88. {
  89. $d = Carbon::now();
  90. $d->hour = 25;
  91. $this->assertSame(1, $d->hour);
  92. }
  93. public function testMinuteSetter()
  94. {
  95. $d = Carbon::now();
  96. $d->minute = 2;
  97. $this->assertSame(2, $d->minute);
  98. }
  99. public function testMinuteSetterWithWrap()
  100. {
  101. $d = Carbon::now();
  102. $d->minute = 65;
  103. $this->assertSame(5, $d->minute);
  104. }
  105. public function testSecondSetter()
  106. {
  107. $d = Carbon::now();
  108. $d->second = 2;
  109. $this->assertSame(2, $d->second);
  110. }
  111. public function testTimeSetter()
  112. {
  113. $d = Carbon::now();
  114. $d->setTime(1, 1, 1);
  115. $this->assertSame(1, $d->second);
  116. $d->setTime(1, 1);
  117. $this->assertSame(0, $d->second);
  118. }
  119. public function testTimeSetterWithChaining()
  120. {
  121. $d = Carbon::now();
  122. $d->setTime(2, 2, 2)->setTime(1, 1, 1);
  123. $this->assertInstanceOfCarbon($d);
  124. $this->assertSame(1, $d->second);
  125. $d->setTime(2, 2, 2)->setTime(1, 1);
  126. $this->assertInstanceOfCarbon($d);
  127. $this->assertSame(0, $d->second);
  128. }
  129. public function testTimeSetterWithZero()
  130. {
  131. $d = Carbon::now();
  132. $d->setTime(1, 1);
  133. $this->assertSame(0, $d->second);
  134. }
  135. public function testDateTimeSetter()
  136. {
  137. $d = Carbon::now();
  138. $d->setDateTime($d->year, $d->month, $d->day, 1, 1, 1);
  139. $this->assertSame(1, $d->second);
  140. }
  141. public function testDateTimeSetterWithZero()
  142. {
  143. $d = Carbon::now();
  144. $d->setDateTime($d->year, $d->month, $d->day, 1, 1);
  145. $this->assertSame(0, $d->second);
  146. }
  147. public function testDateTimeSetterWithChaining()
  148. {
  149. $d = Carbon::now();
  150. $d->setDateTime(2013, 9, 24, 17, 4, 29);
  151. $this->assertInstanceOfCarbon($d);
  152. $d->setDateTime(2014, 10, 25, 18, 5, 30);
  153. $this->assertInstanceOfCarbon($d);
  154. $this->assertCarbon($d, 2014, 10, 25, 18, 5, 30);
  155. }
  156. /**
  157. * @link https://github.com/briannesbitt/Carbon/issues/539
  158. */
  159. public function testSetDateAfterStringCreation()
  160. {
  161. $d = new Carbon('first day of this month');
  162. $this->assertSame(1, $d->day);
  163. $d->setDate($d->year, $d->month, 12);
  164. $this->assertSame(12, $d->day);
  165. }
  166. public function testSecondSetterWithWrap()
  167. {
  168. $d = Carbon::now();
  169. $d->second = 65;
  170. $this->assertSame(5, $d->second);
  171. }
  172. public function testMicrosecondSetterWithWrap()
  173. {
  174. $d = Carbon::now();
  175. $d->micro = -4;
  176. $this->assertSame(999996, $d->micro);
  177. $this->assertSame((Carbon::now()->second + 59) % 60, $d->second);
  178. $d->microsecond = 3123456;
  179. $this->assertSame(123456, $d->micro);
  180. $this->assertSame((Carbon::now()->second + 2) % 60, $d->second);
  181. $d->micro -= 12123400;
  182. $this->assertSame(56, $d->micro);
  183. $this->assertSame((Carbon::now()->second + 50) % 60, $d->second);
  184. $d->micro = -12600000;
  185. $this->assertSame(400000, $d->micro);
  186. $this->assertSame((Carbon::now()->second + 37) % 60, $d->second);
  187. $d->millisecond = 123;
  188. $this->assertSame(123, $d->milli);
  189. $this->assertSame(123000, $d->micro);
  190. $d->milli = 456;
  191. $this->assertSame(456, $d->millisecond);
  192. $this->assertSame(456000, $d->microsecond);
  193. $d->microseconds(567);
  194. $this->assertSame(567, $d->microsecond);
  195. $d->setMicroseconds(678);
  196. $this->assertSame(678, $d->microsecond);
  197. $d->milliseconds(567);
  198. $this->assertSame(567, $d->millisecond);
  199. $this->assertSame(567000, $d->microsecond);
  200. $d->setMilliseconds(678);
  201. $this->assertSame(678, $d->millisecond);
  202. $this->assertSame(678000, $d->microsecond);
  203. }
  204. public function testTimestampSetter()
  205. {
  206. $d = Carbon::now();
  207. $d->timestamp = 10;
  208. $this->assertSame(10, $d->timestamp);
  209. $d->setTimestamp(11);
  210. $this->assertSame(11, $d->timestamp);
  211. $d->timestamp = 1600887164.88952298;
  212. $this->assertSame('2020-09-23 14:52:44.889523', $d->format('Y-m-d H:i:s.u'));
  213. $d->setTimestamp(1599828571.23561248);
  214. $this->assertSame('2020-09-11 08:49:31.235612', $d->format('Y-m-d H:i:s.u'));
  215. $d->timestamp = '0.88951247 1600887164';
  216. $this->assertSame('2020-09-23 14:52:44.889512', $d->format('Y-m-d H:i:s.u'));
  217. $d->setTimestamp('0.23561248 1599828571');
  218. $this->assertSame('2020-09-11 08:49:31.235612', $d->format('Y-m-d H:i:s.u'));
  219. $d->timestamp = '0.88951247/1600887164/12.56';
  220. $this->assertSame('2020-09-23 14:52:57.449512', $d->format('Y-m-d H:i:s.u'));
  221. $d->setTimestamp('0.00561248/1599828570--1.23');
  222. $this->assertSame('2020-09-11 08:49:31.235612', $d->format('Y-m-d H:i:s.u'));
  223. }
  224. public function testSetTimezoneWithInvalidTimezone()
  225. {
  226. $this->expectExceptionObject(new InvalidArgumentException(
  227. 'Unknown or bad timezone (sdf)'
  228. ));
  229. $d = Carbon::now();
  230. $d->setTimezone('sdf');
  231. }
  232. public function testTimezoneWithInvalidTimezone()
  233. {
  234. $this->expectExceptionObject(new InvalidArgumentException(
  235. 'Unknown or bad timezone (sdf)'
  236. ));
  237. /** @var mixed $d */
  238. $d = Carbon::now();
  239. $d->timezone = 'sdf';
  240. }
  241. public function testTimeZoneOfUnserialized()
  242. {
  243. $date = new Carbon('2020-01-01', 'America/Vancouver');
  244. $new = unserialize(serialize($date));
  245. $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
  246. $date->cleanupDumpProperties()->timezone = 'UTC';
  247. $this->assertSame('UTC', $date->getTimezone()->getName());
  248. $this->assertSame('America/Vancouver', $new->getTimezone()->getName());
  249. @$new->timezone = 'UTC';
  250. $this->assertSame('UTC', $new->getTimezone()->getName());
  251. /** @var mixed $date */
  252. $date = new Carbon('2020-01-01', 'America/Vancouver');
  253. $new = clone $date;
  254. $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
  255. @$date->timezone = 'UTC';
  256. $this->assertSame('UTC', $date->getTimezone()->getName());
  257. $this->assertSame('America/Vancouver', $new->getTimezone()->getName());
  258. @$new->timezone = 'UTC';
  259. $this->assertSame('UTC', $new->getTimezone()->getName());
  260. $date = new Carbon('2020-01-01', 'America/Vancouver');
  261. $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
  262. var_export($date, true);
  263. $date->cleanupDumpProperties()->timezone = 'UTC';
  264. $this->assertSame('UTC', $date->getTimezone()->getName());
  265. $date = new Carbon('2020-01-01', 'America/Vancouver');
  266. $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
  267. /** @var array $array */
  268. $array = $date;
  269. foreach ($array as $item) {
  270. }
  271. $date->cleanupDumpProperties()->timezone = 'UTC';
  272. $this->assertSame('UTC', $date->getTimezone()->getName());
  273. $date = new Carbon('2020-01-01', 'America/Vancouver');
  274. $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
  275. get_object_vars($date);
  276. $date->cleanupDumpProperties()->timezone = 'UTC';
  277. $this->assertSame('UTC', $date->getTimezone()->getName());
  278. }
  279. public function testTimezoneWithInvalidTimezoneSetter()
  280. {
  281. $this->expectExceptionObject(new InvalidArgumentException(
  282. 'Unknown or bad timezone (sdf)'
  283. ));
  284. $d = Carbon::now();
  285. $d->timezone('sdf');
  286. }
  287. public function testTzWithInvalidTimezone()
  288. {
  289. $this->expectExceptionObject(new InvalidArgumentException(
  290. 'Unknown or bad timezone (sdf)'
  291. ));
  292. /** @var mixed $d */
  293. $d = Carbon::now();
  294. $d->tz = 'sdf';
  295. }
  296. public function testTzWithInvalidTimezoneSetter()
  297. {
  298. $this->expectExceptionObject(new InvalidArgumentException(
  299. 'Unknown or bad timezone (sdf)'
  300. ));
  301. $d = Carbon::now();
  302. $d->tz('sdf');
  303. }
  304. public function testSetTimezoneUsingString()
  305. {
  306. $d = Carbon::now();
  307. $d->setTimezone('America/Toronto');
  308. $this->assertSame('America/Toronto', $d->tzName);
  309. }
  310. public function testShiftTimezone()
  311. {
  312. $d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
  313. $d2 = $d->copy()->setTimezone('America/Toronto');
  314. $this->assertSame(0, $d2->getTimestamp() - $d->getTimestamp());
  315. $this->assertSame('04:53:12', $d2->format('H:i:s'));
  316. $d = Carbon::parse('2018-08-13 10:53:12.321654', 'Europe/Paris');
  317. $d2 = $d->copy()->shiftTimezone('America/Toronto');
  318. $this->assertSame(21600, $d2->getTimestamp() - $d->getTimestamp());
  319. $this->assertSame('America/Toronto', $d2->tzName);
  320. $this->assertSame('10:53:12.321654', $d2->format('H:i:s.u'));
  321. $d = Carbon::parse('2018-03-25 00:53:12.321654 America/Toronto')->shiftTimezone('Europe/Oslo');
  322. $this->assertSame('2018-03-25 00:53:12.321654 Europe/Oslo', $d->format('Y-m-d H:i:s.u e'));
  323. }
  324. public function testTimezoneUsingString()
  325. {
  326. /** @var mixed $d */
  327. $d = Carbon::now();
  328. $d->timezone = 'America/Toronto';
  329. $this->assertSame('America/Toronto', $d->tzName);
  330. $d->timezone('America/Vancouver');
  331. $this->assertSame('America/Vancouver', $d->tzName);
  332. }
  333. public function testTzUsingString()
  334. {
  335. /** @var mixed $d */
  336. $d = Carbon::now();
  337. $d->tz = 'America/Toronto';
  338. $this->assertSame('America/Toronto', $d->tzName);
  339. $this->assertSame('America/Toronto', $d->tz());
  340. $d->tz('America/Vancouver');
  341. $this->assertSame('America/Vancouver', $d->tzName);
  342. $this->assertSame('America/Vancouver', $d->tz());
  343. }
  344. public function testTzUsingOffset()
  345. {
  346. $d = Carbon::create(2000, 8, 1, 0, 0, 0);
  347. $d->offset = 7200;
  348. $this->assertSame(7200, $d->offset);
  349. $this->assertSame(120, $d->offsetMinutes);
  350. $this->assertSame(2, $d->offsetHours);
  351. $this->assertSame(120, $d->utcOffset());
  352. $d->utcOffset(-180);
  353. $this->assertSame(-10800, $d->offset);
  354. $this->assertSame(-180, $d->offsetMinutes);
  355. $this->assertSame(-3, $d->offsetHours);
  356. $this->assertSame(-180, $d->utcOffset());
  357. $d->offsetMinutes = -240;
  358. $this->assertSame(-14400, $d->offset);
  359. $this->assertSame(-240, $d->offsetMinutes);
  360. $this->assertSame(-4, $d->offsetHours);
  361. $this->assertSame(-240, $d->utcOffset());
  362. $d->offsetHours = 1;
  363. $this->assertSame(3600, $d->offset);
  364. $this->assertSame(60, $d->offsetMinutes);
  365. $this->assertSame(1, $d->offsetHours);
  366. $this->assertSame(60, $d->utcOffset());
  367. $d->utcOffset(330);
  368. $this->assertSame(330, $d->utcOffset());
  369. }
  370. public function testSetTimezoneUsingDateTimeZone()
  371. {
  372. $d = Carbon::now();
  373. $d->setTimezone(new DateTimeZone('America/Toronto'));
  374. $this->assertSame('America/Toronto', $d->tzName);
  375. }
  376. public function testTimezoneUsingDateTimeZone()
  377. {
  378. /** @var mixed $d */
  379. $d = Carbon::now();
  380. $d->timezone = new DateTimeZone('America/Toronto');
  381. $this->assertSame('America/Toronto', $d->tzName);
  382. $d->timezone(new DateTimeZone('America/Vancouver'));
  383. $this->assertSame('America/Vancouver', $d->tzName);
  384. }
  385. public function testTzUsingDateTimeZone()
  386. {
  387. /** @var mixed $d */
  388. $d = Carbon::now();
  389. $d->tz = new DateTimeZone('America/Toronto');
  390. $this->assertSame('America/Toronto', $d->tzName);
  391. $d->tz(new DateTimeZone('America/Vancouver'));
  392. $this->assertSame('America/Vancouver', $d->tzName);
  393. }
  394. public function testInvalidSetter()
  395. {
  396. $this->expectExceptionObject(new InvalidArgumentException(
  397. "Unknown setter 'doesNotExit'"
  398. ));
  399. /** @var mixed $date */
  400. $date = Carbon::now();
  401. $date->doesNotExit = 'bb';
  402. }
  403. /**
  404. * @dataProvider \Tests\Carbon\SettersTest::dataForTestSetTimeFromTimeString
  405. *
  406. * @param int $hour
  407. * @param int $minute
  408. * @param int $second
  409. * @param string $time
  410. */
  411. public function testSetTimeFromTimeString($hour, $minute, $second, $time)
  412. {
  413. Carbon::setTestNow(Carbon::create(2016, 2, 12, 1, 2, 3));
  414. $d = Carbon::now()->setTimeFromTimeString($time);
  415. $this->assertCarbon($d, 2016, 2, 12, $hour, $minute, $second);
  416. }
  417. public static function dataForTestSetTimeFromTimeString(): Generator
  418. {
  419. yield [9, 15, 30, '09:15:30'];
  420. yield [9, 15, 0, '09:15'];
  421. yield [9, 0, 0, '09'];
  422. yield [9, 5, 3, '9:5:3'];
  423. yield [9, 5, 0, '9:5'];
  424. yield [9, 0, 0, '9'];
  425. }
  426. public function testWeekendDaysSetter()
  427. {
  428. $weekendDays = [Carbon::FRIDAY,Carbon::SATURDAY];
  429. $d = Carbon::now();
  430. $d->setWeekendDays($weekendDays);
  431. $this->assertSame($weekendDays, $d->getWeekendDays());
  432. Carbon::setWeekendDays([Carbon::SATURDAY, Carbon::SUNDAY]);
  433. }
  434. public function testMidDayAtSetter()
  435. {
  436. $d = Carbon::now();
  437. $d->setMidDayAt(11);
  438. $this->assertSame(11, $d->getMidDayAt());
  439. $d->setMidDayAt(12);
  440. $this->assertSame(12, $d->getMidDayAt());
  441. }
  442. public function testSetUnitNoOverflow()
  443. {
  444. $results = [
  445. 'current' => 0,
  446. 'start' => 0,
  447. 'end' => 0,
  448. 'failure' => 0,
  449. ];
  450. for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
  451. $year = mt_rand(2000, 2500);
  452. $month = mt_rand(1, 12);
  453. $day = mt_rand(1, 28);
  454. $hour = mt_rand(0, 23);
  455. $minute = mt_rand(0, 59);
  456. $second = mt_rand(0, 59);
  457. $microsecond = mt_rand(0, 999999);
  458. $units = ['millennium', 'century', 'decade', 'year', 'quarter', 'month', 'day', 'hour', 'minute', 'second', 'week'];
  459. $overflowUnit = $units[mt_rand(0, \count($units) - 1)];
  460. $units = [
  461. 'year' => 10,
  462. 'month' => 12,
  463. 'day' => 9999,
  464. 'hour' => 24,
  465. 'minute' => 60,
  466. 'second' => 60,
  467. 'microsecond' => 1000000,
  468. ];
  469. $valueUnit = array_keys($units)[mt_rand(0, \count($units) - 1)];
  470. $value = mt_rand() > 0.5 ?
  471. mt_rand(-9999, 9999) :
  472. mt_rand(-60, 60);
  473. $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000);
  474. $original = $date->copy();
  475. $date->setUnitNoOverflow($valueUnit, $value, $overflowUnit);
  476. $start = $original->copy()->startOf($overflowUnit);
  477. $end = $original->copy()->endOf($overflowUnit);
  478. if ($date < $start || $date > $end) {
  479. $results['failure']++;
  480. continue;
  481. }
  482. $unit = ucfirst(Carbon::pluralUnit($valueUnit));
  483. $modulo = $value % $units[$valueUnit];
  484. if ($modulo < 0) {
  485. $modulo += $units[$valueUnit];
  486. }
  487. if ($value === $date->$valueUnit ||
  488. $modulo === $date->$valueUnit ||
  489. (method_exists($date, "diffInReal$unit") && $$valueUnit - $date->{"diffInReal$unit"}($original, false) === $value) ||
  490. $$valueUnit - $date->{"diffIn$unit"}($original, false) === $value
  491. ) {
  492. $results['current']++;
  493. continue;
  494. }
  495. if ($date->$valueUnit === $start->$valueUnit) {
  496. $results['start']++;
  497. continue;
  498. }
  499. if ($date->$valueUnit === $end->$valueUnit) {
  500. $results['end']++;
  501. continue;
  502. }
  503. $this->failOperation(
  504. $original,
  505. $date,
  506. $start,
  507. $end,
  508. 'setUnitNoOverflow',
  509. $valueUnit,
  510. $value,
  511. $overflowUnit,
  512. $unit,
  513. $modulo,
  514. $$valueUnit
  515. );
  516. }
  517. $minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
  518. $this->assertSame(0, $results['failure']);
  519. $this->assertGreaterThan($minimum, $results['start']);
  520. $this->assertGreaterThan($minimum, $results['end']);
  521. $this->assertGreaterThan($minimum, $results['current']);
  522. $this->assertSame(static::SET_UNIT_NO_OVERFLOW_SAMPLE, $results['end'] + $results['start'] + $results['current']);
  523. }
  524. public function testSetUnitNoOverflowInputUnitException()
  525. {
  526. $this->expectExceptionObject(new InvalidArgumentException(
  527. 'Unknown unit \'anyUnit\''
  528. ));
  529. Carbon::now()->setUnitNoOverflow('anyUnit', 1, 'year');
  530. }
  531. public function testSetUnitNoOverflowOverflowUnitException()
  532. {
  533. $this->expectExceptionObject(new InvalidArgumentException(
  534. 'Unknown unit \'anyUnit\''
  535. ));
  536. Carbon::now()->setUnitNoOverflow('minute', 1, 'anyUnit');
  537. }
  538. public function testAddUnitError()
  539. {
  540. $this->expectExceptionObject(new UnitException(implode("\n", [
  541. 'Unable to add unit array (',
  542. " 0 => 'foobar',",
  543. ' 1 => 1,',
  544. ')',
  545. ])));
  546. $date = Carbon::parse('2021-09-13');
  547. @$date->addUnit('foobar', 1);
  548. }
  549. public function testAddUnitNoOverflow()
  550. {
  551. $results = [
  552. 'current' => 0,
  553. 'start' => 0,
  554. 'end' => 0,
  555. 'failure' => 0,
  556. ];
  557. for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
  558. $year = mt_rand(2000, 2500);
  559. $month = mt_rand(1, 12);
  560. $day = mt_rand(1, 28);
  561. $hour = mt_rand(0, 23);
  562. $minute = mt_rand(0, 59);
  563. $second = mt_rand(0, 59);
  564. $microsecond = mt_rand(0, 999999);
  565. $units = ['millennium', 'century', 'decade', 'year', 'quarter', 'month', 'day', 'hour', 'minute', 'second', 'week'];
  566. $overflowUnit = $units[mt_rand(0, \count($units) - 1)];
  567. $units = [
  568. 'year' => 10,
  569. 'month' => 12,
  570. 'day' => 9999,
  571. 'hour' => 24,
  572. 'minute' => 60,
  573. 'second' => 60,
  574. 'microsecond' => 1000000,
  575. ];
  576. $valueUnit = array_keys($units)[mt_rand(0, \count($units) - 1)];
  577. $value = mt_rand() > 0.5 ?
  578. mt_rand(-9999, 9999) :
  579. mt_rand(-60, 60);
  580. $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000);
  581. $original = $date->copy();
  582. $date->addUnitNoOverflow($valueUnit, $value, $overflowUnit);
  583. $start = $original->copy()->startOf($overflowUnit);
  584. $end = $original->copy()->endOf($overflowUnit);
  585. if ($date < $start || $date > $end) {
  586. $results['failure']++;
  587. continue;
  588. }
  589. $unit = ucfirst(Carbon::pluralUnit($valueUnit));
  590. $modulo = ($$valueUnit + $value) % $units[$valueUnit];
  591. if ($modulo < 0) {
  592. $modulo += $units[$valueUnit];
  593. }
  594. if ($value === $date->$valueUnit ||
  595. $modulo === $date->$valueUnit ||
  596. (method_exists($date, "diffInReal$unit") && -$date->{"diffInReal$unit"}($original, false) === $value) ||
  597. -$date->{"diffIn$unit"}($original, false) === $value
  598. ) {
  599. $results['current']++;
  600. continue;
  601. }
  602. if ($date->$valueUnit === $start->$valueUnit) {
  603. $results['start']++;
  604. continue;
  605. }
  606. if ($date->$valueUnit === $end->$valueUnit) {
  607. $results['end']++;
  608. continue;
  609. }
  610. $this->failOperation(
  611. $original,
  612. $date,
  613. $start,
  614. $end,
  615. 'addUnitNoOverflow',
  616. $valueUnit,
  617. $value,
  618. $overflowUnit,
  619. $unit,
  620. $modulo,
  621. $$valueUnit
  622. );
  623. }
  624. $minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
  625. $this->assertSame(0, $results['failure']);
  626. $this->assertGreaterThan($minimum, $results['start']);
  627. $this->assertGreaterThan($minimum, $results['end']);
  628. $this->assertGreaterThan($minimum, $results['current']);
  629. $this->assertSame(static::SET_UNIT_NO_OVERFLOW_SAMPLE, $results['end'] + $results['start'] + $results['current']);
  630. }
  631. public function testSubUnitNoOverflow()
  632. {
  633. $results = [
  634. 'current' => 0,
  635. 'start' => 0,
  636. 'end' => 0,
  637. 'failure' => 0,
  638. ];
  639. for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
  640. $year = mt_rand(2000, 2500);
  641. $month = mt_rand(1, 12);
  642. $day = mt_rand(1, 28);
  643. $hour = mt_rand(0, 23);
  644. $minute = mt_rand(0, 59);
  645. $second = mt_rand(0, 59);
  646. $microsecond = mt_rand(0, 999999);
  647. $units = ['millennium', 'century', 'decade', 'year', 'quarter', 'month', 'day', 'hour', 'minute', 'second', 'week'];
  648. $overflowUnit = $units[mt_rand(0, \count($units) - 1)];
  649. $units = [
  650. 'year' => 10,
  651. 'month' => 12,
  652. 'day' => 9999,
  653. 'hour' => 24,
  654. 'minute' => 60,
  655. 'second' => 60,
  656. 'microsecond' => 1000000,
  657. ];
  658. $valueUnit = array_keys($units)[mt_rand(0, \count($units) - 1)];
  659. $value = mt_rand() > 0.5 ?
  660. mt_rand(-9999, 9999) :
  661. mt_rand(-60, 60);
  662. $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000);
  663. $original = $date->copy();
  664. $date->subUnitNoOverflow($valueUnit, $value, $overflowUnit);
  665. $start = $original->copy()->startOf($overflowUnit);
  666. $end = $original->copy()->endOf($overflowUnit);
  667. if ($date < $start || $date > $end) {
  668. $results['failure']++;
  669. continue;
  670. }
  671. $unit = ucfirst(Carbon::pluralUnit($valueUnit));
  672. $modulo = ($$valueUnit - $value) % $units[$valueUnit];
  673. if ($modulo < 0) {
  674. $modulo += $units[$valueUnit];
  675. }
  676. if ($value === $date->$valueUnit ||
  677. $modulo === $date->$valueUnit ||
  678. (method_exists($date, "diffInReal$unit") && $value === $date->{"diffInReal$unit"}($original, false)) ||
  679. $value === $date->{"diffIn$unit"}($original, false)
  680. ) {
  681. $results['current']++;
  682. continue;
  683. }
  684. if ($date->$valueUnit === $start->$valueUnit) {
  685. $results['start']++;
  686. continue;
  687. }
  688. if ($date->$valueUnit === $end->$valueUnit) {
  689. $results['end']++;
  690. continue;
  691. }
  692. $this->failOperation(
  693. $original,
  694. $date,
  695. $start,
  696. $end,
  697. 'subUnitNoOverflow',
  698. $valueUnit,
  699. $value,
  700. $overflowUnit,
  701. $unit,
  702. $modulo,
  703. $$valueUnit
  704. );
  705. }
  706. $minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
  707. $this->assertSame(0, $results['failure']);
  708. $this->assertGreaterThan($minimum, $results['start']);
  709. $this->assertGreaterThan($minimum, $results['end']);
  710. $this->assertGreaterThan($minimum, $results['current']);
  711. $this->assertSame(static::SET_UNIT_NO_OVERFLOW_SAMPLE, $results['end'] + $results['start'] + $results['current']);
  712. }
  713. /**
  714. * @SuppressWarnings(PHPMD.TooManyFields)
  715. */
  716. private function failOperation(
  717. Carbon $original,
  718. Carbon $date,
  719. Carbon $start,
  720. Carbon $end,
  721. string $method,
  722. string $valueUnit,
  723. int $value,
  724. string $overflowUnit,
  725. string $unit,
  726. int $modulo,
  727. int $variableValue
  728. ): void {
  729. throw new Exception(implode("\n", [
  730. 'Unhandled result for: '.
  731. 'Carbon::parse('.var_export($original->format('Y-m-d H:i:s.u'), true).', '.
  732. var_export($original->timezoneName, true).
  733. ")->$method(".implode(', ', array_map(function ($value) {
  734. return var_export($value, true);
  735. }, [$valueUnit, $value, $overflowUnit])).');',
  736. 'Getting: '.$date->format('Y-m-d H:i:s.u e'),
  737. "Current $valueUnit: ".$date->$valueUnit,
  738. 'Is neither '.$start->$valueUnit." (from $start)",
  739. 'Nor '.$end->$valueUnit." (from $end)",
  740. "Nor $value (from value)",
  741. "Nor $modulo (from modulo)",
  742. method_exists($date, "diffInReal$unit")
  743. ? "diffInReal$unit() exists and returns ".$date->{"diffInReal$unit"}($original, false)
  744. ." while expecting $variableValue"
  745. : "diffInReal$unit() does not exist",
  746. "diffIn$unit() exists and returns ".$date->{"diffIn$unit"}($original, false)
  747. ." while expecting $variableValue",
  748. ]));
  749. }
  750. }