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/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/Shop/Tests/Unit/CatalogTest.php
<?php

namespace App\KommaApp\Shop\Tests\Unit;

use App\KommaApp\Shop\Catalog\Kms\CatalogIndexModel;
use App\KommaApp\Shop\Catalog\Kms\CatalogService;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Products\ProductComposite\ProductComposite;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroup;
use App\KommaApp\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use App\KommaApp\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()
    {
        parent::setup();

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

    ///////////////////////////////////////////////
    // Tests without price modification
    ///////////////////////////////////////////////

    /**
     * Runs after each test
     */
    public function teardown()
    {
        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 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();

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

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

    /**
     * @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);
    }
}