ProductAttributeUnitTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Tests\Unit\ProductAttributes;
  3. use App\Shop\Attributes\Attribute;
  4. use App\Shop\Attributes\Repositories\AttributeRepository;
  5. use App\Shop\AttributeValues\AttributeValue;
  6. use App\Shop\AttributeValues\Repositories\AttributeValueRepository;
  7. use App\Shop\ProductAttributes\Exceptions\ProductAttributeNotFoundException;
  8. use App\Shop\ProductAttributes\ProductAttribute;
  9. use App\Shop\ProductAttributes\Repositories\ProductAttributeRepository;
  10. use App\Shop\Products\Product;
  11. use App\Shop\Products\Repositories\ProductRepository;
  12. use Tests\TestCase;
  13. class ProductAttributeUnitTest extends TestCase
  14. {
  15. /** @test */
  16. public function it_throws_error_when_the_product_attribute_is_not_found()
  17. {
  18. $this->expectException(ProductAttributeNotFoundException::class);
  19. $productAttributeRepo = new ProductAttributeRepository(new ProductAttribute);
  20. $productAttributeRepo->findProductAttributeById(999);
  21. }
  22. /** @test */
  23. public function it_can_find_the_product_attribute_by_id()
  24. {
  25. $productAttribute = factory(ProductAttribute::class)->create([
  26. 'product_id' => $this->product->id
  27. ]);
  28. $productAttributeRepo = new ProductAttributeRepository(new ProductAttribute);
  29. $found = $productAttributeRepo->findProductAttributeById($productAttribute->id);
  30. $this->assertEquals($productAttribute->quantity, $found->quantity);
  31. $this->assertEquals($productAttribute->price, $found->price);
  32. }
  33. /** @test */
  34. public function it_can_sync_the_attribute_values_to_product_attributes()
  35. {
  36. $attribute = factory(Attribute::class)->create(['name' => 'Color']);
  37. $attributeValueRepo = new AttributeValueRepository(new AttributeValue);
  38. $created = $attributeValueRepo->createAttributeValue($attribute, ['value' => 'green']);
  39. $attributeRepo = new AttributeRepository($attribute);
  40. $associated = $attributeRepo->associateAttributeValue($created);
  41. $this->assertInstanceOf(AttributeValue::class, $created);
  42. $this->assertInstanceOf(AttributeValue::class, $associated);
  43. $this->assertEquals($created->name, $associated->name);
  44. }
  45. /** @test */
  46. public function it_returns_null_deleting_non_existing_product_attribute()
  47. {
  48. $product = factory(Product::class)->create();
  49. $productRepo = new ProductRepository($product);
  50. $deleted = $productRepo->removeProductAttribute(new ProductAttribute);
  51. $this->assertNull($deleted);
  52. }
  53. /** @test */
  54. public function it_can_remove_product_attribute()
  55. {
  56. $data = [
  57. 'quantity' => 1,
  58. 'price' => 10.45
  59. ];
  60. $productAttribute = new ProductAttribute($data);
  61. $product = factory(Product::class)->create();
  62. $productRepo = new ProductRepository($product);
  63. $created = $productRepo->saveProductAttributes($productAttribute);
  64. $deleted = $productRepo->removeProductAttribute($created);
  65. $this->assertTrue($deleted);
  66. }
  67. /** @test */
  68. public function it_can_create_product_attribute()
  69. {
  70. $data = [
  71. 'quantity' => 1,
  72. 'price' => 10.45
  73. ];
  74. $productAttribute = new ProductAttribute($data);
  75. $product = factory(Product::class)->create();
  76. $productRepo = new ProductRepository($product);
  77. $created = $productRepo->saveProductAttributes($productAttribute);
  78. $this->assertInstanceOf(ProductAttribute::class, $created);
  79. $this->assertInstanceOf(Product::class, $productAttribute->product);
  80. $this->assertEquals($data['quantity'], $created->quantity);
  81. $this->assertEquals($data['price'], $created->price);
  82. $this->assertEquals($product->name, $created->product->name);
  83. $this->assertEquals($product->price, $created->product->price);
  84. }
  85. }