AttributesFeatureTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Tests\Feature\Admin\Attributes;
  3. use App\Shop\Attributes\Attribute;
  4. use App\Shop\AttributeValues\AttributeValue;
  5. use App\Shop\AttributeValues\Repositories\AttributeValueRepository;
  6. use App\Shop\ProductAttributes\ProductAttribute;
  7. use App\Shop\Products\Product;
  8. use App\Shop\Products\Repositories\ProductRepository;
  9. use Tests\TestCase;
  10. class AttributesFeatureTest extends TestCase
  11. {
  12. /** @test */
  13. public function it_can_update_product_attribute_combination()
  14. {
  15. $product = factory(Product::class)->create();
  16. $attributeValue1 = new AttributeValue(['value' => 'small']);
  17. $attributeValueRepo1 = new AttributeValueRepository($attributeValue1);
  18. $attribute1 = factory(Attribute::class)->create(['name' => 'Sizes']);
  19. $createdValue = $attributeValueRepo1->associateToAttribute($attribute1);
  20. $data = [
  21. 'price' => 0,
  22. 'quantity' => 0,
  23. 'sku' => $this->faker->uuid,
  24. 'name' => 'test',
  25. 'productAttributeQuantity' => 1,
  26. 'productAttributePrice' => 2.45,
  27. 'attributeValue' => [$createdValue->id],
  28. 'combination' => 1
  29. ];
  30. $this
  31. ->actingAs($this->employee, 'employee')
  32. ->put(route('admin.products.update', $product->id), $data)
  33. ->assertStatus(302)
  34. ->assertRedirect(route('admin.products.edit', [$product->id, 'combination' => 1]))
  35. ->assertSessionHas('message', 'Attribute combination created successful');
  36. }
  37. /** @test */
  38. public function it_can_detach_the_attribute_from_the_product_and_delete_it()
  39. {
  40. $attributeValue1 = new AttributeValue(['value' => 'small']);
  41. $attributeValueRepo1 = new AttributeValueRepository($attributeValue1);
  42. $attribute1 = factory(Attribute::class)->create(['name' => 'Sizes']);
  43. $createdValue1 = $attributeValueRepo1->associateToAttribute($attribute1);
  44. $attributeValue2 = new AttributeValue(['value' => 'red']);
  45. $attributeValueRepo2 = new AttributeValueRepository($attributeValue2);
  46. $attribute2 = factory(Attribute::class)->create(['name' => 'Colors']);
  47. $createdValue2 = $attributeValueRepo2->associateToAttribute($attribute2);
  48. $data = [
  49. 'quantity' => 2,
  50. 'price' => 2.50
  51. ];
  52. $productAttribute = new ProductAttribute($data);
  53. $product = factory(Product::class)->create();
  54. $productRepo = new ProductRepository($product);
  55. $created = $productRepo->saveProductAttributes($productAttribute);
  56. $productRepo->saveCombination($created, $createdValue1, $createdValue2);
  57. $this
  58. ->actingAs($this->employee, 'employee')
  59. ->get(route('admin.products.edit', [$product->id, 'delete' => 1, 'pa' => $created->id]))
  60. ->assertStatus(302)
  61. ->assertRedirect(route('admin.products.edit', [$product->id, 'combination' => 1]))
  62. ->assertSessionHas('message', 'Delete successful');
  63. }
  64. /** @test */
  65. public function it_error_when_the_attribute_is_not_found()
  66. {
  67. $this
  68. ->actingAs($this->employee, 'employee')
  69. ->get(route('admin.attributes.show', 999))
  70. ->assertStatus(302)
  71. ->assertRedirect(route('admin.attributes.index'))
  72. ->assertSessionHas('error', 'The attribute you are looking for is not found.');
  73. }
  74. /** @test */
  75. public function it_can_show_the_attribute()
  76. {
  77. $attribute = factory(Attribute::class)->create();
  78. $this
  79. ->actingAs($this->employee, 'employee')
  80. ->get(route('admin.attributes.show', $attribute->id))
  81. ->assertStatus(200)
  82. ->assertSee('Attribute name')
  83. ->assertSee('Back');
  84. }
  85. /** @test */
  86. public function it_can_delete_the_attribute()
  87. {
  88. $attribute = factory(Attribute::class)->create();
  89. $this
  90. ->actingAs($this->employee, 'employee')
  91. ->delete(route('admin.attributes.destroy', $attribute->id))
  92. ->assertStatus(302)
  93. ->assertRedirect(route('admin.attributes.index'))
  94. ->assertSessionHas('message', 'Attribute deleted successfully!');
  95. }
  96. /** @test */
  97. public function it_should_be_able_to_create_an_attribute()
  98. {
  99. $data = [
  100. 'name' => $this->faker->word
  101. ];
  102. $this
  103. ->actingAs($this->employee, 'employee')
  104. ->post(route('admin.attributes.store'), $data)
  105. ->assertStatus(302)
  106. ->assertRedirect(route('admin.attributes.edit', 1));
  107. }
  108. /** @test */
  109. public function it_should_show_the_create_attribute_page()
  110. {
  111. $this
  112. ->actingAs($this->employee, 'employee')
  113. ->get(route('admin.attributes.create'))
  114. ->assertStatus(200)
  115. ->assertSee('Attribute name')
  116. ->assertSee('Create')
  117. ->assertSee('Back');
  118. }
  119. /** @test */
  120. public function it_errors_updating_the_attribute()
  121. {
  122. $attribute = factory(Attribute::class)->create();
  123. $data = ['name' => null];
  124. $this
  125. ->actingAs($this->employee, 'employee')
  126. ->put(route('admin.attributes.update', $attribute->id), $data)
  127. ->assertStatus(302)
  128. ->assertRedirect(route('admin.attributes.edit', $attribute->id))
  129. ->assertSessionHas('error');
  130. }
  131. /** @test */
  132. public function it_should_be_able_to_update_the_attribute()
  133. {
  134. $attribute = factory(Attribute::class)->create();
  135. $data = [
  136. 'name' => $this->faker->word
  137. ];
  138. $this
  139. ->actingAs($this->employee, 'employee')
  140. ->put(route('admin.attributes.update', $attribute->id), $data)
  141. ->assertStatus(302)
  142. ->assertRedirect(route('admin.attributes.edit', $attribute->id))
  143. ->assertSessionHas('message', 'Attribute update successful!');
  144. }
  145. /** @test */
  146. public function it_should_show_the_update_attribute_page()
  147. {
  148. $attribute = factory(Attribute::class)->create();
  149. $this
  150. ->actingAs($this->employee, 'employee')
  151. ->get(route('admin.attributes.edit', $attribute->id))
  152. ->assertStatus(200)
  153. ->assertSee('Attribute name')
  154. ->assertSee('Update')
  155. ->assertSee('Back');
  156. }
  157. /** @test */
  158. public function it_should_show_the_list_of_attributes_page()
  159. {
  160. factory(Attribute::class)->create();
  161. $this
  162. ->actingAs($this->employee, 'employee')
  163. ->get(route('admin.attributes.index'))
  164. ->assertStatus(200)
  165. ->assertSee('Attribute name')
  166. ->assertSee('Edit')
  167. ->assertSee('Delete');
  168. }
  169. }