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: diff --git a/README.md b/README.md index 1a0f6de..2b71e22 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 * [Important note](#important-note) diff --git a/composer.json b/composer.json index c503067..3825978 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "require": { "illuminate/support": "5.4.*||5.5.*||5.6.*||5.7.*||5.8.*||^6.0||^7.0", "illuminate/session": "5.4.*||5.5.*||5.6.*||5.7.*||5.8.*||^6.0||^7.0", - "illuminate/events": "5.4.*||5.5.*||5.6.*||5.7.*||5.8.*||^6.0||^7.0" + "illuminate/events": "5.4.*||5.5.*||5.6.*||5.7.*||5.8.*||^6.0||^7.0", + "nesbot/carbon": "~1.20||^1.26.3||^2.0" }, "require-dev": { "phpunit/phpunit": "~5.0||~6.0||~7.0||~8.0", diff --git a/src/Cart.php b/src/Cart.php index 53bedbd..c73d0e7 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; @@ -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. * @@ -618,6 +633,8 @@ class Cart 'identifier' => $identifier, 'instance' => $this->currentInstance(), 'content' => serialize($content), + 'created_at' => $this->createdAt ?: Carbon::now(), + 'updated_at' => Carbon::now(), ]); $this->events->dispatch('cart.stored'); @@ -661,6 +678,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(); } @@ -864,4 +884,24 @@ class Cart return number_format($value, $decimals, $decimalPoint, $thousandSeperator); } + + /** + * Get the creation date of the cart (db context). + * + * @return \Carbon\Carbon|null + */ + public function createdAt() + { + return $this->createdAt; + } + + /** + * Get the lats update date of the cart (db context). + * + * @return \Carbon\Carbon|null + */ + public function updatedAt() + { + return $this->updatedAt; + } } diff --git a/tests/CartTest.php b/tests/CartTest.php index 24da3cf..09e3526 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,50 @@ 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()); + + /* Sleep as database does not store ms */ + $beforeStore = Carbon::now(); + 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())); + + /* 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'); + } + /** * @test */