From 04f4dfa0187967a14268dd4ba6e1d4f584ae41fc Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Thu, 7 Feb 2019 12:32:53 +0100 Subject: [PATCH 1/9] Improved Readme Manually merged https://github.com/Crinsane/LaravelShoppingcart/pull/311 --- README.md | 2 +- src/Cart.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1819ed..733d03b 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Now you're ready to start using the shoppingcart in your application. **As of version 2 of this package it's possibly to use dependency injection to inject an instance of the Cart class into your controller or other class** -## Overview +## Table of Contents Look at one of the following topics to learn more about LaravelShoppingcart * [Usage](#usage) diff --git a/src/Cart.php b/src/Cart.php index 7e7e6b3..dc5df93 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -580,6 +580,8 @@ class Cart 'identifier' => $identifier, 'instance' => $this->currentInstance(), 'content' => serialize($content), + 'created_at' => date('Y-m-d H:i:s'), + 'updated_at' => date('Y-m-d H:i:s') ]); $this->events->fire('cart.stored'); From 7873c90c2b0334ea1a90e856be5a6ff9663b2bed Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Thu, 7 Feb 2019 12:34:49 +0100 Subject: [PATCH 2/9] removed monitor only master --- .travis.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 17377cc..b5776a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,11 +15,6 @@ after_success: # Submit coverage report to https://codecov.io - bash <(curl -s https://codecov.io/bash) -# Monitor only these branches -branches: - only: - - master - # You can delete the cache using travis-ci web interface cache: directories: From f8445b4d45c4a6dc56c35f4ff4c9d60d6ececb46 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Thu, 7 Feb 2019 12:35:48 +0100 Subject: [PATCH 3/9] Codestyle --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index dc5df93..01c7ec9 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -581,7 +581,7 @@ class Cart 'instance' => $this->currentInstance(), 'content' => serialize($content), 'created_at' => date('Y-m-d H:i:s'), - 'updated_at' => date('Y-m-d H:i:s') + 'updated_at' => date('Y-m-d H:i:s'), ]); $this->events->fire('cart.stored'); From 698098139b1ed68a414c813aea4783767185713c Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Mon, 18 Feb 2019 16:02:56 +0100 Subject: [PATCH 4/9] Added fields for created and updated at Added getters needs testing --- composer.json | 3 ++- src/Cart.php | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index d2b0ad4..ff0757c 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "require": { "illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*", "illuminate/session": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*", - "illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*" + "illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*", + "nesbot/carbon": "^1.26.3" }, "require-dev": { "phpunit/phpunit": "~5.0|~6.0|~7.0", diff --git a/src/Cart.php b/src/Cart.php index 01c7ec9..429cc32 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -12,6 +12,7 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\DatabaseManager; use Illuminate\Session\SessionManager; use Illuminate\Support\Collection; +use Carbon\Carbon; class Cart { @@ -38,6 +39,20 @@ class Cart */ private $instance; + /** + * Holds the creation date of the cart + * + * @var mixed + */ + private $createdAt; + + /** + * Holds the update date of the cart + * + * @var mixed + */ + private $updatedAt; + /** * Defines the discount percentage. * @@ -580,7 +595,7 @@ class Cart 'identifier' => $identifier, 'instance' => $this->currentInstance(), 'content' => serialize($content), - 'created_at' => date('Y-m-d H:i:s'), + 'created_at' => $this->createdAt ?: date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), ]); @@ -625,6 +640,9 @@ class Cart $this->instance($currentInstance); + $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(); } @@ -803,4 +821,24 @@ class Cart return number_format($value, $decimals, $decimalPoint, $thousandSeperator); } + + /** + * Get the creation date of the cart (db context). + * + * @return \Carbon\Carbon|null + */ + private function createdAt() + { + return $this->createdAt; + } + + /** + * Get the lats update date of the cart (db context). + * + * @return \Carbon\Carbon|null + */ + private function updatedAt() + { + return $this->createdAt; + } } From d6c0f2f72255d077fd37d84dd24c08212b1bc055 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Mon, 18 Feb 2019 16:04:50 +0100 Subject: [PATCH 5/9] style --- src/Cart.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 429cc32..2ffead8 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -2,6 +2,7 @@ namespace Gloudemans\Shoppingcart; +use Carbon\Carbon; use Closure; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; @@ -12,7 +13,6 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\DatabaseManager; use Illuminate\Session\SessionManager; use Illuminate\Support\Collection; -use Carbon\Carbon; class Cart { @@ -40,15 +40,15 @@ class Cart private $instance; /** - * Holds the creation date of the cart - * + * Holds the creation date of the cart. + * * @var mixed */ private $createdAt; /** - * Holds the update date of the cart - * + * Holds the update date of the cart. + * * @var mixed */ private $updatedAt; From a50767d58449922fff5e79ce54f41ee7664ba04e Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Fri, 8 May 2020 20:42:17 +0200 Subject: [PATCH 6/9] Use Carbon instead of date --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 182ef29..f4474c3 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -633,8 +633,8 @@ class Cart 'identifier' => $identifier, 'instance' => $this->currentInstance(), 'content' => serialize($content), - 'created_at' => $this->createdAt ?: date('Y-m-d H:i:s'), - 'updated_at' => date('Y-m-d H:i:s'), + 'created_at' => $this->createdAt ?: Carbon::now(), + 'updated_at' => Carbon::now(), ]); $this->events->dispatch('cart.stored'); From 05b99d4e721b6f658ca345475dcdf5ff614db1b2 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Fri, 8 May 2020 20:58:58 +0200 Subject: [PATCH 7/9] Make getters public, Add Test --- src/Cart.php | 4 ++-- tests/CartTest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index f4474c3..382cbd6 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -890,7 +890,7 @@ class Cart * * @return \Carbon\Carbon|null */ - private function createdAt() + public function createdAt() { return $this->createdAt; } @@ -900,7 +900,7 @@ class Cart * * @return \Carbon\Carbon|null */ - private function updatedAt() + public function updatedAt() { return $this->createdAt; } diff --git a/tests/CartTest.php b/tests/CartTest.php index 24da3cf..15d2d66 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -2,6 +2,7 @@ namespace Gloudemans\Tests\Shoppingcart; +use Carbon\Carbon; use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; @@ -870,6 +871,40 @@ class CartTest extends TestCase Event::assertDispatched('cart.stored'); } + /** @test */ + public function it_can_store_and_retrieve_cart_from_the_database_with_correct_timestamps() + { + $this->artisan('migrate', [ + '--database' => 'testing', + ]); + + Event::fake(); + + $cart = $this->getCart(); + + + + $cart->add(new BuyableProduct()); + + $beforeStore = Carbon::now(); + + /* Sleep as database does not store ms */ + sleep(1); + + $cart->store($identifier = 123); + + sleep(1); + + $afterStore = Carbon::now(); + + $cart->restore($identifier); + + $this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt())); + $this->assertTrue($beforeStore->lessThanOrEqualTo($cart->updatedAt()) && $afterStore->greaterThanOrEqualTo($cart->updatedAt())); + + Event::assertDispatched('cart.stored'); + } + /** * @test */ From 53b8015c1f06a4322ab39e538a294a5dbe835364 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Fri, 8 May 2020 21:00:41 +0200 Subject: [PATCH 8/9] StyleCI --- tests/CartTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 15d2d66..59a82c8 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -882,10 +882,8 @@ class CartTest extends TestCase $cart = $this->getCart(); - - $cart->add(new BuyableProduct()); - + $beforeStore = Carbon::now(); /* Sleep as database does not store ms */ From 8ee9bad739f65d0fa6903254f4392f1139720f15 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Fri, 8 May 2020 21:17:10 +0200 Subject: [PATCH 9/9] Improve test, Fix updatedAt(), --- src/Cart.php | 2 +- tests/CartTest.php | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 382cbd6..c73d0e7 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -902,6 +902,6 @@ class Cart */ public function updatedAt() { - return $this->createdAt; + return $this->updatedAt; } } diff --git a/tests/CartTest.php b/tests/CartTest.php index 59a82c8..09e3526 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -884,15 +884,13 @@ class CartTest extends TestCase $cart->add(new BuyableProduct()); - $beforeStore = Carbon::now(); - /* Sleep as database does not store ms */ + $beforeStore = Carbon::now(); sleep(1); $cart->store($identifier = 123); sleep(1); - $afterStore = Carbon::now(); $cart->restore($identifier); @@ -900,6 +898,20 @@ class CartTest extends TestCase $this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt())); $this->assertTrue($beforeStore->lessThanOrEqualTo($cart->updatedAt()) && $afterStore->greaterThanOrEqualTo($cart->updatedAt())); + /* Sleep as database does not store ms */ + $beforeSecondStore = Carbon::now(); + sleep(1); + + $cart->store($identifier); + + sleep(1); + $afterSecondStore = Carbon::now(); + + $cart->restore($identifier); + + $this->assertTrue($beforeStore->lessThanOrEqualTo($cart->createdAt()) && $afterStore->greaterThanOrEqualTo($cart->createdAt())); + $this->assertTrue($beforeSecondStore->lessThanOrEqualTo($cart->updatedAt()) && $afterSecondStore->greaterThanOrEqualTo($cart->updatedAt())); + Event::assertDispatched('cart.stored'); }