File: D:/HostingSpaces/SBogers10/shop.komma.nl/tests/Unit/CategoriesTest.php
<?php
namespace Tests\Unit;
use App\Categories\Models\Category;
use App\Products\Product\Product;
use App\Products\ProductComposite\ProductComposite;
use App\Products\ProductGroup\ProductGroup;
use App\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class CategoriesTest extends TestCase
{
use DatabaseTransactions; //Automatically rolls back database actions after tests
/**
* @group Categories
* @test
*/
public function testCategoryCreation()
{
/** @var Category $category */
$category = factory(Category::class)->make();
// $site = Site::find(1);
$this->assertInstanceOf(Category::class, $category);
// $this->assertInstanceOf(Site::class, $site);
// $category->site()->associate($site);
$category->save();
$dbCategory = Category::find($category->id);
$this->assertTrue($dbCategory->id === $category->id);
}
/**
* @group Categories
* @test
*/
public function testCategoryProductRelation()
{
$product = factory(Product::class)->make();
$product->save();
/** @var Category $category */
$category = factory(Category::class)->make();
// $site = Site::find(1);
// $category->site()->associate($site);
$category->save();
$category->products()->attach($product);
$categoryDb = Category::find($category->id);
$this->assertTrue($category->id == $categoryDb->id);
$this->assertNotNull($category->products()->first());
$this->assertEquals($category->products()->first()->id, $product->id);
}
/**
* @group Categories
* @test
*/
public function testCategoryProductGroupRelation()
{
/** @var Collection $behaviours */
$behaviours = factory(ProductGroupBehaviour::class, 2)->create();
/** @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($behaviours[0]);
$group->save();
/** @var Category $category */
$category = factory(Category::class)->make();
// $site = Site::find(1);
// $category->site()->associate($site);
$category->save();
$category->product_groups()->attach($group);
$categoryDb = Category::find($category->id);
$this->assertTrue($category->id == $categoryDb->id);
$this->assertNotNull($category->product_groups()->first());
$this->assertEquals($category->product_groups()->first()->id, $group->id);
}
/**
* @group Categories
* @test
*/
public function testCategoryProductCompositeRelation()
{
/** @var Collection $behaviours */
$behaviours = factory(ProductGroupBehaviour::class, 2)->create();
/** @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($behaviours[0]);
$group->save();
/** @var ProductComposite $composite */
$composite = factory(ProductComposite::class)->make();
//We cannot have a ProductGroup without behaviour so we need to associate an existing ProductGroupBehaviour instance with it
$composite->save();
$composite->groups()->attach($group);
$composite->save();
/** @var Category $category */
$category = factory(Category::class)->make();
// $site = Site::find(1);
// $category->site()->associate($site);
$category->save();
$category->product_composites()->attach($composite);
$categoryDb = Category::find($category->id);
$this->assertNotNull($categoryDb);
$this->assertTrue($category->id == $categoryDb->id);
$this->assertNotNull($category->product_composites()->first());
$this->assertEquals($category->product_composites()->first()->id, $composite->id);
}
}