![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mcoil.corals.io/tests/Unit/Attributes/ |
<?php namespace Tests\Unit\Attributes; use App\Shop\Attributes\Attribute; use App\Shop\Attributes\Exceptions\AttributeNotFoundException; use App\Shop\Attributes\Exceptions\CreateAttributeErrorException; use App\Shop\Attributes\Exceptions\UpdateAttributeErrorException; use App\Shop\Attributes\Repositories\AttributeRepository; use Tests\TestCase; class AttributeUnitTest extends TestCase { /** @test */ public function it_should_error_when_the_attribute_is_not_found() { $this->expectException(AttributeNotFoundException::class); $attributeRepo = new AttributeRepository(new Attribute); $attributeRepo->findAttributeById(999); } /** @test */ public function it_should_show_the_attribute() { $attribute = factory(Attribute::class)->create(); $attributeRepo = new AttributeRepository(new Attribute); $found = $attributeRepo->findAttributeById($attribute->id); $this->assertInstanceOf(Attribute::class, $attribute); $this->assertEquals($attribute->name, $found->name); } /** @test */ public function it_should_list_all_the_attributes() { factory(Attribute::class, 5)->create(); $attributeRepo = new AttributeRepository(new Attribute); $list = $attributeRepo->listAttributes(); $this->assertCount(5, $list); } /** @test */ public function it_will_return_null_when_deleting_attribute_that_is_not_created_yet() { $attributeRepo = new AttributeRepository(new Attribute); $delete = $attributeRepo->deleteAttribute(); $this->assertNull($delete); } /** @test */ public function it_can_delete_the_attribute() { $attribute = factory(Attribute::class)->create(); $attributeRepo = new AttributeRepository($attribute); $delete = $attributeRepo->deleteAttribute(); $this->assertTrue($delete); } /** @test */ public function it_errors_when_updating_attribute() { $this->expectException(UpdateAttributeErrorException::class); $attribute = factory(Attribute::class)->create(); $attributeRepo = new AttributeRepository($attribute); $attributeRepo->updateAttribute(['name' => null]); } /** @test */ public function it_can_update_the_attribute() { $attribute = factory(Attribute::class)->create(); $data = [ 'name' => $this->faker->word ]; $attributeRepo = new AttributeRepository($attribute); $update = $attributeRepo->updateAttribute($data); $this->assertTrue($update); $this->assertEquals($data['name'], $attribute->name); } /** @test */ public function it_errors_when_creating_attribute() { $this->expectException(CreateAttributeErrorException::class); $attributeRepo = new AttributeRepository(new Attribute); $attributeRepo->createAttribute([]); } /** @test */ public function it_can_create_attribute() { $data = [ 'name' => $this->faker->word ]; $attributeRepo = new AttributeRepository(new Attribute); $attribute = $attributeRepo->createAttribute($data); $this->assertInstanceOf(Attribute::class, $attribute); $this->assertEquals($data['name'], $attribute->name); } /** @test */ public function it_can_add_child_attribute(){ $attribute = factory(Attribute::class)->create(); $attributeRepo = new AttributeRepository($attribute); $child = factory(Attribute::class)->create(); $attributeRepo -> addChildAttribute ($child); $attribute = $attributeRepo -> findAttributeById($attribute -> id); $foundChild = $attribute -> children() -> first(); $this->assertInstanceOf(Attribute::class, $foundChild); $this->assertEquals($child -> name,$foundChild -> name); $child1 = factory(Attribute::class)->create(); $attributeRepo -> addChildAttribute ($child1); $this->assertCount(2,$attribute -> children()->get()->toArray()); $child2 = factory(Attribute::class)->create(); $attributeRepo -> addChildAttribute ($child2); $this->assertCount(3,$attribute -> children()->get()->toArray()); } /** @test */ public function it_can_add_array_of_child_attribute(){ $attribute = factory(Attribute::class)->create(); $attributeRepo = new AttributeRepository($attribute); $children = factory(Attribute::class,10)->create(); $attributeRepo -> addChildren ($children); $allChildren = $attributeRepo -> allChildrenAttributes(); $this->assertCount(10,$allChildren ->toArray()); } /** @test */ public function it_can_get_all_children_attributes(){ $attribute = factory(Attribute::class)->create(); $attributeRepo = new AttributeRepository($attribute); $child = factory(Attribute::class)->create(); $child1 = factory(Attribute::class)->create(); $child2 = factory(Attribute::class)->create(); $attributeRepo -> addChildAttribute ($child); $attributeRepo -> addChildAttribute ($child1); $attributeRepo -> addChildAttribute ($child2); $allChildren = $attributeRepo -> allChildrenAttributes(); $this->assertCount(3,$allChildren ->toArray()); } /** @test */ public function it_can_delete_child(){ $attribute = factory(Attribute::class)->create(); $attributeRepo = new AttributeRepository($attribute); $child = factory(Attribute::class)->create(); $attributeRepo -> addChildAttribute ($child); $allChildren = $attributeRepo -> allChildrenAttributes(); $this->assertCount(1,$allChildren ->toArray()); $attributeRepo -> deleteChild($child); $allChildren = $attributeRepo -> allChildrenAttributes(); $this->assertCount(0,$allChildren ->toArray()); } /** @test */ /* public function it_can_delete_file_only_in_the_database() { $attribute = new AttributeRepository($this->attribute); $category->deleteFile(['attribute' => $this->attribute->id]); $this->assertDatabaseHas('attributes', ['image_src' => null]); } */ //need add test for delete directory }