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/SBogers10/inzigd.komma.pro/app/Komma/Shop/Tests/Unit/CheckoutTest.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\Checkout\CheckoutServiceInterface;
use App\Komma\Shop\Orders\Kms\OrderMailServiceInterface;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedCustomer;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedStaff;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\Product\OrderedProduct;
use App\Komma\Shop\Orders\ProductComposite\OrderedProductComposite;
use App\Komma\Shop\Orders\ProductGroup\OrderedProductGroup;
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\Tests\TestCase;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Models\Role;
use App\Komma\Users\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;

class CheckoutTest extends TestCase
{
    use DatabaseTransactions; //Automatically rolls back database actions after tests

    /** @var ShoppingCartService $shoppingCart */
    private $shoppingCart;

    /** @var CheckoutServiceInterface $checkoutService */
    private $checkoutService;

    /** @var OrderMailServiceInterface $orderMailService */
    private $orderMailService;

    /**
     * Runs before each test
     */
    public function setup(): void
    {
        parent::setup();

        /** @var SiteServiceInterface $siteService */
        $siteService = app(SiteServiceInterface::class); //Singleton
        $siteService->setCurrentSiteToDefault();

        $this->shoppingCart = new ShoppingCartService();
        $this->checkoutService = app(CheckoutServiceInterface::class);
        $this->orderMailService = app(OrderMailServiceInterface::class);
    }

    /**
     * @test
     * @group Checkout
     * @throws \Throwable
     */
    public function testCheckoutOrderCreationProcessWithProductables()
    {
        //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);
//        echo 'Productables that wil be ordered: '.PHP_EOL;
//        $allProductables->each(function(ProductableInterface $productable) {
//            echo get_class($productable) .' => '. $productable->id.PHP_EOL;
//        });
//        echo PHP_EOL;

//        echo 'Productables in shopping cart: '.PHP_EOL;
        $allProductables->each(function(ProductableInterface $productable) {
            /** @var ShoppingCartItem $shoppingCartItem */
            $shoppingCartItem = $this->shoppingCart->addProductable($productable, mt_rand(1, 3));
//            echo get_class($shoppingCartItem->getProductable()) .' => '. $shoppingCartItem->getProductable()->id.' ('.$shoppingCartItem->getItemsCount().' pc(s))'.PHP_EOL;
        });
//        echo PHP_EOL;

        $order = $this->checkoutService->createOrderFromShoppingCartItems($this->shoppingCart->getItems(), $customer, $customer->addresses()->first(), $customer->addresses()->first());
        $this->assertInstanceOf(Order::class, $order);

        //Check that the created order is related to the customer and vice versa
        $this->assertEquals($order->customer()->first()->id, $customer->id);
        $orderFromCustomer = $customer->orders()->where('id', '=', $order->id)->first();
        $this->assertEquals($order->id, $orderFromCustomer->id);

        //Check that the product ids are in the orderedProduct product id's
//        echo 'Products for order: '.PHP_EOL;
        $productIds = $products->pluck('id');
        $orderedProductProductIds = $order->orderedProducts()->with('product')->get()->map(function(OrderedProduct $orderOrderedProduct) {
//            echo get_class($orderOrderedProduct->product) .' => '. $orderOrderedProduct->product->id.PHP_EOL;
            return $orderOrderedProduct->product->id;
        });
//        echo PHP_EOL;
        $productIds->each(function(int $id) use ($orderedProductProductIds) {
            $this->assertTrue($orderedProductProductIds->search($id) !== false); //Check that the product id is in the collection of OrderedProduct product ids
        });

        //Check that the product group ids are in the orderedProductGroups productgroup id's
//        echo 'Product groups for order: '.PHP_EOL;
        $productGroupIds = $productGroups->pluck('id');
//        echo $productGroupIds.PHP_EOL;
//        echo 'Order ordered product groups count: '.$order->orderedProductGroups()->count().PHP_EOL;
        $orderedProductGroupIds = $order->orderedProductGroups()->with('productGroup')->get()->map(function(OrderedProductGroup $orderOrderedProductGroup) {
//            echo get_class($orderOrderedProductGroup->productGroup) .' => '. $orderOrderedProductGroup->productGroup->id.PHP_EOL;
            return $orderOrderedProductGroup->productGroup->id;
        });
//        echo $orderedProductGroupIds.PHP_EOL;
//        echo PHP_EOL;
        $productGroupIds->each(function(int $id) use ($orderedProductGroupIds) {
            $this->assertTrue($orderedProductGroupIds->search($id) !== false); //Check that the productGroupId is in the collection of OrderedProductGroup ids
        });

        //Check that the product composite ids are in the orderedProductComposite productComposite id's
//        echo 'Product composites for order: '.PHP_EOL;
        $productCompositeIds = $productComposites->pluck('id');
        $orderedProductCompositeIds = $order->orderedProductComposites()->with('productComposite')->get()->map(function(OrderedProductComposite $orderOrderedProductComposite) {
//            echo get_class($orderOrderedProductComposite->productComposite) .' => '. $orderOrderedProductComposite->productComposite->id.PHP_EOL;
            return $orderOrderedProductComposite->productComposite->id;
        });
//        echo PHP_EOL;
        $productCompositeIds->each(function(int $id) use ($orderedProductCompositeIds) {
            $this->assertTrue($orderedProductCompositeIds->search($id) !== false); //Check that the productGroupId is in the collection of OrderedProductGroup ids
        });

        //Check if customers address attributes are in the order itself (to preserve history)
        $this->assertEquals($customer->first_name, $order->customer->first_name);
        $this->assertEquals($customer->last_name, $order->customer->last_name);
        $this->assertEquals($customer->email, $order->customer->email);

        $this->assertEquals($customer->addresses[0]->postal_code, $order->invoice_postal_code);
        $this->assertEquals($customer->addresses[0]->city, $order->invoice_city);
        $this->assertEquals($customer->addresses[0]->street, $order->invoice_street);
        $this->assertEquals($customer->addresses[0]->house_number, $order->invoice_house_number);
        $this->assertEquals($customer->addresses[0]->telephone, $order->invoice_telephone);

        $this->assertEquals($customer->addresses[0]->postal_code, $order->shipping_postal_code);
        $this->assertEquals($customer->addresses[0]->city, $order->shipping_city);
        $this->assertEquals($customer->addresses[0]->street, $order->shipping_street);
        $this->assertEquals($customer->addresses[0]->house_number, $order->shipping_house_number);
        $this->assertEquals($customer->addresses[0]->telephone, $order->shipping_telephone);
    }

    /**
     * @test
     * @group Checkout
     * @throws \Throwable
     */
    public function testCheckoutMails()
    {
        Mail::fake();

        /** @var User $customer */
        $customer = factory(User::class)->create();
        $customer->role()->associate(Role::whereValue(Role::Customer)->first());
        $address = factory(Address::class)->create();
        $customer->addresses()->save($address);

        /** @var User $customer */
        $staff = factory(User::class, 3)->create();
        $staff->each(function(User $staffMember) {
            $staffMember->role()->associate(Role::whereValue(Role::Admin)->first());
            $staffMember->save();
            $address = factory(Address::class)->create();
            $staffMember->addresses()->save($address);
        });

        $this->shoppingCart->addProductable(ProductComposite::inRandomOrder()->take(1)->first());

        $address = $customer->addresses()->first();
        $order = $this->checkoutService->createOrderFromShoppingCartItems($this->shoppingCart->getItems(), $customer, $address, $address);

        $this->orderMailService->mailStaffAboutCurrentOrderStatus($order);
        $staffEmails = $staff->pluck('email');

        Mail::assertSent(OrderStatusUpdatedStaff::class, function (Mailable $mail) use ($staffEmails){
            $found = false;
            foreach($staffEmails as $staffEmail)
            {
                if($mail->hasTo($staffEmail)) {
                    $found = true;
                    break;
                }
            }

//            echo 'An OrderStatusUpdatedStaff mail '.(($found) ? 'was': 'was not').' queued for a staff member with e-mail: '.$staffEmail.PHP_EOL;
            return $found;
        });

        $this->orderMailService->mailCustomerAboutCurrentOrderStatus($order, $customer);


        Mail::assertSent(OrderStatusUpdatedCustomer::class, function (Mailable $mail) use ($customer){
            $found = $mail->hasTo($customer->email);
//            echo 'An OrderStatusUpdatedCustomer mail '.(($found) ? 'was': 'was not').' queued for a customer with e-mail: '.$customer->email.PHP_EOL;
            return $found;
        });
    }
}