HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/Lacom/lacom.nl/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));
    }
}