AttributeUnitTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Tests\Unit\Attributes;
  3. use App\Shop\Attributes\Attribute;
  4. use App\Shop\Attributes\Exceptions\AttributeNotFoundException;
  5. use App\Shop\Attributes\Exceptions\CreateAttributeErrorException;
  6. use App\Shop\Attributes\Exceptions\UpdateAttributeErrorException;
  7. use App\Shop\Attributes\Repositories\AttributeRepository;
  8. use Tests\TestCase;
  9. class AttributeUnitTest extends TestCase
  10. {
  11. /** @test */
  12. public function it_should_error_when_the_attribute_is_not_found()
  13. {
  14. $this->expectException(AttributeNotFoundException::class);
  15. $attributeRepo = new AttributeRepository(new Attribute);
  16. $attributeRepo->findAttributeById(999);
  17. }
  18. /** @test */
  19. public function it_should_show_the_attribute()
  20. {
  21. $attribute = factory(Attribute::class)->create();
  22. $attributeRepo = new AttributeRepository(new Attribute);
  23. $found = $attributeRepo->findAttributeById($attribute->id);
  24. $this->assertInstanceOf(Attribute::class, $attribute);
  25. $this->assertEquals($attribute->name, $found->name);
  26. }
  27. /** @test */
  28. public function it_should_list_all_the_attributes()
  29. {
  30. factory(Attribute::class, 5)->create();
  31. $attributeRepo = new AttributeRepository(new Attribute);
  32. $list = $attributeRepo->listAttributes();
  33. $this->assertCount(5, $list);
  34. }
  35. /** @test */
  36. public function it_will_return_null_when_deleting_attribute_that_is_not_created_yet()
  37. {
  38. $attributeRepo = new AttributeRepository(new Attribute);
  39. $delete = $attributeRepo->deleteAttribute();
  40. $this->assertNull($delete);
  41. }
  42. /** @test */
  43. public function it_can_delete_the_attribute()
  44. {
  45. $attribute = factory(Attribute::class)->create();
  46. $attributeRepo = new AttributeRepository($attribute);
  47. $delete = $attributeRepo->deleteAttribute();
  48. $this->assertTrue($delete);
  49. }
  50. /** @test */
  51. public function it_errors_when_updating_attribute()
  52. {
  53. $this->expectException(UpdateAttributeErrorException::class);
  54. $attribute = factory(Attribute::class)->create();
  55. $attributeRepo = new AttributeRepository($attribute);
  56. $attributeRepo->updateAttribute(['name' => null]);
  57. }
  58. /** @test */
  59. public function it_can_update_the_attribute()
  60. {
  61. $attribute = factory(Attribute::class)->create();
  62. $data = [
  63. 'name' => $this->faker->word
  64. ];
  65. $attributeRepo = new AttributeRepository($attribute);
  66. $update = $attributeRepo->updateAttribute($data);
  67. $this->assertTrue($update);
  68. $this->assertEquals($data['name'], $attribute->name);
  69. }
  70. /** @test */
  71. public function it_errors_when_creating_attribute()
  72. {
  73. $this->expectException(CreateAttributeErrorException::class);
  74. $attributeRepo = new AttributeRepository(new Attribute);
  75. $attributeRepo->createAttribute([]);
  76. }
  77. /** @test */
  78. public function it_can_create_attribute()
  79. {
  80. $data = [
  81. 'name' => $this->faker->word
  82. ];
  83. $attributeRepo = new AttributeRepository(new Attribute);
  84. $attribute = $attributeRepo->createAttribute($data);
  85. $this->assertInstanceOf(Attribute::class, $attribute);
  86. $this->assertEquals($data['name'], $attribute->name);
  87. }
  88. }