mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
refactor: use assertEqualsWithDelta for float comparisons, add @internal to fake()
Replace Assert::assertEquals with Assert::assertEqualsWithDelta (epsilon 0.0001) in assertSubTotal, assertTotal, and assertTotalQuantity to avoid IEEE 754 floating-point equality pitfalls. Add @internal docblock to fake() to signal it is test-only. Add regression test for consecutive withItems calls.
This commit is contained in:
+12
-6
@@ -52,6 +52,9 @@ public function getDriver(): CartDriver
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal For testing purposes only. Not intended for production use.
|
||||
*/
|
||||
public function fake(): CartFactory
|
||||
{
|
||||
$this->driver = new NullDriver($this->driver->getSessionKey());
|
||||
@@ -78,27 +81,30 @@ public function assertCount(int $count): void
|
||||
|
||||
public function assertTotalQuantity(int|float $quantity): void
|
||||
{
|
||||
Assert::assertEquals(
|
||||
Assert::assertEqualsWithDelta(
|
||||
$quantity,
|
||||
$this->getTotalQuantity(),
|
||||
(float) $this->getTotalQuantity(),
|
||||
0.0001,
|
||||
"Failed asserting that cart total quantity is [{$quantity}]."
|
||||
);
|
||||
}
|
||||
|
||||
public function assertSubTotal(float $amount): void
|
||||
{
|
||||
Assert::assertEquals(
|
||||
Assert::assertEqualsWithDelta(
|
||||
$amount,
|
||||
$this->getSubTotal(false),
|
||||
(float) $this->getSubTotal(false),
|
||||
0.0001,
|
||||
"Failed asserting that cart subtotal is [{$amount}]."
|
||||
);
|
||||
}
|
||||
|
||||
public function assertTotal(float $amount): void
|
||||
{
|
||||
Assert::assertEquals(
|
||||
Assert::assertEqualsWithDelta(
|
||||
$amount,
|
||||
$this->getTotal(),
|
||||
(float) $this->getTotal(),
|
||||
0.0001,
|
||||
"Failed asserting that cart total is [{$amount}]."
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user