123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of the Carbon package.
- *
- * (c) Brian Nesbitt <brian@nesbot.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Tests\Carbon;
- use Carbon\Carbon;
- use Carbon\Exceptions\UnitException;
- use DateTimeZone;
- use Exception;
- use Generator;
- use InvalidArgumentException;
- use Tests\AbstractTestCase;
- class SettersTest extends AbstractTestCase
- {
- public const SET_UNIT_NO_OVERFLOW_SAMPLE = 200;
- public function testSingularUnit()
- {
- $this->assertSame('year', Carbon::singularUnit('year'));
- $this->assertSame('year', Carbon::singularUnit('Years'));
- $this->assertSame('century', Carbon::singularUnit('centuries'));
- $this->assertSame('millennium', Carbon::singularUnit('Millennia'));
- $this->assertSame('millennium', Carbon::singularUnit('millenniums'));
- }
- public function testPluralUnit()
- {
- $this->assertSame('years', Carbon::pluralUnit('year'));
- $this->assertSame('years', Carbon::pluralUnit('Years'));
- $this->assertSame('centuries', Carbon::pluralUnit('century'));
- $this->assertSame('centuries', Carbon::pluralUnit('centuries'));
- $this->assertSame('millennia', Carbon::pluralUnit('Millennia'));
- $this->assertSame('millennia', Carbon::pluralUnit('millenniums'));
- $this->assertSame('millennia', Carbon::pluralUnit('millennium'));
- }
- public function testSet()
- {
- $d = Carbon::create(2000, 1, 12);
- $d->set([
- 'year' => 1995,
- 'month' => 4,
- ]);
- $this->assertSame(1995, $d->year);
- $this->assertSame(4, $d->month);
- $this->assertSame(12, $d->day);
- }
- public function testYearSetter()
- {
- $d = Carbon::now();
- $d->year = 1995;
- $this->assertSame(1995, $d->year);
- }
- public function testMonthSetter()
- {
- $d = Carbon::now();
- $d->month = 3;
- $this->assertSame(3, $d->month);
- }
- public function testMonthSetterWithWrap()
- {
- $d = Carbon::now();
- $d->month = 13;
- $this->assertSame(1, $d->month);
- }
- public function testDaySetter()
- {
- $d = Carbon::now();
- $d->day = 2;
- $this->assertSame(2, $d->day);
- }
- public function testDaySetterWithWrap()
- {
- $d = Carbon::createFromDate(2012, 8, 5);
- $d->day = 32;
- $this->assertSame(1, $d->day);
- }
- public function testHourSetter()
- {
- $d = Carbon::now();
- $d->hour = 2;
- $this->assertSame(2, $d->hour);
- }
- public function testHourSetterWithWrap()
- {
- $d = Carbon::now();
- $d->hour = 25;
- $this->assertSame(1, $d->hour);
- }
- public function testMinuteSetter()
- {
- $d = Carbon::now();
- $d->minute = 2;
- $this->assertSame(2, $d->minute);
- }
- public function testMinuteSetterWithWrap()
- {
- $d = Carbon::now();
- $d->minute = 65;
- $this->assertSame(5, $d->minute);
- }
- public function testSecondSetter()
- {
- $d = Carbon::now();
- $d->second = 2;
- $this->assertSame(2, $d->second);
- }
- public function testTimeSetter()
- {
- $d = Carbon::now();
- $d->setTime(1, 1, 1);
- $this->assertSame(1, $d->second);
- $d->setTime(1, 1);
- $this->assertSame(0, $d->second);
- }
- public function testTimeSetterWithChaining()
- {
- $d = Carbon::now();
- $d->setTime(2, 2, 2)->setTime(1, 1, 1);
- $this->assertInstanceOfCarbon($d);
- $this->assertSame(1, $d->second);
- $d->setTime(2, 2, 2)->setTime(1, 1);
- $this->assertInstanceOfCarbon($d);
- $this->assertSame(0, $d->second);
- }
- public function testTimeSetterWithZero()
- {
- $d = Carbon::now();
- $d->setTime(1, 1);
- $this->assertSame(0, $d->second);
- }
- public function testDateTimeSetter()
- {
- $d = Carbon::now();
- $d->setDateTime($d->year, $d->month, $d->day, 1, 1, 1);
- $this->assertSame(1, $d->second);
- }
- public function testDateTimeSetterWithZero()
- {
- $d = Carbon::now();
- $d->setDateTime($d->year, $d->month, $d->day, 1, 1);
- $this->assertSame(0, $d->second);
- }
- public function testDateTimeSetterWithChaining()
- {
- $d = Carbon::now();
- $d->setDateTime(2013, 9, 24, 17, 4, 29);
- $this->assertInstanceOfCarbon($d);
- $d->setDateTime(2014, 10, 25, 18, 5, 30);
- $this->assertInstanceOfCarbon($d);
- $this->assertCarbon($d, 2014, 10, 25, 18, 5, 30);
- }
- /**
- * @link https://github.com/briannesbitt/Carbon/issues/539
- */
- public function testSetDateAfterStringCreation()
- {
- $d = new Carbon('first day of this month');
- $this->assertSame(1, $d->day);
- $d->setDate($d->year, $d->month, 12);
- $this->assertSame(12, $d->day);
- }
- public function testSecondSetterWithWrap()
- {
- $d = Carbon::now();
- $d->second = 65;
- $this->assertSame(5, $d->second);
- }
- public function testMicrosecondSetterWithWrap()
- {
- $d = Carbon::now();
- $d->micro = -4;
- $this->assertSame(999996, $d->micro);
- $this->assertSame((Carbon::now()->second + 59) % 60, $d->second);
- $d->microsecond = 3123456;
- $this->assertSame(123456, $d->micro);
- $this->assertSame((Carbon::now()->second + 2) % 60, $d->second);
- $d->micro -= 12123400;
- $this->assertSame(56, $d->micro);
- $this->assertSame((Carbon::now()->second + 50) % 60, $d->second);
- $d->micro = -12600000;
- $this->assertSame(400000, $d->micro);
- $this->assertSame((Carbon::now()->second + 37) % 60, $d->second);
- $d->millisecond = 123;
- $this->assertSame(123, $d->milli);
- $this->assertSame(123000, $d->micro);
- $d->milli = 456;
- $this->assertSame(456, $d->millisecond);
- $this->assertSame(456000, $d->microsecond);
- $d->microseconds(567);
- $this->assertSame(567, $d->microsecond);
- $d->setMicroseconds(678);
- $this->assertSame(678, $d->microsecond);
- $d->milliseconds(567);
- $this->assertSame(567, $d->millisecond);
- $this->assertSame(567000, $d->microsecond);
- $d->setMilliseconds(678);
- $this->assertSame(678, $d->millisecond);
- $this->assertSame(678000, $d->microsecond);
- }
- public function testTimestampSetter()
- {
- $d = Carbon::now();
- $d->timestamp = 10;
- $this->assertSame(10, $d->timestamp);
- $d->setTimestamp(11);
- $this->assertSame(11, $d->timestamp);
- $d->timestamp = 1600887164.88952298;
- $this->assertSame('2020-09-23 14:52:44.889523', $d->format('Y-m-d H:i:s.u'));
- $d->setTimestamp(1599828571.23561248);
- $this->assertSame('2020-09-11 08:49:31.235612', $d->format('Y-m-d H:i:s.u'));
- $d->timestamp = '0.88951247 1600887164';
- $this->assertSame('2020-09-23 14:52:44.889512', $d->format('Y-m-d H:i:s.u'));
- $d->setTimestamp('0.23561248 1599828571');
- $this->assertSame('2020-09-11 08:49:31.235612', $d->format('Y-m-d H:i:s.u'));
- $d->timestamp = '0.88951247/1600887164/12.56';
- $this->assertSame('2020-09-23 14:52:57.449512', $d->format('Y-m-d H:i:s.u'));
- $d->setTimestamp('0.00561248/1599828570--1.23');
- $this->assertSame('2020-09-11 08:49:31.235612', $d->format('Y-m-d H:i:s.u'));
- }
- public function testSetTimezoneWithInvalidTimezone()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Unknown or bad timezone (sdf)'
- ));
- $d = Carbon::now();
- $d->setTimezone('sdf');
- }
- public function testTimezoneWithInvalidTimezone()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Unknown or bad timezone (sdf)'
- ));
- /** @var mixed $d */
- $d = Carbon::now();
- $d->timezone = 'sdf';
- }
- public function testTimeZoneOfUnserialized()
- {
- $date = new Carbon('2020-01-01', 'America/Vancouver');
- $new = unserialize(serialize($date));
- $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
- $date->cleanupDumpProperties()->timezone = 'UTC';
- $this->assertSame('UTC', $date->getTimezone()->getName());
- $this->assertSame('America/Vancouver', $new->getTimezone()->getName());
- @$new->timezone = 'UTC';
- $this->assertSame('UTC', $new->getTimezone()->getName());
- /** @var mixed $date */
- $date = new Carbon('2020-01-01', 'America/Vancouver');
- $new = clone $date;
- $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
- @$date->timezone = 'UTC';
- $this->assertSame('UTC', $date->getTimezone()->getName());
- $this->assertSame('America/Vancouver', $new->getTimezone()->getName());
- @$new->timezone = 'UTC';
- $this->assertSame('UTC', $new->getTimezone()->getName());
- $date = new Carbon('2020-01-01', 'America/Vancouver');
- $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
- var_export($date, true);
- $date->cleanupDumpProperties()->timezone = 'UTC';
- $this->assertSame('UTC', $date->getTimezone()->getName());
- $date = new Carbon('2020-01-01', 'America/Vancouver');
- $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
- /** @var array $array */
- $array = $date;
- foreach ($array as $item) {
- }
- $date->cleanupDumpProperties()->timezone = 'UTC';
- $this->assertSame('UTC', $date->getTimezone()->getName());
- $date = new Carbon('2020-01-01', 'America/Vancouver');
- $this->assertSame('America/Vancouver', $date->getTimezone()->getName());
- get_object_vars($date);
- $date->cleanupDumpProperties()->timezone = 'UTC';
- $this->assertSame('UTC', $date->getTimezone()->getName());
- }
- public function testTimezoneWithInvalidTimezoneSetter()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Unknown or bad timezone (sdf)'
- ));
- $d = Carbon::now();
- $d->timezone('sdf');
- }
- public function testTzWithInvalidTimezone()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Unknown or bad timezone (sdf)'
- ));
- /** @var mixed $d */
- $d = Carbon::now();
- $d->tz = 'sdf';
- }
- public function testTzWithInvalidTimezoneSetter()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Unknown or bad timezone (sdf)'
- ));
- $d = Carbon::now();
- $d->tz('sdf');
- }
- public function testSetTimezoneUsingString()
- {
- $d = Carbon::now();
- $d->setTimezone('America/Toronto');
- $this->assertSame('America/Toronto', $d->tzName);
- }
- public function testShiftTimezone()
- {
- $d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
- $d2 = $d->copy()->setTimezone('America/Toronto');
- $this->assertSame(0, $d2->getTimestamp() - $d->getTimestamp());
- $this->assertSame('04:53:12', $d2->format('H:i:s'));
- $d = Carbon::parse('2018-08-13 10:53:12.321654', 'Europe/Paris');
- $d2 = $d->copy()->shiftTimezone('America/Toronto');
- $this->assertSame(21600, $d2->getTimestamp() - $d->getTimestamp());
- $this->assertSame('America/Toronto', $d2->tzName);
- $this->assertSame('10:53:12.321654', $d2->format('H:i:s.u'));
- $d = Carbon::parse('2018-03-25 00:53:12.321654 America/Toronto')->shiftTimezone('Europe/Oslo');
- $this->assertSame('2018-03-25 00:53:12.321654 Europe/Oslo', $d->format('Y-m-d H:i:s.u e'));
- }
- public function testTimezoneUsingString()
- {
- /** @var mixed $d */
- $d = Carbon::now();
- $d->timezone = 'America/Toronto';
- $this->assertSame('America/Toronto', $d->tzName);
- $d->timezone('America/Vancouver');
- $this->assertSame('America/Vancouver', $d->tzName);
- }
- public function testTzUsingString()
- {
- /** @var mixed $d */
- $d = Carbon::now();
- $d->tz = 'America/Toronto';
- $this->assertSame('America/Toronto', $d->tzName);
- $this->assertSame('America/Toronto', $d->tz());
- $d->tz('America/Vancouver');
- $this->assertSame('America/Vancouver', $d->tzName);
- $this->assertSame('America/Vancouver', $d->tz());
- }
- public function testTzUsingOffset()
- {
- $d = Carbon::create(2000, 8, 1, 0, 0, 0);
- $d->offset = 7200;
- $this->assertSame(7200, $d->offset);
- $this->assertSame(120, $d->offsetMinutes);
- $this->assertSame(2, $d->offsetHours);
- $this->assertSame(120, $d->utcOffset());
- $d->utcOffset(-180);
- $this->assertSame(-10800, $d->offset);
- $this->assertSame(-180, $d->offsetMinutes);
- $this->assertSame(-3, $d->offsetHours);
- $this->assertSame(-180, $d->utcOffset());
- $d->offsetMinutes = -240;
- $this->assertSame(-14400, $d->offset);
- $this->assertSame(-240, $d->offsetMinutes);
- $this->assertSame(-4, $d->offsetHours);
- $this->assertSame(-240, $d->utcOffset());
- $d->offsetHours = 1;
- $this->assertSame(3600, $d->offset);
- $this->assertSame(60, $d->offsetMinutes);
- $this->assertSame(1, $d->offsetHours);
- $this->assertSame(60, $d->utcOffset());
- $d->utcOffset(330);
- $this->assertSame(330, $d->utcOffset());
- }
- public function testSetTimezoneUsingDateTimeZone()
- {
- $d = Carbon::now();
- $d->setTimezone(new DateTimeZone('America/Toronto'));
- $this->assertSame('America/Toronto', $d->tzName);
- }
- public function testTimezoneUsingDateTimeZone()
- {
- /** @var mixed $d */
- $d = Carbon::now();
- $d->timezone = new DateTimeZone('America/Toronto');
- $this->assertSame('America/Toronto', $d->tzName);
- $d->timezone(new DateTimeZone('America/Vancouver'));
- $this->assertSame('America/Vancouver', $d->tzName);
- }
- public function testTzUsingDateTimeZone()
- {
- /** @var mixed $d */
- $d = Carbon::now();
- $d->tz = new DateTimeZone('America/Toronto');
- $this->assertSame('America/Toronto', $d->tzName);
- $d->tz(new DateTimeZone('America/Vancouver'));
- $this->assertSame('America/Vancouver', $d->tzName);
- }
- public function testInvalidSetter()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- "Unknown setter 'doesNotExit'"
- ));
- /** @var mixed $date */
- $date = Carbon::now();
- $date->doesNotExit = 'bb';
- }
- /**
- * @dataProvider \Tests\Carbon\SettersTest::dataForTestSetTimeFromTimeString
- *
- * @param int $hour
- * @param int $minute
- * @param int $second
- * @param string $time
- */
- public function testSetTimeFromTimeString($hour, $minute, $second, $time)
- {
- Carbon::setTestNow(Carbon::create(2016, 2, 12, 1, 2, 3));
- $d = Carbon::now()->setTimeFromTimeString($time);
- $this->assertCarbon($d, 2016, 2, 12, $hour, $minute, $second);
- }
- public static function dataForTestSetTimeFromTimeString(): Generator
- {
- yield [9, 15, 30, '09:15:30'];
- yield [9, 15, 0, '09:15'];
- yield [9, 0, 0, '09'];
- yield [9, 5, 3, '9:5:3'];
- yield [9, 5, 0, '9:5'];
- yield [9, 0, 0, '9'];
- }
- public function testWeekendDaysSetter()
- {
- $weekendDays = [Carbon::FRIDAY,Carbon::SATURDAY];
- $d = Carbon::now();
- $d->setWeekendDays($weekendDays);
- $this->assertSame($weekendDays, $d->getWeekendDays());
- Carbon::setWeekendDays([Carbon::SATURDAY, Carbon::SUNDAY]);
- }
- public function testMidDayAtSetter()
- {
- $d = Carbon::now();
- $d->setMidDayAt(11);
- $this->assertSame(11, $d->getMidDayAt());
- $d->setMidDayAt(12);
- $this->assertSame(12, $d->getMidDayAt());
- }
- public function testSetUnitNoOverflow()
- {
- $results = [
- 'current' => 0,
- 'start' => 0,
- 'end' => 0,
- 'failure' => 0,
- ];
- for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
- $year = mt_rand(2000, 2500);
- $month = mt_rand(1, 12);
- $day = mt_rand(1, 28);
- $hour = mt_rand(0, 23);
- $minute = mt_rand(0, 59);
- $second = mt_rand(0, 59);
- $microsecond = mt_rand(0, 999999);
- $units = ['millennium', 'century', 'decade', 'year', 'quarter', 'month', 'day', 'hour', 'minute', 'second', 'week'];
- $overflowUnit = $units[mt_rand(0, \count($units) - 1)];
- $units = [
- 'year' => 10,
- 'month' => 12,
- 'day' => 9999,
- 'hour' => 24,
- 'minute' => 60,
- 'second' => 60,
- 'microsecond' => 1000000,
- ];
- $valueUnit = array_keys($units)[mt_rand(0, \count($units) - 1)];
- $value = mt_rand() > 0.5 ?
- mt_rand(-9999, 9999) :
- mt_rand(-60, 60);
- $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000);
- $original = $date->copy();
- $date->setUnitNoOverflow($valueUnit, $value, $overflowUnit);
- $start = $original->copy()->startOf($overflowUnit);
- $end = $original->copy()->endOf($overflowUnit);
- if ($date < $start || $date > $end) {
- $results['failure']++;
- continue;
- }
- $unit = ucfirst(Carbon::pluralUnit($valueUnit));
- $modulo = $value % $units[$valueUnit];
- if ($modulo < 0) {
- $modulo += $units[$valueUnit];
- }
- if ($value === $date->$valueUnit ||
- $modulo === $date->$valueUnit ||
- (method_exists($date, "diffInReal$unit") && $$valueUnit - $date->{"diffInReal$unit"}($original, false) === $value) ||
- $$valueUnit - $date->{"diffIn$unit"}($original, false) === $value
- ) {
- $results['current']++;
- continue;
- }
- if ($date->$valueUnit === $start->$valueUnit) {
- $results['start']++;
- continue;
- }
- if ($date->$valueUnit === $end->$valueUnit) {
- $results['end']++;
- continue;
- }
- $this->failOperation(
- $original,
- $date,
- $start,
- $end,
- 'setUnitNoOverflow',
- $valueUnit,
- $value,
- $overflowUnit,
- $unit,
- $modulo,
- $$valueUnit
- );
- }
- $minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
- $this->assertSame(0, $results['failure']);
- $this->assertGreaterThan($minimum, $results['start']);
- $this->assertGreaterThan($minimum, $results['end']);
- $this->assertGreaterThan($minimum, $results['current']);
- $this->assertSame(static::SET_UNIT_NO_OVERFLOW_SAMPLE, $results['end'] + $results['start'] + $results['current']);
- }
- public function testSetUnitNoOverflowInputUnitException()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Unknown unit \'anyUnit\''
- ));
- Carbon::now()->setUnitNoOverflow('anyUnit', 1, 'year');
- }
- public function testSetUnitNoOverflowOverflowUnitException()
- {
- $this->expectExceptionObject(new InvalidArgumentException(
- 'Unknown unit \'anyUnit\''
- ));
- Carbon::now()->setUnitNoOverflow('minute', 1, 'anyUnit');
- }
- public function testAddUnitError()
- {
- $this->expectExceptionObject(new UnitException(implode("\n", [
- 'Unable to add unit array (',
- " 0 => 'foobar',",
- ' 1 => 1,',
- ')',
- ])));
- $date = Carbon::parse('2021-09-13');
- @$date->addUnit('foobar', 1);
- }
- public function testAddUnitNoOverflow()
- {
- $results = [
- 'current' => 0,
- 'start' => 0,
- 'end' => 0,
- 'failure' => 0,
- ];
- for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
- $year = mt_rand(2000, 2500);
- $month = mt_rand(1, 12);
- $day = mt_rand(1, 28);
- $hour = mt_rand(0, 23);
- $minute = mt_rand(0, 59);
- $second = mt_rand(0, 59);
- $microsecond = mt_rand(0, 999999);
- $units = ['millennium', 'century', 'decade', 'year', 'quarter', 'month', 'day', 'hour', 'minute', 'second', 'week'];
- $overflowUnit = $units[mt_rand(0, \count($units) - 1)];
- $units = [
- 'year' => 10,
- 'month' => 12,
- 'day' => 9999,
- 'hour' => 24,
- 'minute' => 60,
- 'second' => 60,
- 'microsecond' => 1000000,
- ];
- $valueUnit = array_keys($units)[mt_rand(0, \count($units) - 1)];
- $value = mt_rand() > 0.5 ?
- mt_rand(-9999, 9999) :
- mt_rand(-60, 60);
- $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000);
- $original = $date->copy();
- $date->addUnitNoOverflow($valueUnit, $value, $overflowUnit);
- $start = $original->copy()->startOf($overflowUnit);
- $end = $original->copy()->endOf($overflowUnit);
- if ($date < $start || $date > $end) {
- $results['failure']++;
- continue;
- }
- $unit = ucfirst(Carbon::pluralUnit($valueUnit));
- $modulo = ($$valueUnit + $value) % $units[$valueUnit];
- if ($modulo < 0) {
- $modulo += $units[$valueUnit];
- }
- if ($value === $date->$valueUnit ||
- $modulo === $date->$valueUnit ||
- (method_exists($date, "diffInReal$unit") && -$date->{"diffInReal$unit"}($original, false) === $value) ||
- -$date->{"diffIn$unit"}($original, false) === $value
- ) {
- $results['current']++;
- continue;
- }
- if ($date->$valueUnit === $start->$valueUnit) {
- $results['start']++;
- continue;
- }
- if ($date->$valueUnit === $end->$valueUnit) {
- $results['end']++;
- continue;
- }
- $this->failOperation(
- $original,
- $date,
- $start,
- $end,
- 'addUnitNoOverflow',
- $valueUnit,
- $value,
- $overflowUnit,
- $unit,
- $modulo,
- $$valueUnit
- );
- }
- $minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
- $this->assertSame(0, $results['failure']);
- $this->assertGreaterThan($minimum, $results['start']);
- $this->assertGreaterThan($minimum, $results['end']);
- $this->assertGreaterThan($minimum, $results['current']);
- $this->assertSame(static::SET_UNIT_NO_OVERFLOW_SAMPLE, $results['end'] + $results['start'] + $results['current']);
- }
- public function testSubUnitNoOverflow()
- {
- $results = [
- 'current' => 0,
- 'start' => 0,
- 'end' => 0,
- 'failure' => 0,
- ];
- for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
- $year = mt_rand(2000, 2500);
- $month = mt_rand(1, 12);
- $day = mt_rand(1, 28);
- $hour = mt_rand(0, 23);
- $minute = mt_rand(0, 59);
- $second = mt_rand(0, 59);
- $microsecond = mt_rand(0, 999999);
- $units = ['millennium', 'century', 'decade', 'year', 'quarter', 'month', 'day', 'hour', 'minute', 'second', 'week'];
- $overflowUnit = $units[mt_rand(0, \count($units) - 1)];
- $units = [
- 'year' => 10,
- 'month' => 12,
- 'day' => 9999,
- 'hour' => 24,
- 'minute' => 60,
- 'second' => 60,
- 'microsecond' => 1000000,
- ];
- $valueUnit = array_keys($units)[mt_rand(0, \count($units) - 1)];
- $value = mt_rand() > 0.5 ?
- mt_rand(-9999, 9999) :
- mt_rand(-60, 60);
- $date = Carbon::create($year, $month, $day, $hour, $minute, $second + $microsecond / 1000000);
- $original = $date->copy();
- $date->subUnitNoOverflow($valueUnit, $value, $overflowUnit);
- $start = $original->copy()->startOf($overflowUnit);
- $end = $original->copy()->endOf($overflowUnit);
- if ($date < $start || $date > $end) {
- $results['failure']++;
- continue;
- }
- $unit = ucfirst(Carbon::pluralUnit($valueUnit));
- $modulo = ($$valueUnit - $value) % $units[$valueUnit];
- if ($modulo < 0) {
- $modulo += $units[$valueUnit];
- }
- if ($value === $date->$valueUnit ||
- $modulo === $date->$valueUnit ||
- (method_exists($date, "diffInReal$unit") && $value === $date->{"diffInReal$unit"}($original, false)) ||
- $value === $date->{"diffIn$unit"}($original, false)
- ) {
- $results['current']++;
- continue;
- }
- if ($date->$valueUnit === $start->$valueUnit) {
- $results['start']++;
- continue;
- }
- if ($date->$valueUnit === $end->$valueUnit) {
- $results['end']++;
- continue;
- }
- $this->failOperation(
- $original,
- $date,
- $start,
- $end,
- 'subUnitNoOverflow',
- $valueUnit,
- $value,
- $overflowUnit,
- $unit,
- $modulo,
- $$valueUnit
- );
- }
- $minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
- $this->assertSame(0, $results['failure']);
- $this->assertGreaterThan($minimum, $results['start']);
- $this->assertGreaterThan($minimum, $results['end']);
- $this->assertGreaterThan($minimum, $results['current']);
- $this->assertSame(static::SET_UNIT_NO_OVERFLOW_SAMPLE, $results['end'] + $results['start'] + $results['current']);
- }
- /**
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- private function failOperation(
- Carbon $original,
- Carbon $date,
- Carbon $start,
- Carbon $end,
- string $method,
- string $valueUnit,
- int $value,
- string $overflowUnit,
- string $unit,
- int $modulo,
- int $variableValue
- ): void {
- throw new Exception(implode("\n", [
- 'Unhandled result for: '.
- 'Carbon::parse('.var_export($original->format('Y-m-d H:i:s.u'), true).', '.
- var_export($original->timezoneName, true).
- ")->$method(".implode(', ', array_map(function ($value) {
- return var_export($value, true);
- }, [$valueUnit, $value, $overflowUnit])).');',
- 'Getting: '.$date->format('Y-m-d H:i:s.u e'),
- "Current $valueUnit: ".$date->$valueUnit,
- 'Is neither '.$start->$valueUnit." (from $start)",
- 'Nor '.$end->$valueUnit." (from $end)",
- "Nor $value (from value)",
- "Nor $modulo (from modulo)",
- method_exists($date, "diffInReal$unit")
- ? "diffInReal$unit() exists and returns ".$date->{"diffInReal$unit"}($original, false)
- ." while expecting $variableValue"
- : "diffInReal$unit() does not exist",
- "diffIn$unit() exists and returns ".$date->{"diffIn$unit"}($original, false)
- ." while expecting $variableValue",
- ]));
- }
- }
|