mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
manuall readded reverted changes
This commit is contained in:
@@ -22,7 +22,7 @@ class CartItemTest extends TestCase
|
||||
/** @test */
|
||||
public function it_can_be_cast_to_an_array()
|
||||
{
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, 550, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem->setQuantity(2);
|
||||
|
||||
$this->assertEquals([
|
||||
@@ -37,19 +37,20 @@ class CartItemTest extends TestCase
|
||||
],
|
||||
'tax' => 0,
|
||||
'subtotal' => 20.00,
|
||||
'discount' => 0.0
|
||||
'discount' => 0.0,
|
||||
'weight' => 550.0
|
||||
], $cartItem->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_be_cast_to_json()
|
||||
{
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, 550, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem->setQuantity(2);
|
||||
|
||||
$this->assertJson($cartItem->toJson());
|
||||
|
||||
$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10,"options":{"size":"XL","color":"red"},"discount":0,"tax":0,"subtotal":20}';
|
||||
$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10,"weight":550,"options":{"size":"XL","color":"red"},"discount":0,"tax":0,"subtotal":20}';
|
||||
|
||||
$this->assertEquals($json, $cartItem->toJson());
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ class CartTest extends TestCase
|
||||
|
||||
$cart = $this->getCart();
|
||||
|
||||
$cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => 10.00]);
|
||||
$cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => 10.00, 'weight' => 550]);
|
||||
|
||||
$this->assertEquals(1, $cart->count());
|
||||
|
||||
@@ -181,8 +181,8 @@ class CartTest extends TestCase
|
||||
$cart = $this->getCart();
|
||||
|
||||
$cart->add([
|
||||
['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => 10.00],
|
||||
['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => 10.00]
|
||||
['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => 10.00, 'weight' => 550],
|
||||
['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => 10.00, 'weight' => 550]
|
||||
]);
|
||||
|
||||
$this->assertEquals(2, $cart->count());
|
||||
@@ -488,7 +488,8 @@ class CartTest extends TestCase
|
||||
'tax' => 2.10,
|
||||
'subtotal' => 10.0,
|
||||
'options' => [],
|
||||
'discount' => 0.0
|
||||
'discount' => 0.0,
|
||||
'weight' => 0.0
|
||||
],
|
||||
'370d08585360f5c568b18d1f2e4ca1df' => [
|
||||
'rowId' => '370d08585360f5c568b18d1f2e4ca1df',
|
||||
@@ -499,7 +500,8 @@ class CartTest extends TestCase
|
||||
'tax' => 2.10,
|
||||
'subtotal' => 10.0,
|
||||
'options' => [],
|
||||
'discount' => 0.0
|
||||
'discount' => 0.0,
|
||||
'weight' => 0.0
|
||||
]
|
||||
], $content->toArray());
|
||||
}
|
||||
@@ -997,6 +999,75 @@ class CartTest extends TestCase
|
||||
$this->assertEquals(10, $cart3->totalFloat());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_cant_merge_non_existing_cart()
|
||||
{
|
||||
$this->artisan('migrate', [
|
||||
'--database' => 'testing',
|
||||
]);
|
||||
Event::fake();
|
||||
$cart = $this->getCartDiscount(50);
|
||||
$cart->add( new BuyableProduct(1, 'Item', 10.00), 1);
|
||||
$cart->add( new BuyableProduct(2, 'Item 2', 10.00), 1);
|
||||
$this->assertEquals(false, $cart->merge('doesNotExist'));
|
||||
$this->assertEquals(2, $cart->countInstances());
|
||||
}
|
||||
/** @test */
|
||||
public function cart_can_calculate_all_values()
|
||||
{
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19);
|
||||
$this->assertEquals('10.00', $cart->initial(2));
|
||||
$this->assertEquals(10.00, $cart->initialFloat());
|
||||
$this->assertEquals('5.00', $cart->discount(2));
|
||||
$this->assertEquals(5.00, $cart->discountFloat());
|
||||
$this->assertEquals('5.00', $cart->subtotal(2));
|
||||
$this->assertEquals(5.00, $cart->subtotalFloat());
|
||||
$this->assertEquals('0.95', $cart->tax(2));
|
||||
$this->assertEquals(0.95, $cart->taxFloat());
|
||||
$this->assertEquals('5.95', $cart->total(2));
|
||||
$this->assertEquals(5.95, $cart->totalFloat());
|
||||
}
|
||||
/** @test */
|
||||
public function can_access_cart_item_propertys()
|
||||
{
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$this->assertEquals(50, $cartItem->discountRate);
|
||||
}
|
||||
/** @test */
|
||||
public function cant_access_non_existant_propertys()
|
||||
{
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$this->assertEquals(null, $cartItem->doesNotExist);
|
||||
$this->assertEquals(null, $cart->doesNotExist);
|
||||
}
|
||||
/** @test */
|
||||
public function can_set_cart_item_discount()
|
||||
{
|
||||
$cart = $this->getCart();
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50);
|
||||
$this->assertEquals(50, $cartItem->discountRate);
|
||||
}
|
||||
/** @test */
|
||||
public function can_set_cart_item_weight_and_calculate_total_weight()
|
||||
{
|
||||
$cart = $this->getCart();
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00, 250), 2);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50);
|
||||
$this->assertEquals('500.00', $cart->weight(2));
|
||||
$this->assertEquals(500.00, $cart->weightFloat());
|
||||
$this->assertEquals(500.00, $cartItem->weightTotal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the cart.
|
||||
*
|
||||
|
||||
@@ -21,6 +21,11 @@ class BuyableProduct implements Buyable
|
||||
*/
|
||||
private $price;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* BuyableProduct constructor.
|
||||
*
|
||||
@@ -28,11 +33,12 @@ class BuyableProduct implements Buyable
|
||||
* @param string $name
|
||||
* @param float $price
|
||||
*/
|
||||
public function __construct($id = 1, $name = 'Item name', $price = 10.00)
|
||||
public function __construct($id = 1, $name = 'Item name', $price = 10.00, $weight = 0)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = $price;
|
||||
$this->weight = $weight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,4 +70,14 @@ class BuyableProduct implements Buyable
|
||||
{
|
||||
return $this->price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price of the Buyable item.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getBuyableWeight($options = null)
|
||||
{
|
||||
return $this->weight;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user