File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Shop/Tests/Unit/StockTest.php
<?php
namespace App\Komma\Shop\Tests\Unit;
use App\Komma\Addresses\Models\Address;
use App\Komma\Shop\Cart\ShoppingCartServiceInterface;
use App\Komma\Shop\Checkout\CheckoutServiceInterface;
use App\Komma\Shop\Orders\Product\OrderedProduct;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\ProductableInterface;
use App\Komma\Shop\Stock\StockService;
use App\Komma\Shop\Tests\TestCase;
use App\Komma\Users\Models\SiteUser;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Collection;
class StockTest extends TestCase
{
use DatabaseTransactions; //Automatically rolls back database actions after tests
/**
* @var StockService $catalogService
*/
private $stockService;
/**
* Runs before each test
*/
public function setUp(): void
{
parent::setUp();
$this->stockService = new StockService();
}
/**
* Runs after each test
*/
public function tearDown(): void
{
parent::tearDown();
}
/**
* @group Stock
* @test
*/
public function testAddingStock()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$stockBefore = $product->stock;
$this->stockService->stockProductable($product);
$this->assertEquals($stockBefore + 1, $product->stock);
}
/**
* @group Stock
* @test
*/
public function testAddingMoreStock()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$stockBefore = $product->stock;
$this->stockService->stockProductable($product, 3);
$this->assertEquals($stockBefore + 3, $product->stock);
}
/**
* @group Stock
* @test
*/
public function testDeletingStock()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$stockBefore = $product->stock;
$this->stockService->stockProductable($product, -1);
$this->assertEquals($stockBefore - 1, $product->stock);
}
/**
* @group Stock
* @test
*/
public function testDeletingMoreStock()
{
/** @var Product $product */
$product = factory(Product::class)->create();
$stockBefore = $product->stock;
$this->stockService->stockProductable($product, -3);
$this->assertEquals($stockBefore - 3, $product->stock);
}
/**
* @group Stock
* @test
*/
public function testStockDepletionWhenCheckingOut()
{
/** @var CheckoutServiceInterface $checkoutService */
$checkoutService = app(CheckoutServiceInterface::class);
/** @var ShoppingCartServiceInterface $shoppingCart */
$shoppingCart = app(ShoppingCartServiceInterface::class);
//Get a random customer user to test with
/** @var SiteUser $customer */
$customer = factory(SiteUser::class)->create();
$this->assertInstanceOf(SiteUser::class, $customer);
//Give it an address
/** @var Address $address */
$address = factory(Address::class)->create();
$customer->addresses()->save($address);
//Put some random products in the shopping cart and keep track of their stock before the order was placed and the quantity that needs to be ordered.
/** @var Collection $products */
$products = factory(Product::class, 3)->create();
$productsInformation = [];
$productsInformation[$products[0]->id] = ['stock_before' => $products[0]->stock, 'order_quantity' => 1];
$productsInformation[$products[1]->id] = ['stock_before' => $products[1]->stock, 'order_quantity' => 2];
$productsInformation[$products[2]->id] = ['stock_before' => $products[2]->stock, 'order_quantity' => 3];
foreach($products as $product) $shoppingCart->addProductable($product, $productsInformation[$product->id]['order_quantity']);
//Place the order. The products quantity
$order = $checkoutService->createOrder($shoppingCart, $customer, $customer->addresses()->first(), $customer->addresses()->first());
//Check that the stock of the products is less then before.
foreach($products as $product) {
/** @var Product $product */
$this->assertNotEquals($productsInformation[$product->id]['stock_before'], $product->stock);
$this->assertEquals($productsInformation[$product->id]['stock_before'] - $productsInformation[$product->id]['order_quantity'], $product->stock);
}
}
}