From aa654e5a126817ba3891aae6700f53458ff97fd0 Mon Sep 17 00:00:00 2001 From: Nicolas Sanavia <bsn.sanavia.nicolas@gmail.com> Date: Thu, 26 May 2022 11:55:45 +0200 Subject: [PATCH 1/3] feat: make possible to provide only id as model on cart item associate method --- src/CartItem.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 2a1ea36..382b62c 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -140,15 +140,23 @@ public function updateFromArray(array $attributes) } /** - * Associate the cart item with the given model. + * Associate the cart item with a given model or something else. * - * @param mixed $model - * @return CartItem + * @param $item + * @param bool $is_model + * @return $this */ - public function associate($model): CartItem + public function associate($item, bool $is_model = true): CartItem { - $this->associatedModel = is_string($model) ? $model : get_class($model); - $this->model = $model; + if ($is_model) { + $this->associatedModel = is_string($item) ? $item : get_class($item); + $this->model = $item; + + return $this; + } + + $this->associatedModel = Arr::has($item, 'associatedModel') ? Arr::get($item, 'associatedModel') : null; + $this->model = Arr::has($item, 'modelId') ? Arr::get($item, 'modelId') : null; return $this; } From 121734debb120a29298ab7086d7a43b96d8ba39b Mon Sep 17 00:00:00 2001 From: Nicolas Sanavia <bsn.sanavia.nicolas@gmail.com> Date: Thu, 26 May 2022 12:08:35 +0200 Subject: [PATCH 2/3] feat: add test for the new feature --- tests/CartItemTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 5e81e15..7cc1bc8 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -355,4 +355,30 @@ public function it_can_sum_discount() $this->assertTrue($cartItem->hasCoupons()); } + + /** @test */ + public function it_can_associate_model_id() + { + $cartItem = new CartItem( + 1, + 'First Cart item', + 'This is a simple description', + 1, + 100.00, + 122.00, + '0', + '0', + 22.00, + 'https://ecommerce.test/images/item-name.png', + ['size' => 'XL', 'color' => 'red'] + ); + + $cartItem->associate([ + 'associatedModel' => 'OfflineAgency\LaravelCart\Tests\Fixtures\ProductModel', + 'modelId' => 'fake_id' + ], false); + + $this->assertEquals('OfflineAgency\LaravelCart\Tests\Fixtures\ProductModel', $cartItem->associatedModel); + $this->assertEquals('fake_id', $cartItem->model); + } } From a175e9d5d3c30260be862875d94d4461937e3fd6 Mon Sep 17 00:00:00 2001 From: StyleCI Bot <bot@styleci.io> Date: Thu, 26 May 2022 10:12:53 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI --- src/CartItem.php | 2 +- tests/CartItemTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 382b62c..4283c2e 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -143,7 +143,7 @@ public function updateFromArray(array $attributes) * Associate the cart item with a given model or something else. * * @param $item - * @param bool $is_model + * @param bool $is_model * @return $this */ public function associate($item, bool $is_model = true): CartItem diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 7cc1bc8..4e85434 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -375,7 +375,7 @@ public function it_can_associate_model_id() $cartItem->associate([ 'associatedModel' => 'OfflineAgency\LaravelCart\Tests\Fixtures\ProductModel', - 'modelId' => 'fake_id' + 'modelId' => 'fake_id', ], false); $this->assertEquals('OfflineAgency\LaravelCart\Tests\Fixtures\ProductModel', $cartItem->associatedModel);