mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
Merge pull request #3 from bumbummen99/fix_cart_database_timestamps
Fix cart database timestamps
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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",
|
||||
|
||||
40
src/Cart.php
40
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user