File: D:/HostingSpaces/SBogers10/douven.komma.pro/app/KommaApp/Shop/Tests/Unit/ShoppingCartTest.php
<?php
namespace App\KommaApp\Shop\Tests\Unit;
use App\KommaApp\Shop\Cart\ShoppingCartItem;
use App\KommaApp\Shop\Cart\ShoppingCartService;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ShoppingCartTest extends TestCase
{
use DatabaseTransactions; //Automatically rolls back database actions after tests
/**
* @var ShoppingCartService $shoppingCart
*/
private $shoppingCart;
/**
* Runs before each test
*/
public function setup()
{
parent::setup();
$this->shoppingCart = new ShoppingCartService();
}
///////////////////////////////////////////////
// Tests without price modification
///////////////////////////////////////////////
/**
* Runs after each test
* @group ShoppingCart
*/
public function teardown()
{
parent::tearDown();
}
/**
* @test
*/
public function testAddSingleProductAndGetItems()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$this->assertCount(1, $this->shoppingCart->getItems());
}
/**
* @test
* @group ShoppingCart
*/
public function testGetItemsCount()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$this->assertEquals(3, $this->shoppingCart->getItemsCount(false, false));
}
/**
* @test
* @group ShoppingCart
*/
public function testGetUniqueItemsCount()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$this->assertEquals(1, $this->shoppingCart->getItemsCount(true, false));
}
/**
* @test
* @group ShoppingCart
*/
public function testDeleteProduct()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
/** @var Product $productToDelete */
$productToDelete = factory(Product::class)->create();
/** @var Product $productToDelete */
$shoppingCartItemToDelete = $this->shoppingCart->addProduct($productToDelete->id);
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$this->assertEquals(4, count($this->shoppingCart->getItems()));
$this->shoppingCart->deleteItem($shoppingCartItemToDelete->getId());
$this->assertEquals(3, count($this->shoppingCart->getItems()));
}
/**
* @test
* @group ShoppingCart
*/
public function testClearCart()
{
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$product = factory(Product::class)->create();
$this->shoppingCart->addProduct($product->id);
$this->assertCount(2, $this->shoppingCart->getItems());
$this->shoppingCart->clear();
$this->assertEquals(0, count($this->shoppingCart->getItems()));
}
/**
* @test
* @group ShoppingCart
*/
public function testGetPrice()
{
$expectedPrice = 0;
/** @var Product $product */
$product = factory(Product::class)->create();
$expectedPrice += $product->price;
$this->shoppingCart->addProduct($product->id);
$product = factory(Product::class)->create();
$expectedPrice += $product->price;
$this->shoppingCart->addProduct($product->id);
$this->assertEquals($expectedPrice, $this->shoppingCart->getTotal(false));
}
}