File: D:/HostingSpaces/SBogers10/zipwire.komma.pro/app/KommaApp/Shop/Tests/Unit/ProductRelatedTest.php
<?php
namespace App\KommaApp\Shop\Tests\Unit;
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;
use Illuminate\Support\Collection;
class ProductRelatedTest extends TestCase
{
use DatabaseTransactions; //Automatically rolls back database actions after tests
/**
* This method is being run before each test
*/
protected function setUp()
{
parent::setUp();
}
/*
|--------------------------------------------------------------------------
| Basic Instantiation and Database Tests
|--------------------------------------------------------------------------
*/
public function testProductCreation()
{
$instance = factory(Product::class)->create();
$this->assertInstanceOf(Product::class, $instance);
$this->assertDatabaseHas('products', [
'title' => $instance->title,
'price' => $instance->price,
'stock_keeping_unit' => $instance->stock_keeping_unit,
]);
}
public function testProductGroupBehaviourCreation()
{
$instance = factory(ProductGroupBehaviour::class)->create();
$this->assertInstanceOf(ProductGroupBehaviour::class, $instance);
$this->assertDatabaseHas('product_group_behaviours', [
'title' => $instance->title,
]);
}
public function testProductCompositeCreation()
{
$this->assertTrue(true);
$instance = factory(ProductComposite::class)->create();
$this->assertInstanceOf(ProductComposite::class, $instance);
$this->assertDatabaseHas('product_composites', [
'title' => $instance->title,
]);
}
/*
|--------------------------------------------------------------------------
| Relation Tests
|--------------------------------------------------------------------------
*/
public function testMultipleProductsLinkingToGroup()
{
/** @var Collection $products */
$products = factory(Product::class, 3)->make();
/** @var ProductGroupBehaviour $behaviour */
$behaviour = factory(ProductGroupBehaviour::class)->make();
//We need to save it because only then it will have an id and it can be associated with a product group
$behaviour->save();
/** @var ProductGroup $group */
$group = factory(ProductGroup::class)->make();
//We cannot have a ProductGroup without behaviour so we need to associate an existing ProductGroupBehaviour instance with it
$group->productGroupBehaviour()->associate($behaviour);
//We need to save the group first to give it an id. Then we can add products to it.
$group->save();
$group->products()->saveMany($products);
$products->each(function($product) use ($group) {
$this->assertDatabaseHas('product_product_group', [
'product_id' => $product->id,
'product_group_id' => $group->id
]);
});
$this->assertCount(3, $group->products()->get());
}
public function testMultipleGroupLinkingAndRetrievalFromProduct()
{
/** @var Collection $behaviours */
$behaviours = factory(ProductGroupBehaviour::class, 2)->make();
//We need to save them because only then it will have an id and it can be associated with a product group
$behaviours->each(function($behaviour) {
/** @var ProductGroupBehaviour $behaviour */
$behaviour->save();
});
/** @var ProductGroup $group */
$group1 = factory(ProductGroup::class)->make();
//We cannot have a ProductGroup without behaviour so we need to associate an existing ProductGroupBehaviour instance with it
$group1->productGroupBehaviour()->associate($behaviours[0]);
/** @var ProductGroup $group */
$group2 = factory(ProductGroup::class)->make();
//We cannot have a ProductGroup without behaviour so we need to associate an existing ProductGroupBehaviour instance with it
$group2->productGroupBehaviour()->associate($behaviours[1]);
/** @var Product $product */
$product = factory(Product::class)->create();
$product->groups()->save($group1);
$product->groups()->save($group2);
$this->assertEquals(2, $product->groups()->get()->count());
}
public function testBehaviourLinkingToGroup()
{
/** @var ProductGroup $group */
$group = factory(ProductGroup::class)->make();
/** @var ProductGroupBehaviour $group */
$productGroupBehaviour = factory(ProductGroupBehaviour::class)->make();
$productGroupBehaviour->save(); //We need to first save it before associating it. Else it will try to associate with an id of 0
$group->productgroupbehaviour()->associate($productGroupBehaviour);
$group->save();
self::assertEquals(1, $group->productgroupbehaviour()->get()->count());
}
public function testGroupLinkingToBehaviour()
{
/** @var ProductGroup $group */
$group = factory(ProductGroup::class)->make();
/** @var ProductGroupBehaviour $productGroupBehaviour */
$productGroupBehaviour = factory(ProductGroupBehaviour::class)->create();
$productGroupBehaviour->groups()->save($group);
$this->assertEquals(1, $productGroupBehaviour->groups()->get()->count());
}
public function testCompositeLinkingToGroup()
{
/** @var ProductComposite $composite */
$composite = factory(ProductComposite::class)->create();
/** @var ProductGroupBehaviour $productGroupBehaviour */
$productGroupBehaviour = factory(ProductGroupBehaviour::class)->create();
/** @var ProductGroup $productGroup */
$productGroup = factory(ProductGroup::class)->make();
//Add behaviour to the group before we can save it. It won't save without it because of the foreign keys
$productGroup->productGroupBehaviour()->associate($productGroupBehaviour);
$productGroup->save(); //We can now save it. And because it then has an id, it can be used in composites
$composite->groups()->save($productGroup);
$composite->save();
self::assertEquals(1, $composite->groups()->get()->count());
}
public function testGroupLinkingToComposite()
{
/** @var ProductGroup $group */
$group = factory(ProductGroup::class)->make();
/** @var ProductGroupBehaviour $productGroupBehaviour */
$productGroupBehaviour = factory(ProductGroupBehaviour::class)->create();
$group->productGroupBehaviour()->associate($productGroupBehaviour);
/** @var ProductGroupBehaviour $productComposite */
$productComposite = factory(ProductComposite::class)->create();
$productComposite->groups()->save($group);
$this->assertEquals(1, $productComposite->groups()->get()->count());
}
}