File: D:/HostingSpaces/SBogers10/shop.komma.nl/tests/Unit/StockTest.php
<?php
//TODO Update when shop v1 is ready
//
//namespace App\Tests\Unit;
//
//use App\Addresses\Models\Address;
//use App\Cart\ShoppingCartServiceInterface;
//use App\Checkout\CheckoutServiceInterface;
//use App\Orders\Product\OrderedProduct;
//use App\Products\Product\Product;
//use App\Products\ProductableInterface;
//use App\Stock\StockService;
//use Tests\TestCase;
//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);
// }
// }
//}