From 963dcd8d07410a78129129935e4469345429d603 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Sun, 21 Jul 2024 12:29:08 +0330 Subject: [PATCH 01/19] add `options` column to cart_items table --- .../migrations/2024_05_31_103315_create_cart_items_table.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/migrations/2024_05_31_103315_create_cart_items_table.php b/database/migrations/2024_05_31_103315_create_cart_items_table.php index 9a1d272..ca127a9 100644 --- a/database/migrations/2024_05_31_103315_create_cart_items_table.php +++ b/database/migrations/2024_05_31_103315_create_cart_items_table.php @@ -21,7 +21,8 @@ public function up(): void $table->foreignId($cartForeignName)->constrained($cartTableName)->cascadeOnDelete(); $table->morphs('itemable'); $table->unsignedInteger('quantity')->default(1); - + $table->json('options')->nullable(); + $table->timestamps(); }); } From c7ab74ff5bc7f7520019eddbdff508806d5f7437 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Sun, 21 Jul 2024 12:30:14 +0330 Subject: [PATCH 02/19] wip --- .../migrations/2024_05_31_103315_create_cart_items_table.php | 2 +- src/Models/CartItem.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/database/migrations/2024_05_31_103315_create_cart_items_table.php b/database/migrations/2024_05_31_103315_create_cart_items_table.php index ca127a9..5839076 100644 --- a/database/migrations/2024_05_31_103315_create_cart_items_table.php +++ b/database/migrations/2024_05_31_103315_create_cart_items_table.php @@ -22,7 +22,7 @@ public function up(): void $table->morphs('itemable'); $table->unsignedInteger('quantity')->default(1); $table->json('options')->nullable(); - + $table->timestamps(); }); } diff --git a/src/Models/CartItem.php b/src/Models/CartItem.php index 5ed65f3..ac9cfb1 100644 --- a/src/Models/CartItem.php +++ b/src/Models/CartItem.php @@ -14,7 +14,7 @@ class CartItem extends Model * * @var string[] */ - protected $fillable = ['cart_id', 'itemable_id', 'itemable_type', 'quantity']; + protected $guarded = ['id']; /** * Create a new instance of the model. From b8846414d23760a420a6dfc0e97b6ebe95ebd543 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Sun, 21 Jul 2024 12:34:37 +0330 Subject: [PATCH 03/19] Update CartItem.php --- src/Models/CartItem.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Models/CartItem.php b/src/Models/CartItem.php index ac9cfb1..6ce2355 100644 --- a/src/Models/CartItem.php +++ b/src/Models/CartItem.php @@ -26,6 +26,24 @@ public function __construct(array $attributes = []) $this->table = config('laravel-cart.cart_items.table', 'cart_items'); } + /** + * Get option. + */ + public function getOption(string $option): mixed + { + $options = json_decode($this->options); + + return $options[$option] ?? null; + } + + /** + * Get options. + */ + public function getOptions(): mixed + { + return json_decode($this->options); + } + /** * Relation polymorphic, inverse one-to-one or many relationship. */ From f62d8dd1bd5f126d4ddac82b2ec1ea221b9bc981 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Sun, 21 Jul 2024 12:48:25 +0330 Subject: [PATCH 04/19] add `setOption` --- src/Models/CartItem.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Models/CartItem.php b/src/Models/CartItem.php index 6ce2355..372de2b 100644 --- a/src/Models/CartItem.php +++ b/src/Models/CartItem.php @@ -44,6 +44,16 @@ public function getOptions(): mixed return json_decode($this->options); } + /** + * Get options. + */ + public function setOption(string $key, mixed $value): static + { + $this->update(['options' => json_encode([$key => $value])]); + + return $this; + } + /** * Relation polymorphic, inverse one-to-one or many relationship. */ From ce5bff7f0e4ec6d124a910e7f448923d4675be48 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Mon, 22 Jul 2024 09:30:11 +0330 Subject: [PATCH 05/19] add `addOption` method --- src/Models/CartItem.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Models/CartItem.php b/src/Models/CartItem.php index 372de2b..daa9f91 100644 --- a/src/Models/CartItem.php +++ b/src/Models/CartItem.php @@ -31,7 +31,7 @@ public function __construct(array $attributes = []) */ public function getOption(string $option): mixed { - $options = json_decode($this->options); + $options = json_decode($this->options, true); return $options[$option] ?? null; } @@ -41,11 +41,11 @@ public function getOption(string $option): mixed */ public function getOptions(): mixed { - return json_decode($this->options); + return json_decode($this->options, true); } /** - * Get options. + * Set option. */ public function setOption(string $key, mixed $value): static { @@ -54,6 +54,23 @@ public function setOption(string $key, mixed $value): static return $this; } + /** + * Add option. + */ + public function addOption(string $key, mixed $value): static + { + $options = $this->options; + if (!is_array($options)) { + $options = json_decode($options, true); + } + $options[] = [$key => $value]; + + $this->options = json_encode($options); + $this->save(); + + return $this; + } + /** * Relation polymorphic, inverse one-to-one or many relationship. */ From 530db3721f95498269d7606d13afb3295641227f Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Mon, 22 Jul 2024 09:38:39 +0330 Subject: [PATCH 06/19] Create CartOptionTest.php --- tests/Feature/Models/CartOptionTest.php | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/Feature/Models/CartOptionTest.php diff --git a/tests/Feature/Models/CartOptionTest.php b/tests/Feature/Models/CartOptionTest.php new file mode 100644 index 0000000..7a492c3 --- /dev/null +++ b/tests/Feature/Models/CartOptionTest.php @@ -0,0 +1,29 @@ +create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Store items to cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + $cart->storeItem($product1); + + // Set options + $cart->setOption($option); +}); From 9798f4998c54b07bb72d8f46c7ba30a2b7b584ad Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Tue, 30 Jul 2024 17:16:35 +0330 Subject: [PATCH 07/19] Update CartItemOptionTest.php --- .../Models/{CartOptionTest.php => CartItemOptionTest.php} | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) rename tests/Feature/Models/{CartOptionTest.php => CartItemOptionTest.php} (79%) diff --git a/tests/Feature/Models/CartOptionTest.php b/tests/Feature/Models/CartItemOptionTest.php similarity index 79% rename from tests/Feature/Models/CartOptionTest.php rename to tests/Feature/Models/CartItemOptionTest.php index 7a492c3..85cb699 100644 --- a/tests/Feature/Models/CartOptionTest.php +++ b/tests/Feature/Models/CartItemOptionTest.php @@ -25,5 +25,9 @@ $cart->storeItem($product1); // Set options - $cart->setOption($option); + $cart->items()->first()->setOption('size', 34); + + // DB Assertions + assertDatabaseCount('cart_items', 1); + \Pest\Laravel\assertDatabaseHas('cart_items', ['options' => $cart->items()->first()->getOption('size')]); }); From f8f61fa068c413cd4f6ea5c0f6d30bc5a001e120 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 15:57:25 +0330 Subject: [PATCH 08/19] Update CartItemOptionTest.php --- tests/Feature/Models/CartItemOptionTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Models/CartItemOptionTest.php b/tests/Feature/Models/CartItemOptionTest.php index 85cb699..1f0703a 100644 --- a/tests/Feature/Models/CartItemOptionTest.php +++ b/tests/Feature/Models/CartItemOptionTest.php @@ -10,6 +10,7 @@ use Tests\SetUp\Models\User; use function Pest\Laravel\assertDatabaseCount; +use function Pest\Laravel\assertDatabaseHas; /* * Use `RefreshDatabase` for delete migration data for each test. @@ -27,7 +28,9 @@ // Set options $cart->items()->first()->setOption('size', 34); + // Assertions + expect($cart->items()->first()->getOption('size'))->toBe(34); + // DB Assertions assertDatabaseCount('cart_items', 1); - \Pest\Laravel\assertDatabaseHas('cart_items', ['options' => $cart->items()->first()->getOption('size')]); }); From 15469b842378ae51f45dcc8594716c26de9951ff Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 15:58:39 +0330 Subject: [PATCH 09/19] Update CartItemOptionTest.php --- tests/Feature/Models/CartItemOptionTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Feature/Models/CartItemOptionTest.php b/tests/Feature/Models/CartItemOptionTest.php index 1f0703a..9c55f36 100644 --- a/tests/Feature/Models/CartItemOptionTest.php +++ b/tests/Feature/Models/CartItemOptionTest.php @@ -34,3 +34,21 @@ // DB Assertions assertDatabaseCount('cart_items', 1); }); + +test('can get all option of one items', closure: function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Store items to cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + $cart->storeItem($product1); + + // Set options + $cart->items()->first()->setOption('size', 34); + + // Assertions + expect($cart->items()->first()->getOptions())->toBe(['size' => 34]); + + // DB Assertions + assertDatabaseCount('cart_items', 1); +}); From be7e352ac9432a81dfffb20966fea7e458833297 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:04:24 +0330 Subject: [PATCH 10/19] add `can add option for one cart item` test and fix bug --- src/Models/CartItem.php | 7 +++--- tests/Feature/Models/CartItemOptionTest.php | 24 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Models/CartItem.php b/src/Models/CartItem.php index daa9f91..aa1ca6d 100644 --- a/src/Models/CartItem.php +++ b/src/Models/CartItem.php @@ -59,11 +59,12 @@ public function setOption(string $key, mixed $value): static */ public function addOption(string $key, mixed $value): static { - $options = $this->options; - if (!is_array($options)) { + $options = $this->getOptions(); + if (! is_array($options)) { $options = json_decode($options, true); } - $options[] = [$key => $value]; + + $options[$key] = $value; $this->options = json_encode($options); $this->save(); diff --git a/tests/Feature/Models/CartItemOptionTest.php b/tests/Feature/Models/CartItemOptionTest.php index 9c55f36..2763b7b 100644 --- a/tests/Feature/Models/CartItemOptionTest.php +++ b/tests/Feature/Models/CartItemOptionTest.php @@ -35,7 +35,7 @@ assertDatabaseCount('cart_items', 1); }); -test('can get all option of one items', closure: function () { +test('can get all option of one cart item', closure: function () { $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); $product1 = Product::query()->create(['title' => 'Product 1']); @@ -52,3 +52,25 @@ // DB Assertions assertDatabaseCount('cart_items', 1); }); + +test('can add option for one cart item', closure: function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Store items to cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + $cart->storeItem($product1); + + // Set options + $cart->items()->first()->addOption('size', 34); + $cart->items()->first()->addOption('address', 'something'); + + // Assertions + expect($cart->items()->first()->getOption('size')) + ->toBe(34) + ->and($cart->items()->first()->getOption('address')) + ->toBe('something'); + + // DB Assertions + assertDatabaseCount('cart_items', 1); +}); From 5a101c4f719644e65eb4ea35223eff7c29da2ee9 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:04:44 +0330 Subject: [PATCH 11/19] Update CartItem.php --- src/Models/CartItem.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Models/CartItem.php b/src/Models/CartItem.php index aa1ca6d..21ca6ee 100644 --- a/src/Models/CartItem.php +++ b/src/Models/CartItem.php @@ -26,6 +26,8 @@ public function __construct(array $attributes = []) $this->table = config('laravel-cart.cart_items.table', 'cart_items'); } + // Methods + /** * Get option. */ @@ -72,6 +74,8 @@ public function addOption(string $key, mixed $value): static return $this; } + // Relations + /** * Relation polymorphic, inverse one-to-one or many relationship. */ From 3bd3a4e7fc9eaa4748102e55463b313334f88466 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:14:20 +0330 Subject: [PATCH 12/19] Update LaravelCartSession.php --- src/Drivers/LaravelCartSession.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Drivers/LaravelCartSession.php b/src/Drivers/LaravelCartSession.php index a3e1ad9..2edf755 100644 --- a/src/Drivers/LaravelCartSession.php +++ b/src/Drivers/LaravelCartSession.php @@ -143,6 +143,7 @@ protected function resolveUserId(?int $userId): int return $userId ?? auth()->id(); } + // @codeCoverageIgnoreStart /** * Format an item for storage. */ @@ -159,4 +160,5 @@ protected function formatItem(Model|array $item): array return $item; } + // @codeCoverageIgnoreEnd } From 3aa7ba35569b6e0a6f6dab6eaf286b38ea379134 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:14:22 +0330 Subject: [PATCH 13/19] Create LaravelCartManagerTest.php --- tests/Feature/Manager/LaravelCartManagerTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/Feature/Manager/LaravelCartManagerTest.php diff --git a/tests/Feature/Manager/LaravelCartManagerTest.php b/tests/Feature/Manager/LaravelCartManagerTest.php new file mode 100644 index 0000000..a8f3871 --- /dev/null +++ b/tests/Feature/Manager/LaravelCartManagerTest.php @@ -0,0 +1,10 @@ +toBe(config('laravel-cart.driver.default')); + + config(['laravel-cart.driver.default' => 'session']); + expect(LaravelCart::getDefaultDriver())->toBe('session'); +}); From 204e283259ff06a5b3950125b7680dd1bac1eee5 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:14:40 +0330 Subject: [PATCH 14/19] add `getOption` method to LaravelCartDatabase --- src/Drivers/LaravelCartDatabase.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Drivers/LaravelCartDatabase.php b/src/Drivers/LaravelCartDatabase.php index 4c51074..fbecd61 100644 --- a/src/Drivers/LaravelCartDatabase.php +++ b/src/Drivers/LaravelCartDatabase.php @@ -3,6 +3,7 @@ namespace Binafy\LaravelCart\Drivers; use Binafy\LaravelCart\Models\Cart; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class LaravelCartDatabase implements Driver @@ -86,6 +87,19 @@ public function emptyCart(?int $userId = null): static return $this; } + /** + * Get option from item. + */ + public function getOption(string $option, ?int $itemId = null, ?int $userId = null): mixed + { + $cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]); + $items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) { + $builder->where('id', $itemId); + }); + + return $items->first()->getOption($option); + } + /** * Resolve the user ID, defaulting to the authenticated user. */ From e89b626487a233f6a664bf3e57870b56bbaae153 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:18:58 +0330 Subject: [PATCH 15/19] add option methods for LaravelCartDatabase --- src/Drivers/LaravelCartDatabase.php | 45 ++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Drivers/LaravelCartDatabase.php b/src/Drivers/LaravelCartDatabase.php index fbecd61..bba5728 100644 --- a/src/Drivers/LaravelCartDatabase.php +++ b/src/Drivers/LaravelCartDatabase.php @@ -88,7 +88,7 @@ public function emptyCart(?int $userId = null): static } /** - * Get option from item. + * Get option for item. */ public function getOption(string $option, ?int $itemId = null, ?int $userId = null): mixed { @@ -100,6 +100,49 @@ public function getOption(string $option, ?int $itemId = null, ?int $userId = nu return $items->first()->getOption($option); } + /** + * Get all options of one item. + */ + public function getOptions(?int $itemId = null, ?int $userId = null): mixed + { + $cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]); + $items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) { + $builder->where('id', $itemId); + }); + + return $items->first()->getOptions(); + } + + /** + * Set option for item. + */ + public function setOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null): static + { + $cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]); + $items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) { + $builder->where('id', $itemId); + }); + + $items->first()->setOption($key, $value); + + return $this; + } + + /** + * Get option for item. + */ + public function addOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null): static + { + $cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]); + $items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) { + $builder->where('id', $itemId); + }); + + $items->first()->addOption($key, $value); + + return $this; + } + /** * Resolve the user ID, defaulting to the authenticated user. */ From 71c6dcbf370b5f188b1418d1a92c98861e941db6 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:20:12 +0330 Subject: [PATCH 16/19] Update LaravelCart.php --- src/LaravelCart.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/LaravelCart.php b/src/LaravelCart.php index 2b0b7dc..faff20d 100644 --- a/src/LaravelCart.php +++ b/src/LaravelCart.php @@ -2,8 +2,6 @@ namespace Binafy\LaravelCart; -use Illuminate\Support\Facades\Facade; - /** * @method static \Binafy\LaravelCart\Drivers\Driver driver(string|null $driver = null) * @method static \Binafy\LaravelCart\Drivers\Driver storeItem(\Illuminate\Database\Eloquent\Model|array $item, int|null $userId = null) @@ -12,12 +10,16 @@ * @method static \Binafy\LaravelCart\Drivers\Driver decreaseQuantity(\Illuminate\Database\Eloquent\Model $item, int $quantity = 1) * @method static \Binafy\LaravelCart\Drivers\Driver removeItem(\Illuminate\Database\Eloquent\Model $item) * @method static \Binafy\LaravelCart\Drivers\Driver emptyCart() + * @method static \Binafy\LaravelCart\Drivers\Driver getOption(string $option, ?int $itemId = null, ?int $userId = null) + * @method static \Binafy\LaravelCart\Drivers\Driver getOptions(?int $itemId = null, ?int $userId = null) + * @method static \Binafy\LaravelCart\Drivers\Driver setOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null) + * @method static \Binafy\LaravelCart\Drivers\Driver addOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null) * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) * * @see \Binafy\LaravelCart\Manager\LaravelCartManager */ -class LaravelCart extends Facade +class LaravelCart extends \Illuminate\Support\Facades\Facade { /** * Get the registered name of the component. From 8bd0a999a9ab7d4c2ac2d1fe960f4768a6f80247 Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:26:47 +0330 Subject: [PATCH 17/19] Create CartItemOptionTest.php --- .../Manager/Database/CartItemOptionTest.php | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/Feature/Manager/Database/CartItemOptionTest.php diff --git a/tests/Feature/Manager/Database/CartItemOptionTest.php b/tests/Feature/Manager/Database/CartItemOptionTest.php new file mode 100644 index 0000000..0a1c87e --- /dev/null +++ b/tests/Feature/Manager/Database/CartItemOptionTest.php @@ -0,0 +1,107 @@ +create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.com']); + auth()->login($user); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Store items to cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + $cart->storeItem($product1); + + // Set options + LaravelCart::driver('database')->setOption('size', 34); + + // Assertions + expect(LaravelCart::driver('database')->getOption('size'))->toBe(34); + + // DB Assertions + assertDatabaseCount('cart_items', 1); +}); + +test('can set option for cart with specific id', closure: function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.com']); + auth()->login($user); + $product1 = Product::query()->create(['title' => 'Product 1']); + $product2 = Product::query()->create(['title' => 'Product 2']); + + // Store items to cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + $cart->storeItem($product1); + $cart->storeItem($product2); + + // Set options + LaravelCart::driver('database')->setOption('size', 80, 2); + + // Assertions + expect(LaravelCart::driver('database')->getOption('size', 2)) + ->toBe(80) + ->and(LaravelCart::driver('database')->getOptions(2)) + ->toBe(['size' => 80]); + + // Add option + LaravelCart::driver('database')->addOption('address', 'something', 2); + + // DB Assertions + assertDatabaseCount('cart_items', 2); +}); + +test('can get all option of one cart item', closure: function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.com']); + auth()->login($user); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Store items to cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + $cart->storeItem($product1); + + // Set options + LaravelCart::driver('database')->setOption('size', 34); + + // Assertions + expect(LaravelCart::driver('database')->getOptions())->toBe(['size' => 34]); + + // DB Assertions + assertDatabaseCount('cart_items', 1); +}); + +test('can add option for one cart item', closure: function () { + $user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.com']); + auth()->login($user); + $product1 = Product::query()->create(['title' => 'Product 1']); + + // Store items to cart + $cart = Cart::query()->firstOrCreate(['user_id' => $user->id]); + $cart->storeItem($product1); + + // Set options + LaravelCart::driver('database')->addOption('size', 34); + LaravelCart::driver('database')->addOption('address', 'something'); + + // Assertions + expect(LaravelCart::driver('database')->getOption('size')) + ->toBe(34) + ->and(LaravelCart::driver('database')->getOption('address')) + ->toBe('something'); + + // DB Assertions + assertDatabaseCount('cart_items', 1); +}); From 00eae0a7e70c8a6757c081261e6777119b6c619b Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:28:04 +0330 Subject: [PATCH 18/19] Update LaravelCartSession.php --- src/Drivers/LaravelCartSession.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/Drivers/LaravelCartSession.php b/src/Drivers/LaravelCartSession.php index 2edf755..e0f232c 100644 --- a/src/Drivers/LaravelCartSession.php +++ b/src/Drivers/LaravelCartSession.php @@ -142,23 +142,4 @@ protected function resolveUserId(?int $userId): int { return $userId ?? auth()->id(); } - - // @codeCoverageIgnoreStart - /** - * Format an item for storage. - */ - protected function formatItem(Model|array $item): array - { - if ($item instanceof Model) { - return [ - 'id' => $item->getKey(), - 'type' => get_class($item), - 'quantity' => 1, - 'attributes' => $item->toArray(), - ]; - } - - return $item; - } - // @codeCoverageIgnoreEnd } From 174694f5e723772afe2dd7a473cca03bf94abf0b Mon Sep 17 00:00:00 2001 From: Milwad <98118400+milwad-dev@users.noreply.github.com> Date: Thu, 26 Dec 2024 17:04:30 +0330 Subject: [PATCH 19/19] pint --- tests/Feature/Manager/Database/CartItemOptionTest.php | 5 ----- tests/Feature/Models/CartItemOptionTest.php | 5 ----- 2 files changed, 10 deletions(-) diff --git a/tests/Feature/Manager/Database/CartItemOptionTest.php b/tests/Feature/Manager/Database/CartItemOptionTest.php index 0a1c87e..1ab6885 100644 --- a/tests/Feature/Manager/Database/CartItemOptionTest.php +++ b/tests/Feature/Manager/Database/CartItemOptionTest.php @@ -1,17 +1,12 @@