From 6d15ac8c43e93140bf859018d030a61383e8ca3a Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Tue, 3 Nov 2020 13:25:43 +0000 Subject: [PATCH 01/11] Update Cart.php Add instance to storedCartWithIdentifierExists --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 369bb2e..60937f4 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -831,7 +831,7 @@ class Cart */ private function storedCartWithIdentifierExists($identifier) { - return $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->exists(); + return $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->where('instance', $this->currentInstance())->exists(); } /** From 6ba97cafa8573e29357136df387c08454120c477 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Tue, 3 Nov 2020 13:55:43 +0000 Subject: [PATCH 02/11] Use instance in query if there are more than one instances and add tests --- src/Cart.php | 8 ++++++-- tests/CartTest.php | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 60937f4..4b1eb8a 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -98,7 +98,7 @@ class Cart $instance = $instance->getInstanceIdentifier(); } - $this->instance = 'cart.'.$instance; + $this->instance = 'cart.' . $instance; return $this; } @@ -831,7 +831,11 @@ class Cart */ private function storedCartWithIdentifierExists($identifier) { - return $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->where('instance', $this->currentInstance())->exists(); + $data = ['identifier' => $identifier]; + if ($this->countInstances() > 1) { + $data['instance'] = $this->currentInstance(); + } + return $this->getConnection()->table($this->getTableName())->where($data)->exists(); } /** diff --git a/tests/CartTest.php b/tests/CartTest.php index 0e45315..c2a0bd4 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -65,7 +65,7 @@ class CartTest extends TestCase parent::setUp(); $this->app->afterResolving('migrator', function ($migrator) { - $migrator->path(realpath(__DIR__.'/../src/Database/migrations')); + $migrator->path(realpath(__DIR__ . '/../src/Database/migrations')); }); } @@ -1011,7 +1011,7 @@ class CartTest extends TestCase $cart = $this->getCartDiscount(50); $cart->add(new BuyableProduct(1, 'First item', 10.00), 1); - $cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty'=>2]); + $cart->update('027c91341fd5cf4d2579b49c4b6a90da', ['qty' => 2]); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1453,4 +1453,36 @@ class CartTest extends TestCase $this->app['config']->set('cart.format.decimal_point', $decimalPoint); $this->app['config']->set('cart.format.thousand_separator', $thousandSeperator); } + + /** @test */ + public function it_can_store__mutiple_instances_of_the_cart_in_a_database() + { + $this->artisan('migrate', [ + '--database' => 'testing', + ]); + + Event::fake(); + + $cart = $this->getCart(); + + $cart->add(new BuyableProduct()); + + $cart->store($identifier = 123); + + $serialized = serialize($cart->content()); + + $newInstance = $this->getCart(); + $newInstance->instance($instanceName = 'someinstance'); + $newInstance->add(new BuyableProduct()); + $newInstance->store($newIdentifier = 456); + $newInstanceSerialized = serialize($newInstance->content()); + + $this->assertDatabaseCount('shoppingcart', 2); + + $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]); + + $this->assertDatabaseHas('shoppingcart', ['identifier' => $newIdentifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]); + + Event::assertDispatched('cart.stored'); + } } From 6460326b1a9e6ec02c3e1c268c7327b4c12aebf8 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Tue, 3 Nov 2020 14:03:13 +0000 Subject: [PATCH 03/11] Style CI fixes and remove assertDatabaseCount --- src/Cart.php | 2 +- tests/CartTest.php | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 4b1eb8a..a5c0965 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -98,7 +98,7 @@ class Cart $instance = $instance->getInstanceIdentifier(); } - $this->instance = 'cart.' . $instance; + $this->instance = 'cart.'.$instance; return $this; } diff --git a/tests/CartTest.php b/tests/CartTest.php index c2a0bd4..ea6b2ef 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -65,7 +65,7 @@ class CartTest extends TestCase parent::setUp(); $this->app->afterResolving('migrator', function ($migrator) { - $migrator->path(realpath(__DIR__ . '/../src/Database/migrations')); + $migrator->path(realpath(__DIR__.'/../src/Database/migrations')); }); } @@ -1477,8 +1477,6 @@ class CartTest extends TestCase $newInstance->store($newIdentifier = 456); $newInstanceSerialized = serialize($newInstance->content()); - $this->assertDatabaseCount('shoppingcart', 2); - $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]); $this->assertDatabaseHas('shoppingcart', ['identifier' => $newIdentifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]); From 981bf2940f38de0fb7aa9722dde596afd9b090d9 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Tue, 3 Nov 2020 14:04:37 +0000 Subject: [PATCH 04/11] Style CI fixes --- src/Cart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Cart.php b/src/Cart.php index a5c0965..4619d88 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -835,6 +835,7 @@ class Cart if ($this->countInstances() > 1) { $data['instance'] = $this->currentInstance(); } + return $this->getConnection()->table($this->getTableName())->where($data)->exists(); } From 3244c1b306abd767a0fbef9735998618cdee93f0 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Tue, 3 Nov 2020 14:05:55 +0000 Subject: [PATCH 05/11] Style CI fixes --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 4619d88..c5492e6 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -835,7 +835,7 @@ class Cart if ($this->countInstances() > 1) { $data['instance'] = $this->currentInstance(); } - + return $this->getConnection()->table($this->getTableName())->where($data)->exists(); } From 87acd5cfb206c9f7f25d95c221b5ef55b7faaf5d Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Sun, 15 Nov 2020 05:55:29 +0000 Subject: [PATCH 06/11] rename the storedCartWithIdentifier method to storedCartInstanceWithIdentifierExists, changed the signature of the merge and storedCartInstanceWithIdentifierExists methods and add instance to all stored cart retrievals --- src/Cart.php | 37 ++++++++++++++++++------------------- tests/CartTest.php | 27 +++++++++++++++------------ 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index c5492e6..447ffec 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -633,13 +633,15 @@ class Cart $identifier = $identifier->getInstanceIdentifier(); } - if ($this->storedCartWithIdentifierExists($identifier)) { + $instance = $this->currentInstance(); + + if ($this->storedCartInstanceWithIdentifierExists($instance, $identifier)) { throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored."); } $this->getConnection()->table($this->getTableName())->insert([ 'identifier' => $identifier, - 'instance' => $this->currentInstance(), + 'instance' => $instance, 'content' => serialize($content), 'created_at' => $this->createdAt ?: Carbon::now(), 'updated_at' => Carbon::now(), @@ -661,17 +663,17 @@ class Cart $identifier = $identifier->getInstanceIdentifier(); } - if (!$this->storedCartWithIdentifierExists($identifier)) { + $currentInstance = $this->currentInstance(); + + if (!$this->storedCartInstanceWithIdentifierExists($currentInstance, $identifier)) { return; } $stored = $this->getConnection()->table($this->getTableName()) - ->where('identifier', $identifier)->first(); + ->where(['identifier'=> $identifier,'instance' => $currentInstance])->first(); $storedContent = unserialize(data_get($stored, 'content')); - $currentInstance = $this->currentInstance(); - $this->instance(data_get($stored, 'instance')); $content = $this->getContent(); @@ -689,7 +691,7 @@ class Cart $this->createdAt = Carbon::parse(data_get($stored, 'created_at')); $this->updatedAt = Carbon::parse(data_get($stored, 'updated_at')); - $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete(); + $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $currentInstance])->delete(); } /** @@ -705,11 +707,13 @@ class Cart $identifier = $identifier->getInstanceIdentifier(); } - if (!$this->storedCartWithIdentifierExists($identifier)) { + $instance = $this->currentInstance(); + + if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) { return; } - $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete(); + $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $instance])->delete(); $this->events->dispatch('cart.erased'); } @@ -724,14 +728,14 @@ class Cart * * @return bool */ - public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true) + public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true, $instance = SELF::DEFAULT_INSTANCE) { - if (!$this->storedCartWithIdentifierExists($identifier)) { + if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) { return false; } $stored = $this->getConnection()->table($this->getTableName()) - ->where('identifier', $identifier)->first(); + ->where(['identifier'=> $identifier,'instance'=>$instance])->first(); $storedContent = unserialize($stored->content); @@ -829,14 +833,9 @@ class Cart * * @return bool */ - private function storedCartWithIdentifierExists($identifier) + private function storedCartInstanceWithIdentifierExists($instance, $identifier) { - $data = ['identifier' => $identifier]; - if ($this->countInstances() > 1) { - $data['instance'] = $this->currentInstance(); - } - - return $this->getConnection()->table($this->getTableName())->where($data)->exists(); + return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists(); } /** diff --git a/tests/CartTest.php b/tests/CartTest.php index ea6b2ef..4732d67 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -865,11 +865,11 @@ class CartTest extends TestCase $cart->store($identifier = 123); + Event::assertDispatched('cart.stored'); + $serialized = serialize($cart->content()); $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]); - - Event::assertDispatched('cart.stored'); } /** @test */ @@ -905,6 +905,8 @@ class CartTest extends TestCase $cart->store($identifier); + Event::assertDispatched('cart.stored'); + sleep(1); $afterSecondStore = Carbon::now(); @@ -912,8 +914,6 @@ class CartTest extends TestCase $this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt())); $this->assertTrue($beforeSecondStore->lessThanOrEqualTo($cart->updatedAt()) && $afterSecondStore->greaterThanOrEqualTo($cart->updatedAt())); - - Event::assertDispatched('cart.stored'); } /** @@ -936,9 +936,9 @@ class CartTest extends TestCase $cart->store($identifier = 123); - $cart->store($identifier); - Event::assertDispatched('cart.stored'); + + $cart->store($identifier); } /** @test */ @@ -962,11 +962,11 @@ class CartTest extends TestCase $cart->restore($identifier); + Event::assertDispatched('cart.restored'); + $this->assertItemsInCart(1, $cart); $this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => 'default']); - - Event::assertDispatched('cart.restored'); } /** @test */ @@ -1469,18 +1469,21 @@ class CartTest extends TestCase $cart->store($identifier = 123); + Event::assertDispatched('cart.stored'); + $serialized = serialize($cart->content()); $newInstance = $this->getCart(); $newInstance->instance($instanceName = 'someinstance'); $newInstance->add(new BuyableProduct()); - $newInstance->store($newIdentifier = 456); + $newInstance->store($identifier); + + Event::assertDispatched('cart.stored'); + $newInstanceSerialized = serialize($newInstance->content()); $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]); - $this->assertDatabaseHas('shoppingcart', ['identifier' => $newIdentifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]); - - Event::assertDispatched('cart.stored'); + $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]); } } From 7e992af0b7573f41177c2c296a6b44d66974fc65 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Sun, 15 Nov 2020 05:59:23 +0000 Subject: [PATCH 07/11] Add savedcartinstance as a parameter to the merge documentation --- README.md | 2 +- README_Idn.md | 2 +- README_uk-UA.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 98a09b4..1cc5eb3 100644 --- a/README.md +++ b/README.md @@ -585,7 +585,7 @@ If you want to retrieve the cart from the database and restore it, all you have If you want to merge the cart with another one from the database, all you have to do is call the `merge($identifier)` where `$identifier` is the key you specified for the `store` method. You can also define if you want to keep the discount and tax rates of the items and if you want to dispatch "cart.added" events. // Merge the contents of 'savedcart' into 'username'. - Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd); + Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, $dispatchAdd, 'savedcartinstance'); ### Erasing the cart If you want to erase the cart from the database, all you have to do is call the `erase($identifier)` where `$identifier` is the key you specified for the `store` method. diff --git a/README_Idn.md b/README_Idn.md index 5861093..a9999a1 100644 --- a/README_Idn.md +++ b/README_Idn.md @@ -544,7 +544,7 @@ Jika Anda ingin mengambil keranjang dari database dan mengembalikannya, yang har Jika Anda ingin menggabungkan keranjang dengan keranjang lain dari basis data, yang harus Anda lakukan adalah memanggil `gabungan ($ identifier)` di mana `$ identifier` adalah kunci yang Anda tentukan untuk metode` store`. Anda juga dapat menentukan apakah Anda ingin mempertahankan potongan harga dan tarif pajak item. // Merge the contents of 'savedcart' into 'username'. - Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate); + Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, 'savedcartinstance'); ## Pengecualian diff --git a/README_uk-UA.md b/README_uk-UA.md index fb27de2..9b2ed83 100644 --- a/README_uk-UA.md +++ b/README_uk-UA.md @@ -542,7 +542,7 @@ foreach(Cart::content() as $row) { Якщо ви хочете злити кошик із іншим кошиком, збереженим у базі даних, вам знадобиться викликати метод `merge($identifier)`, де `$identifier` - це ключ, який ви зазначили у методі`store`. Ви також можете визначити чи хочете ви зберегти знижку і ставку оподаткування для товарів. // Merge the contents of 'savedcart' into 'username'. - Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate); + Cart::instance('username')->merge('savedcart', $keepDiscount, $keepTaxrate, 'savedcartinstance'); ## Перехоплення From 148347b601911ae84d5042cca261be18546c5ae4 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Sun, 15 Nov 2020 06:03:59 +0000 Subject: [PATCH 08/11] Style CI Fixes --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 447ffec..130f60a 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -735,7 +735,7 @@ class Cart } $stored = $this->getConnection()->table($this->getTableName()) - ->where(['identifier'=> $identifier,'instance'=>$instance])->first(); + ->where(['identifier'=> $identifier, 'instance'=> $instance])->first(); $storedContent = unserialize($stored->content); From 427ea770d714acd752c87ae772037430f44ea023 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Sun, 15 Nov 2020 06:06:15 +0000 Subject: [PATCH 09/11] Style CI Fixes --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 130f60a..20fe910 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -670,7 +670,7 @@ class Cart } $stored = $this->getConnection()->table($this->getTableName()) - ->where(['identifier'=> $identifier,'instance' => $currentInstance])->first(); + ->where(['identifier'=> $identifier, 'instance' => $currentInstance])->first(); $storedContent = unserialize(data_get($stored, 'content')); @@ -728,7 +728,7 @@ class Cart * * @return bool */ - public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true, $instance = SELF::DEFAULT_INSTANCE) + public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true, $instance = self::DEFAULT_INSTANCE) { if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) { return false; From 369bdb95f3a403b5ff5023aff6d80945731a03b8 Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Sun, 15 Nov 2020 06:46:12 +0000 Subject: [PATCH 10/11] Add more tests --- tests/CartTest.php | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 4732d67..4606a65 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1482,8 +1482,46 @@ class CartTest extends TestCase $newInstanceSerialized = serialize($newInstance->content()); - $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]); + $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE, 'content' => $serialized]); $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => $instanceName, 'content' => $newInstanceSerialized]); } + + /** @test */ + public function it_can_calculate_the_total_price_of_the_items_in_cart() + { + $cart = $this->getCart(); + + $cart->add(new BuyableProduct(1, 'first item', $price = 1000), $qty = 5); + $this->assertEquals(5000, $cart->priceTotalFloat()); + } + + /** @test */ + public function it_can_format_the_total_price_of_the_items_in_cart() + { + $cart = $this->getCart(); + + $cart->add(new BuyableProduct(1, 'first item', 1000), 5); + $this->assertEquals("5,000.00", $cart->priceTotal()); + $this->assertEquals("5,000.0000", $cart->priceTotal(4, ".", ",")); + } + + /** @test */ + public function it_can_erase_saved_cart_from_the_database() + { + $this->artisan('migrate', [ + '--database' => 'testing', + ]); + + Event::fake(); + + $cart = $this->getCart(); + $cart->add(new BuyableProduct(1, 'Item', 10.00), 1); + $cart->add(new BuyableProduct(2, 'Item 2', 10.00), 1); + $cart->store($identifier = 'test'); + $cart->erase($identifier); + Event::assertDispatched('cart.erased'); + $this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE]); + + } } From 7afec5d17171b579b20df5d405e8c48c4c504def Mon Sep 17 00:00:00 2001 From: Norris Oduro Date: Sun, 15 Nov 2020 06:47:38 +0000 Subject: [PATCH 11/11] Style CI fixes --- tests/CartTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 4606a65..f6a75bc 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1502,8 +1502,8 @@ class CartTest extends TestCase $cart = $this->getCart(); $cart->add(new BuyableProduct(1, 'first item', 1000), 5); - $this->assertEquals("5,000.00", $cart->priceTotal()); - $this->assertEquals("5,000.0000", $cart->priceTotal(4, ".", ",")); + $this->assertEquals('5,000.00', $cart->priceTotal()); + $this->assertEquals('5,000.0000', $cart->priceTotal(4, '.', ',')); } /** @test */ @@ -1522,6 +1522,5 @@ class CartTest extends TestCase $cart->erase($identifier); Event::assertDispatched('cart.erased'); $this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE]); - } }