Skip to content

Commit

Permalink
Test overflow with more iterations and debug infos
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 5, 2024
1 parent 50c0e29 commit dfe7a32
Showing 1 changed file with 60 additions and 10 deletions.
70 changes: 60 additions & 10 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class SettersTest extends AbstractTestCase
{
public const SET_UNIT_NO_OVERFLOW_SAMPLE = 200;
public const SET_UNIT_NO_OVERFLOW_SAMPLE = 20_000;

public function testMonthEnum()
{
Expand Down Expand Up @@ -629,13 +629,21 @@ public function testMidDayAtSetter()
$this->assertSame(12, $d->getMidDayAt());
}

public function testOverflowOnCeil()
{
$date = Carbon::create(2290, 11, 2, 1, 10, 10 + 888480 / 1000000, 'UTC');
$date->setUnitNoOverflow('minute', 9943, 'hour');

$this->assertSame('2290-11-02 01:59:59.999999 UTC', $date->format('Y-m-d H:i:s.u e'));
}

public function testSetUnitNoOverflow()
{
$results = [
'current' => 0,
'start' => 0,
'end' => 0,
'failure' => 0,
'failure' => [],
];

for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
Expand Down Expand Up @@ -669,7 +677,21 @@ public function testSetUnitNoOverflow()
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
$results['failure']++;
$results['failure'][] = [
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $second,
'microsecond' => $microsecond,
'valueUnit' => $valueUnit,
'value' => $value,
'overflowUnit' => $overflowUnit,
'date' => $date->format('Y-m-d H:i:s'),
'start' => $start->format('Y-m-d H:i:s'),
'end' => $end->format('Y-m-d H:i:s'),
];

continue;
}
Expand Down Expand Up @@ -722,7 +744,7 @@ public function testSetUnitNoOverflow()
}

$minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
$this->assertSame(0, $results['failure']);
$this->assertSame([], $results['failure']);
$this->assertGreaterThan($minimum, $results['start']);
$this->assertGreaterThan($minimum, $results['end']);
$this->assertGreaterThan($minimum, $results['current']);
Expand Down Expand Up @@ -766,7 +788,7 @@ public function testAddUnitNoOverflow()
'current' => 0,
'start' => 0,
'end' => 0,
'failure' => 0,
'failure' => [],
];

for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
Expand Down Expand Up @@ -800,7 +822,21 @@ public function testAddUnitNoOverflow()
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
$results['failure']++;
$results['failure'][] = [
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $second,
'microsecond' => $microsecond,
'valueUnit' => $valueUnit,
'value' => $value,
'overflowUnit' => $overflowUnit,
'date' => $date->format('Y-m-d H:i:s.u e'),
'start' => $start->format('Y-m-d H:i:s.u e'),
'end' => $end->format('Y-m-d H:i:s.u e'),
];

continue;
}
Expand Down Expand Up @@ -848,7 +884,7 @@ public function testAddUnitNoOverflow()
}

$minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
$this->assertSame(0, $results['failure']);
$this->assertSame([], $results['failure']);
$this->assertGreaterThan($minimum, $results['start']);
$this->assertGreaterThan($minimum, $results['end']);
$this->assertGreaterThan($minimum, $results['current']);
Expand All @@ -861,7 +897,7 @@ public function testSubUnitNoOverflow()
'current' => 0,
'start' => 0,
'end' => 0,
'failure' => 0,
'failure' => [],
];

for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
Expand Down Expand Up @@ -895,7 +931,21 @@ public function testSubUnitNoOverflow()
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
$results['failure']++;
$results['failure'][] = [
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $second,
'microsecond' => $microsecond,
'valueUnit' => $valueUnit,
'value' => $value,
'overflowUnit' => $overflowUnit,
'date' => $date->format('Y-m-d H:i:s.u e'),
'start' => $start->format('Y-m-d H:i:s.u e'),
'end' => $end->format('Y-m-d H:i:s.u e'),
];

continue;
}
Expand Down Expand Up @@ -960,7 +1010,7 @@ public function testSubUnitNoOverflow()
}

$minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
$this->assertSame(0, $results['failure']);
$this->assertSame([], $results['failure']);
$this->assertGreaterThan($minimum, $results['start']);
$this->assertGreaterThan($minimum, $results['end']);
$this->assertGreaterThan($minimum, $results['current']);
Expand Down

0 comments on commit dfe7a32

Please sign in to comment.