Skip to content

Commit

Permalink
Fix micro-second approximation test
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 8, 2024
1 parent 37a7dd4 commit 66fa8f3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
6 changes: 3 additions & 3 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 = 20_000;
public const SET_UNIT_NO_OVERFLOW_SAMPLE = 200;

public function testMonthEnum()
{
Expand Down Expand Up @@ -1051,7 +1051,7 @@ public function testSubUnitNoOverflow()
$modulo,
$value,
$hours ?? null,
$delta ?? null,
$delta,
);
}

Expand Down Expand Up @@ -1125,7 +1125,7 @@ private function failOperation(
"Nor $value (from value)",
"Nor $modulo (from modulo)",
...($hours !== null ? [
"Not matching diff (hours = $hours vs delta = " . ($delta ?? 'null') . ')',
"Not matching diff (hours = $hours vs delta = ".($delta ?? 'null').')',
] : []),
method_exists($date, "diffInReal$unit")
? "diffInReal$unit() exists and returns ".$date->{"diffInReal$unit"}($original, false)
Expand Down
46 changes: 42 additions & 4 deletions tests/CarbonInterval/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public function testFromSerialization()
$today = new Carbon('today');
$interval = $today->diffAsCarbonInterval($past);
/** @var CarbonInterval $copy */
$copy = unserialize(serialize($interval));
$copy = unserialize(serialize($interval));

$this->assertInstanceOf(CarbonInterval::class, $copy);

Expand All @@ -454,7 +454,44 @@ public function testFromSerialization()

$interval = $today->locale('ja')->diffAsCarbonInterval($past);
/** @var CarbonInterval $copy */
$copy = unserialize(serialize($interval));
$copy = unserialize(serialize($interval));

$this->assertInstanceOf(CarbonInterval::class, $copy);

$this->assertSame('二日', $interval->forHumans(['altNumbers' => true, 'parts' => 1]));
$this->assertSame('二日', $copy->forHumans(['altNumbers' => true, 'parts' => 1]));

$this->assertSame(['ja'], array_keys($interval->getLocalTranslator()->getMessages()));
$this->assertSame(['ja'], array_keys($copy->getLocalTranslator()->getMessages()));

$this->assertSameIntervals($interval, $copy, 1);
}

public function testFromSerializationConst()
{
$past = new Carbon('2024-01-01 00:00:00');
$today = new Carbon('2024-01-03 06:39:47.065034');
$interval = $today->diffAsCarbonInterval($past);
/** @var CarbonInterval $copy */
$copy = unserialize(serialize($interval));

$this->assertInstanceOf(CarbonInterval::class, $copy);

$this->assertSame('2 days', $interval->forHumans(parts: 1));
$this->assertSame('2 days', $copy->forHumans(parts: 1));

$this->assertSame(['en'], array_keys($interval->getLocalTranslator()->getMessages()) ?: ['en']);
$this->assertSame(['en'], array_keys($copy->getLocalTranslator()->getMessages()) ?: ['en']);
$this->assertSame($interval->locale, $copy->locale);

// Ignore translator for the English comparison
$copy->setLocalTranslator($interval->getLocalTranslator());

$this->assertSameIntervals($interval, $copy, 1);

$interval = $today->locale('ja')->diffAsCarbonInterval($past);
/** @var CarbonInterval $copy */
$copy = unserialize(serialize($interval));

$this->assertInstanceOf(CarbonInterval::class, $copy);

Expand Down Expand Up @@ -483,9 +520,10 @@ private function assertSameIntervals(CarbonInterval $expected, CarbonInterval $a
$expected->microseconds !== $actual->microseconds
&& $microsecondApproximation > 0
&& $actual->microseconds >= $expected->microseconds - $microsecondApproximation
&& $actual->microseconds <= $expected->microseconds - $microsecondApproximation
&& $actual->microseconds <= $expected->microseconds + $microsecondApproximation
) {
$actual->microseconds = $expected->microseconds;
$actual->f = 0.0;
$expected->f = 0.0;
}

if (PHP_VERSION >= 8.2) {
Expand Down

0 comments on commit 66fa8f3

Please sign in to comment.