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/werkenbij.komma.pro/app/Komma/Shop/Tests/Unit/CatalogTest.php
<?php

namespace App\Komma\Shop\Tests\Unit;

use App\Komma\Shop\Catalog\Kms\CatalogIndexModel;
use App\Komma\Shop\Catalog\Kms\CatalogService;
use App\Komma\Shop\Catalog\Kms\CatalogServiceInterface;
use App\Komma\Shop\Categories\Models\Category;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\ProductComposite\ProductComposite;
use App\Komma\Shop\Products\ProductGroup\ProductGroup;
use App\Komma\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use App\Komma\Shop\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;

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

    /**
     * @var CatalogServiceInterface $catalogService
     */
    private $catalogService;

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

        $this->catalogService = new CatalogService();
    }

    /**
     * Runs after each test
     */
    public function tearDown(): void
    {
        parent::tearDown();
    }

    /**
     * @group Catalog
     * @test
     */
    public function testCatalogIndexing()
    {
        //Create basic shop things to test
        /** @var Product $product */
        $product = factory(Product::class)->make();
        $this->assertInstanceOf(Product::class, $product);
        /** @var ProductGroup $group */
        $group = factory(ProductGroup::class)->make();
        $this->assertInstanceOf(ProductGroup::class, $group);
        /** @var ProductGroupBehaviour $behaviour */
        $behaviour = factory(ProductGroupBehaviour::class)->make();
        $this->assertInstanceOf(ProductGroupBehaviour::class, $behaviour);
        /** @var ProductComposite $composite */
        $composite = factory(ProductComposite::class)->make();
        $this->assertInstanceOf(ProductComposite::class, $composite);

        //Link and save them
        $product->save();
        $behaviour->save();
        $group->productGroupBehaviour()->associate($behaviour);
        $group->save();
        $group->products()->attach($product);
        $composite->save();
        $composite->groups()->attach($group);

        //Verify that the current created models are not indexed by the catalog
        $productCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $product->id],
            ['catalogable_type', '=', get_class($product)],
        ])->count();

        $groupCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $group->id],
            ['catalogable_type', '=', get_class($group)],
        ])->count();

        $compositeCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $composite->id],
            ['catalogable_type', '=', get_class($composite)],
        ])->count();

        $this->assertEquals(0, $productCatalogIndexModelCount);
        $this->assertEquals(0, $groupCatalogIndexModelCount);
        $this->assertEquals(0, $compositeCatalogIndexModelCount);

        //index them
        $this->catalogService->createIndex();

        //They now exist if everything went well

        //Verify that the current created models are indexed by the catalog
        $productCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $product->id],
            ['catalogable_type', '=', get_class($product)],
        ])->count();

        $groupCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $group->id],
            ['catalogable_type', '=', get_class($group)],
        ])->count();

        $compositeCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $composite->id],
            ['catalogable_type', '=', get_class($composite)],
        ])->count();

        //echo '$productCatalogIndexModelCount: '.$productCatalogIndexModelCount.' $groupCatalogIndexModelCount: '.$groupCatalogIndexModelCount.' $compositeCatalogIndexModelCount: '.$compositeCatalogIndexModelCount.PHP_EOL;

        $this->assertTrue(1 == $productCatalogIndexModelCount);
        $this->assertTrue(1 == $groupCatalogIndexModelCount);
        $this->assertTrue(1 == $compositeCatalogIndexModelCount);
    }

    /**
     * @group Catalog
     * @test
     */
    public function testGettingCatalogItemsByCategoryId()
    {
        //Create basic shop things to test
        /** @var Product $product */
        $category = factory(Category::class)->make();
        /** @var Product $product */
        $product = factory(Product::class)->make();
        /** @var ProductGroup $group */

        //Link and save them
        $category->save();
        $product->save();
        $product->categories()->attach($category->id);
        $product->save();

        //Verify that the current created models are not indexed by the catalog
        $productCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $product->id],
            ['catalogable_type', '=', get_class($product)],
        ])->count();

        $this->assertEquals(0, $productCatalogIndexModelCount);

        //index them
        $this->catalogService->createIndex();

        //They now exist if everything went well
        /** @var CatalogIndexModel $items */
        $items = $this->catalogService->getCatalogItemsByCategoryID($category->id);
        $item = $items->first();
        $this->assertInstanceOf(CatalogIndexModel::class, $item);

        $this->assertEquals($product->id, $item->catalogable_id);
        $this->assertEquals(Product::class, $item->catalogable_type);
        $this->assertEquals($product->title, $item->title);
        $this->assertEquals($product->price, $item->price);
    }

    /**
     * @group Catalog
     * @test
     */
    public function testCatalogCleaning()
    {
        //Create basic shop things to test
        /** @var Product $product */
        $product = factory(Product::class)->make();
        /** @var ProductGroup $group */
        $group = factory(ProductGroup::class)->make();
        /** @var ProductGroupBehaviour $behaviour */
        $behaviour = factory(ProductGroupBehaviour::class)->make();
        /** @var ProductComposite $composite */
        $composite = factory(ProductComposite::class)->make();

        //Link and save them
        $product->save();
        $behaviour->save();
        $group->productGroupBehaviour()->associate($behaviour);
        $group->save();
        $group->products()->attach($product);
        $composite->save();
        $composite->groups()->attach($group);

        //index them
        $this->catalogService->createIndex();

        //They now exist if everything went well. Check that the at least the composite one is indexed
        $compositeCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $composite->id],
            ['catalogable_type', '=', get_class($composite)],
        ])->count();
        $this->assertTrue(1 == $compositeCatalogIndexModelCount);

        //Delete the composite
        $composite->delete();

        //clean the catalog
        $this->catalogService->doHouseKeeping();

        //Verify that the composite index model does not exist anymore
        //They now exist if everything went well. Check that the at least the composite one is indexed
        $compositeCatalogIndexModelCount = CatalogIndexModel::where([
            ['catalogable_id', '=', $composite->id],
            ['catalogable_type', '=', get_class($composite)],
        ])->count();
        $this->assertTrue(0 == $compositeCatalogIndexModelCount);
    }
}