diff --git a/database/migrations/0000_00_00_000000_create_shoppingcart_table.php b/database/migrations/2018_12_23_120000_create_shoppingcart_table.php similarity index 100% rename from database/migrations/0000_00_00_000000_create_shoppingcart_table.php rename to database/migrations/2018_12_23_120000_create_shoppingcart_table.php diff --git a/src/Cart.php b/src/Cart.php index c3ee405..8df2a57 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -459,8 +459,10 @@ class Cart public function setGlobalTax($taxRate) { $this->taxRate = $taxRate; - if ($this->content && $this->content->count()) { - $this->content->each(function ($item, $key) { + + $content = $this->getContent(); + if ($content && $content->count()) { + $content->each(function ($item, $key) { $item->setTaxRate($this->taxRate); }); } @@ -495,8 +497,10 @@ class Cart public function setGlobalDiscount($discount) { $this->discount = $discount; - if ($this->content && $this->content->count()) { - $this->content->each(function ($item, $key) { + + $content = $this->getContent(); + if ($content && $content->count()) { + $content->each(function ($item, $key) { $item->setDiscountRate($this->discount); }); } diff --git a/src/ShoppingcartServiceProvider.php b/src/ShoppingcartServiceProvider.php index 979f579..691df87 100644 --- a/src/ShoppingcartServiceProvider.php +++ b/src/ShoppingcartServiceProvider.php @@ -29,13 +29,8 @@ class ShoppingcartServiceProvider extends ServiceProvider } }); - if ( ! class_exists('CreateShoppingcartTable')) { - // Publish the migration - $timestamp = date('Y_m_d_His', time()); - - $this->publishes([ - __DIR__.'/../database/migrations/0000_00_00_000000_create_shoppingcart_table.php' => database_path('migrations/'.$timestamp.'_create_shoppingcart_table.php'), - ], 'migrations'); - } + $this->publishes([ + realpath(__DIR__.'/../database/migrations') => $this->app->databasePath().'/migrations' + ]); } } diff --git a/tests/CartTest.php b/tests/CartTest.php index 6fa4d36..213ab84 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -920,6 +920,47 @@ class CartTest extends TestCase event(new Logout(\Auth::guard('web'), $user)); } + /** @test */ + public function can_change_tax_globally() + { + $cart = $this->getCart(); + + $cart->add( new BuyableProduct(1, 'Item', 10.00), 2); + + $cart->setGlobalTax(0); + + $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); + + $this->assertEquals('20.00', $cartItem->total(2)); + } + + /** @test */ + public function can_change_discount_globally() + { + $cart = $this->getCart(); + + $cart->add( new BuyableProduct(1, 'Item', 10.00), 2); + + $cart->setGlobalTax(0); + $cart->setGlobalDiscount(50); + + $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); + + $this->assertEquals('10.00', $cartItem->total(2)); + } + + /** @test */ + public function cart_hast_no_rounding_errors() + { + $cart = $this->getCart(); + + $cart->add( new BuyableProduct(1, 'Item', 10.004), 2); + + $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); + + $this->assertEquals('24.21', $cartItem->total(2)); + } + /** * Get an instance of the cart. *