fix: serialize conditions to arrays before storage; type \$events

- serializeConditions() now maps CartCondition objects to arrays via
  toArray() before passing to the driver, preventing json_encode()
  producing {} for object values in the DatabaseDriver
- Add Dispatcher type hint to \$events property and constructor parameter
- Add database round-trip test for cart-level conditions
- Fix CartConditionsTest to retrieve condition objects from the cart
  rather than relying on object-reference side-effects that no longer
  hold after proper serialization
This commit is contained in:
Joel Male
2026-05-27 21:21:44 +10:00
parent f3e5e958b7
commit 4877911cdd
3 changed files with 38 additions and 5 deletions
+4 -3
View File
@@ -10,13 +10,14 @@
use Wearepixel\Cart\Exceptions\InvalidItemException;
use Wearepixel\Cart\Exceptions\UnknownModelException;
use Wearepixel\Cart\Exceptions\InvalidConditionException;
use Illuminate\Contracts\Events\Dispatcher;
use Wearepixel\Cart\Drivers\Contracts\CartDriver;
class Cart
{
protected CartDriver $driver;
protected $events;
protected Dispatcher $events;
protected string $instanceName;
@@ -24,7 +25,7 @@ class Cart
protected mixed $currentItemId;
public function __construct(CartDriver $driver, $events, string $instanceName, array $config)
public function __construct(CartDriver $driver, Dispatcher $events, string $instanceName, array $config)
{
$this->driver = $driver;
$this->events = $events;
@@ -581,7 +582,7 @@ private function serializeItems(CartCollection $cart): array
private function serializeConditions(CartConditionCollection $conditions): array
{
return $conditions->all();
return $conditions->map(fn(CartCondition $c) => $c->toArray())->all();
}
protected function itemHasConditions(ItemCollection $item): bool