File: D:/HostingSpaces/SBogers10/inzigd.komma.pro/app/Komma/Shop/Tests/Unit/ShipmentTest.php
<?php
namespace App\Komma\Shop\Tests\Unit;
use App\Komma\Addresses\Models\Address;
use App\Komma\Shop\Cart\ShoppingCartItem;
use App\Komma\Shop\Cart\ShoppingCartService;
use App\Komma\Shop\Cart\ShoppingCartServiceInterface;
use App\Komma\Shop\Checkout\CheckoutServiceInterface;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\ProductableInterface;
use App\Komma\Shop\Products\ProductComposite\ProductComposite;
use App\Komma\Shop\Products\ProductGroup\ProductGroup;
use App\Komma\Shop\ShipmentGroups\ShipmentGroup;
use App\Komma\Shop\ShipmentGroups\ShipmentGroupService;
use App\Komma\Shop\ShipmentGroups\ShipmentGroupStatus;
use App\Komma\Shop\Shipments\Shipment;
use App\Komma\Shop\Shipments\ShipmentService;
use App\Komma\Shop\Shipments\ShipmentStatus;
use App\Komma\Shop\Tests\TestCase;
use App\Komma\Users\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ShipmentTest extends TestCase
{
use DatabaseTransactions; //Automatically rolls back database actions after tests
/** @var ShoppingCartServiceInterface */
private $shoppingCart;
/** @var CheckoutServiceInterface $checkoutService */
private $checkoutService;
/**
* @group Shipment
* @test
*/
public function testShipmentAndGroupCreation()
{
$shipmentService = new ShipmentService();
$shipmentGroupService = new ShipmentGroupService();
$this->shoppingCart = new ShoppingCartService();
$this->checkoutService = app(CheckoutServiceInterface::class);
//Get a random customer user to test with
/** @var User $customer */
$customer = factory(User::class)->create();
$this->assertInstanceOf(User::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
$products = Product::inRandomOrder()->take(3)->get();
$this->assertCount(3, $products);
//Put some random product groups in the shopping cart
$productGroups = ProductGroup::inRandomOrder()->take(2)->get();
$this->assertCount(2, $productGroups);
//Put some random product composites in the shopping cart
$productComposites = ProductComposite::inRandomOrder()->take(1)->get();
$this->assertCount(1, $productComposites);
//Build an order
$allProductables = collect();
$allProductables = $allProductables->merge($products);
$allProductables = $allProductables->merge($productGroups);
$allProductables = $allProductables->merge($productComposites);
$allProductables->each(function(ProductableInterface $productable) {
/** @var ShoppingCartItem $shoppingCartItem */
$this->shoppingCart->addProductable($productable, mt_rand(1, 3));
});
//Create an order from the items in the shoppingCart
$order = $this->checkoutService->createOrderFromShoppingCartItems($this->shoppingCart->getItems(), $customer, $customer->addresses()->first(), $customer->addresses()->first());
//Create a shipment from an order
$shipment = $shipmentService->makeForOrder($order, '3S'.mt_rand(1000, 9999), 'Part x will arrive later as it is out of stock.');
$shipment->save();
$this->assertInstanceOf(Shipment::class, $shipment);
$this->assertTrue($shipment->exists);
$this->assertTrue($shipment->status == ShipmentStatus::NEW);
//Create a group from that order
$shipmentGroup = $shipmentGroupService->createForShipment($shipment);
$this->assertInstanceOf(ShipmentGroup::class, $shipmentGroup);
$this->assertTrue($shipmentGroup->status == ShipmentGroupStatus::PENDING);
$this->assertTrue($shipmentGroup->shipments()->count() == 1);
$this->assertTrue($order->shipments()->count() == 1);
}
}