Fix tests

This commit is contained in:
Patrick Henninger
2022-02-06 02:59:32 +01:00
parent 4fdc7a55e3
commit 0aa43abb67
5 changed files with 79 additions and 91 deletions

View File

@@ -525,13 +525,8 @@ class Cart
/** /**
* Set the discount rate for the cart item with the given rowId. * Set the discount rate for the cart item with the given rowId.
*
* @param string $rowId
* @param int|float $taxRate
*
* @return void
*/ */
public function setDiscount(string $rowId, $discount): void public function setDiscount(string $rowId, float|Money $discount): void
{ {
$cartItem = $this->get($rowId); $cartItem = $this->get($rowId);

View File

@@ -180,9 +180,11 @@ class CartItem implements Arrayable, Jsonable
*/ */
public function discount(): Money public function discount(): Money
{ {
$price = $this->price();
if ($this->discount instanceof Money) { if ($this->discount instanceof Money) {
return $this->price()->subtract($this->discount); return $this->price()->subtract($this->discount);
} else { } else {
$result = $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP));
return $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); return $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP));
} }
} }
@@ -193,7 +195,8 @@ class CartItem implements Arrayable, Jsonable
*/ */
public function subtotal(): Money public function subtotal(): Money
{ {
return Money::max(new Money(0, $this->price()->getCurrency()), $this->price()->add($this->discount())); $subtotal = $this->price()->add($this->discount());
return Money::max(new Money(0, $this->price->getCurrency()), $this->price()->subtract($this->discount()));
} }
/** /**
@@ -201,7 +204,8 @@ class CartItem implements Arrayable, Jsonable
*/ */
public function tax(): Money public function tax(): Money
{ {
return $this->subtotal()->multiply($this->taxRate + 1, Config::get('cart.rounding', Money::ROUND_UP)); $tax = $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP));
return $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP));
} }
/** /**

View File

@@ -26,7 +26,7 @@ return [
| |
*/ */
'tax' => 21, 'tax' => 0.21,
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@@ -53,7 +53,7 @@ class CartItemTest extends TestCase
$this->assertJson($cartItem->toJson()); $this->assertJson($cartItem->toJson());
$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","price":"10.00","qty":2,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","subtotal":"20.00","tax":"20.00","total":"40.00"}'; $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","price":"10.00","qty":2,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","subtotal":"20.00","tax":"0.00","total":"20.00"}';
$this->assertEquals($json, $cartItem->toJson()); $this->assertEquals($json, $cartItem->toJson());
} }

View File

@@ -5,7 +5,6 @@ namespace Gloudemans\Tests\Shoppingcart;
use Carbon\Carbon; use Carbon\Carbon;
use Money\Money; use Money\Money;
use Money\Currency; use Money\Currency;
use Gloudemans\Shoppingcart\Calculation\GrossPrice;
use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItem;
use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\CartItemOptions;
@@ -535,7 +534,7 @@ class CartTest extends TestCase
'price' => '10.00', 'price' => '10.00',
'subtotal' => '10.00', 'subtotal' => '10.00',
'tax' => '2.10', 'tax' => '2.10',
'total' => '7.90', 'total' => '12.10',
'options' => [], 'options' => [],
'discount' => '0.00', 'discount' => '0.00',
'weight' => 0, 'weight' => 0,
@@ -549,7 +548,7 @@ class CartTest extends TestCase
'price' => '10.00', 'price' => '10.00',
'subtotal' => '10.00', 'subtotal' => '10.00',
'tax' => '2.10', 'tax' => '2.10',
'total' => '7.90', 'total' => '12.10',
'options' => [], 'options' => [],
'discount' => '0.00', 'discount' => '0.00',
'weight' => 0, 'weight' => 0,
@@ -582,7 +581,7 @@ class CartTest extends TestCase
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'id' => 2, 'id' => 2,
'name' => 'Second item', 'name' => 'Second item',
'price' => '25.00', 'price' => 2500,
]), 2); ]), 2);
$this->assertItemsInCart(3, $cart); $this->assertItemsInCart(3, $cart);
@@ -753,7 +752,7 @@ class CartTest extends TestCase
'name' => 'Some title', 'name' => 'Some title',
]), 1); ]), 1);
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19);
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
@@ -771,7 +770,7 @@ class CartTest extends TestCase
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'id' => 2, 'id' => 2,
'name' => 'Some title', 'name' => 'Some title',
'price' => '20.00', 'price' => 2000,
]), 2); ]), 2);
$this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax()); $this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax());
@@ -786,11 +785,11 @@ class CartTest extends TestCase
'name' => 'Some title', 'name' => 'Some title',
]), 1); ]), 1);
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19);
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$this->assertEquals(19.0, $cartItem->taxRate); $this->assertEquals(0.19, $cartItem->taxRate);
} }
/** @test */ /** @test */
@@ -801,7 +800,7 @@ class CartTest extends TestCase
$cart->add(new BuyableProduct(), 1); $cart->add(new BuyableProduct(), 1);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'id' => 2, 'id' => 2,
'price' => '20.00', 'price' => 2000,
]), 2); ]), 2);
$this->assertEquals(new Money(5000, new Currency('USD')), $cart->subtotal()); $this->assertEquals(new Money(5000, new Currency('USD')), $cart->subtotal());
@@ -943,7 +942,7 @@ class CartTest extends TestCase
/** @test */ /** @test */
public function it_can_calculate_all_values() public function it_can_calculate_all_values()
{ {
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'First item', 'name' => 'First item',
@@ -951,11 +950,11 @@ class CartTest extends TestCase
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount());
$this->assertEquals(new Money(500, new Currency('USD')), $cartItem->subtotal()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal());
$this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax());
$this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total());
} }
@@ -963,7 +962,7 @@ class CartTest extends TestCase
/** @test */ /** @test */
public function it_can_calculate_all_values_after_updating_from_array() public function it_can_calculate_all_values_after_updating_from_array()
{ {
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'First item', 'name' => 'First item',
]), 1); ]), 1);
@@ -972,7 +971,7 @@ class CartTest extends TestCase
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount());
@@ -984,7 +983,7 @@ class CartTest extends TestCase
/** @test */ /** @test */
public function it_can_calculate_all_values_after_updating_from_buyable() public function it_can_calculate_all_values_after_updating_from_buyable()
{ {
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'First item', 'name' => 'First item',
'price' => '5.00', 'price' => '5.00',
@@ -996,7 +995,7 @@ class CartTest extends TestCase
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount());
@@ -1031,7 +1030,7 @@ class CartTest extends TestCase
]), 2); ]), 2);
$cart->setGlobalTax(0); $cart->setGlobalTax(0);
$cart->setGlobalDiscount(50); $cart->setGlobalDiscount(0.5);
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
@@ -1047,7 +1046,7 @@ class CartTest extends TestCase
Event::fake(); Event::fake();
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'Item', 'name' => 'Item',
]), 1); ]), 1);
@@ -1086,7 +1085,7 @@ class CartTest extends TestCase
'--database' => 'testing', '--database' => 'testing',
]); ]);
Event::fake(); Event::fake();
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'Item', 'name' => 'Item',
]), 1); ]), 1);
@@ -1101,12 +1100,11 @@ class CartTest extends TestCase
/** @test */ /** @test */
public function cart_can_calculate_all_values() public function cart_can_calculate_all_values()
{ {
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'First item', 'name' => 'First item',
]), 1); ]), 1);
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19);
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19);
$this->assertEquals(new Money(1000, new Currency('USD')), $cart->price()); $this->assertEquals(new Money(1000, new Currency('USD')), $cart->price());
$this->assertEquals(new Money(500, new Currency('USD')), $cart->discount()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->discount());
$this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal());
@@ -1114,17 +1112,6 @@ class CartTest extends TestCase
$this->assertEquals(new Money(595, new Currency('USD')), $cart->total()); $this->assertEquals(new Money(595, new Currency('USD')), $cart->total());
} }
/** @test */
public function can_access_cart_item_propertys()
{
$cart = $this->getCartDiscount(50);
$cart->add(new BuyableProduct([
'name' => 'First item',
]), 1);
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$this->assertEquals(50, $cartItem->discount);
}
/** @test */ /** @test */
public function can_set_cart_item_discount() public function can_set_cart_item_discount()
{ {
@@ -1133,8 +1120,10 @@ class CartTest extends TestCase
'name' => 'First item', 'name' => 'First item',
]), 1); ]), 1);
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50);
$this->assertEquals(50, $cartItem->discount); $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 0.5);
$this->assertEquals(0.5, $cartItem->discount);
} }
/** @test */ /** @test */
@@ -1182,7 +1171,7 @@ class CartTest extends TestCase
/** @test */ /** @test */
public function cart_can_create_items_from_models_using_the_canbebought_trait() public function cart_can_create_items_from_models_using_the_canbebought_trait()
{ {
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProductTrait([ $cart->add(new BuyableProductTrait([
'name' => 'First item', 'name' => 'First item',
@@ -1190,7 +1179,7 @@ class CartTest extends TestCase
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price);
$this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount());
@@ -1220,7 +1209,7 @@ class CartTest extends TestCase
'--database' => 'testing', '--database' => 'testing',
]); ]);
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'Item', 'name' => 'Item',
]), 1); ]), 1);
@@ -1255,7 +1244,7 @@ class CartTest extends TestCase
'--database' => 'testing', '--database' => 'testing',
]); ]);
$cart = $this->getCartDiscount(50); $cart = $this->getCartDiscount(0.5);
$cart->add(new BuyableProduct([ $cart->add(new BuyableProduct([
'name' => 'Item', 'name' => 'Item',
]), 1); ]), 1);
@@ -1282,48 +1271,6 @@ class CartTest extends TestCase
}); });
} }
/**
* Get an instance of the cart.
*
* @return \Gloudemans\Shoppingcart\Cart
*/
private function getCart()
{
$session = $this->app->make('session');
$events = $this->app->make('events');
return new Cart($session, $events);
}
/**
* Get an instance of the cart with discount.
*
* @param int $discount
*
* @return \Gloudemans\Shoppingcart\Cart
*/
private function getCartDiscount($discount = 50)
{
$cart = $this->getCart();
$cart->setGlobalDiscount($discount);
return $cart;
}
/**
* Set the config number format.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*/
private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator)
{
$this->app['config']->set('cart.format.decimals', $decimals);
$this->app['config']->set('cart.format.decimal_point', $decimalPoint);
$this->app['config']->set('cart.format.thousand_separator', $thousandSeperator);
}
/** @test */ /** @test */
public function it_can_store__mutiple_instances_of_the_cart_in_a_database() public function it_can_store__mutiple_instances_of_the_cart_in_a_database()
{ {
@@ -1391,4 +1338,46 @@ class CartTest extends TestCase
Event::assertDispatched('cart.erased'); Event::assertDispatched('cart.erased');
$this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE]); $this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE]);
} }
/**
* Get an instance of the cart.
*
* @return \Gloudemans\Shoppingcart\Cart
*/
private function getCart()
{
$session = $this->app->make('session');
$events = $this->app->make('events');
return new Cart($session, $events);
}
/**
* Get an instance of the cart with discount.
*
* @param int $discount
*
* @return \Gloudemans\Shoppingcart\Cart
*/
private function getCartDiscount(float $discount = 0.5)
{
$cart = $this->getCart();
$cart->setGlobalDiscount($discount);
return $cart;
}
/**
* Set the config number format.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
*/
private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator)
{
$this->app['config']->set('cart.format.decimals', $decimals);
$this->app['config']->set('cart.format.decimal_point', $decimalPoint);
$this->app['config']->set('cart.format.thousand_separator', $thousandSeperator);
}
} }