ProductFeatureTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Shop\Categories\Category;
  4. use App\Shop\Products\Product;
  5. use App\Shop\Products\Repositories\ProductRepository;
  6. use Illuminate\Http\UploadedFile;
  7. use Tests\TestCase;
  8. class ProductFeatureTest extends TestCase
  9. {
  10. /** @test */
  11. public function it_can_show_the_product()
  12. {
  13. $product = factory(Product::class)->create();
  14. $this
  15. ->actingAs($this->employee, 'employee')
  16. ->get(route('admin.products.show', $product->id))
  17. ->assertStatus(200)
  18. ->assertSee($product->name);
  19. }
  20. /** @test */
  21. public function it_can_search_the_product()
  22. {
  23. $product = factory(Product::class)->create();
  24. $this
  25. ->actingAs($this->employee, 'employee')
  26. ->get(route('admin.products.index', ['q' => str_limit($product->name, 5, '')]))
  27. ->assertStatus(200)
  28. ->assertSee($product->name);
  29. }
  30. /** @test */
  31. public function it_can_remove_the_thumbnail()
  32. {
  33. $product = 'apple';
  34. $cover = UploadedFile::fake()->image('file.png', 600, 600);
  35. $params = [
  36. 'sku' => $this->faker->numberBetween(1111111, 999999),
  37. 'name' => $product,
  38. 'slug' => str_slug($product),
  39. 'description' => $this->faker->paragraph,
  40. 'cover' => $cover,
  41. 'quantity' => 10,
  42. 'price' => 9.95,
  43. 'status' => 1,
  44. 'image' => [
  45. UploadedFile::fake()->image('file.png', 200, 200),
  46. UploadedFile::fake()->image('file1.png', 200, 200),
  47. UploadedFile::fake()->image('file2.png', 200, 200)
  48. ]
  49. ];
  50. $productRepo = new ProductRepository(new Product());
  51. $created = $productRepo->createProduct($params);
  52. $repo = new ProductRepository($created);
  53. $repo->saveProductImages(collect($params['image']), $created);
  54. $image = $repo->findProductImages()->first();
  55. $this
  56. ->actingAs($this->employee, 'employee')
  57. ->get(route('admin.product.remove.thumb', ['src' => $image->src]))
  58. ->assertStatus(302)
  59. ->assertRedirect(url('/'))
  60. ->assertSessionHas('message', 'Image delete successful');
  61. }
  62. /** @test */
  63. public function it_can_remove_the_cover_image()
  64. {
  65. $product = factory(Product::class)->create();
  66. $this
  67. ->actingAs($this->employee, 'employee')
  68. ->get(route('admin.product.remove.image', ['product' => $product->id, 'image' => substr($product->cover, 9)]))
  69. ->assertStatus(302)
  70. ->assertRedirect(url('/'))
  71. ->assertSessionHas('message', 'Image delete successful');
  72. }
  73. /** @test */
  74. public function it_can_delete_the_product()
  75. {
  76. $product = factory(Product::class)->create();
  77. $this
  78. ->actingAs($this->employee, 'employee')
  79. ->delete(route('admin.products.destroy', $product->id))
  80. ->assertStatus(302)
  81. ->assertRedirect(route('admin.products.index'))
  82. ->assertSessionHas('message', 'Delete successful');
  83. }
  84. /** @test */
  85. public function it_can_list_all_products()
  86. {
  87. $product = factory(Product::class)->create();
  88. $this
  89. ->actingAs($this->employee, 'employee')
  90. ->get(route('admin.products.index'))
  91. ->assertStatus(200)
  92. ->assertSee($product->name);
  93. }
  94. /** @test */
  95. public function it_can_show_the_product_edit_page()
  96. {
  97. $product = factory(Product::class)->create();
  98. $this
  99. ->actingAs($this->employee, 'employee')
  100. ->get(route('admin.products.edit', $product->id))
  101. ->assertStatus(200)
  102. ->assertSee($product->name);
  103. }
  104. /** @test */
  105. public function it_can_show_the_product_create()
  106. {
  107. $this
  108. ->actingAs($this->employee, 'employee')
  109. ->get(route('admin.products.create'))
  110. ->assertStatus(200);
  111. }
  112. /** @test */
  113. public function it_can_create_the_product_with_categories()
  114. {
  115. $product = 'apple';
  116. $cover = UploadedFile::fake()->image('file.png', 600, 600);
  117. $thumbnails = [
  118. UploadedFile::fake()->image('cover.jpg', 600, 600),
  119. UploadedFile::fake()->image('cover.jpg', 600, 600),
  120. UploadedFile::fake()->image('cover.jpg', 600, 600)
  121. ];
  122. $categories = factory(Category::class, 2)->create();
  123. $categoryIds = $categories->transform(function (Category $category) {
  124. return $category->id;
  125. })->all();
  126. $params = [
  127. 'sku' => $this->faker->numberBetween(1111111, 999999),
  128. 'name' => $product,
  129. 'slug' => str_slug($product),
  130. 'description' => $this->faker->paragraph,
  131. 'cover' => $cover,
  132. 'quantity' => 10,
  133. 'price' => 9.95,
  134. 'status' => 1,
  135. 'categories' => $categoryIds,
  136. 'image' => $thumbnails
  137. ];
  138. $this
  139. ->actingAs($this->employee, 'employee')
  140. ->post(route('admin.products.store'), $params)
  141. ->assertStatus(302)
  142. ->assertRedirect(route('admin.products.edit', 2))
  143. ->assertSessionHas('message', 'Create successful');
  144. }
  145. /** @test */
  146. public function it_can_create_the_product()
  147. {
  148. $product = 'apple';
  149. $cover = UploadedFile::fake()->image('file.png', 600, 600);
  150. $thumbnails = [
  151. UploadedFile::fake()->image('cover.jpg', 600, 600),
  152. UploadedFile::fake()->image('cover.jpg', 600, 600),
  153. UploadedFile::fake()->image('cover.jpg', 600, 600)
  154. ];
  155. $params = [
  156. 'sku' => $this->faker->numberBetween(1111111, 999999),
  157. 'name' => $product,
  158. 'slug' => str_slug($product),
  159. 'description' => $this->faker->paragraph,
  160. 'cover' => $cover,
  161. 'quantity' => 10,
  162. 'price' => 9.95,
  163. 'status' => 1,
  164. 'image' => $thumbnails
  165. ];
  166. $this
  167. ->actingAs($this->employee, 'employee')
  168. ->post(route('admin.products.store'), $params)
  169. ->assertStatus(302)
  170. ->assertRedirect(route('admin.products.edit', 2))
  171. ->assertSessionHas('message', 'Create successful');
  172. }
  173. /** @test */
  174. public function it_errors_creating_the_product_when_the_required_fields_are_not_filled()
  175. {
  176. $this
  177. ->actingAs($this->employee, 'employee')
  178. ->post(route('admin.products.store'), [])
  179. ->assertStatus(302)
  180. ->assertSessionHasErrors();
  181. }
  182. /** @test */
  183. public function it_can_detach_the_categories_associated_with_the_product()
  184. {
  185. $product = 'apple';
  186. $data = [
  187. 'sku' => $this->faker->numberBetween(1111111, 999999),
  188. 'name' => $product,
  189. 'slug' => str_slug($product),
  190. 'description' => $this->faker->paragraph,
  191. 'cover' => UploadedFile::fake()->image('file.png', 200, 200),
  192. 'quantity' => 10,
  193. 'price' => 9.95,
  194. 'status' => 1
  195. ];
  196. $this->actingAs($this->employee, 'employee')
  197. ->put(route('admin.products.update', $this->product->id), $data)
  198. ->assertSessionHas(['message' => 'Update successful'])
  199. ->assertRedirect(route('admin.products.edit', $this->product->id));
  200. }
  201. /** @test */
  202. public function it_can_sync_the_categories_associated_with_the_product()
  203. {
  204. $categories = [];
  205. $cats = factory(Category::class)->create();
  206. foreach ($cats as $cat) {
  207. $categories[] = $cat['id'];
  208. }
  209. $product = 'apple';
  210. $params = [
  211. 'sku' => $this->faker->numberBetween(1111111, 999999),
  212. 'name' => $product,
  213. 'slug' => str_slug($product),
  214. 'description' => $this->faker->paragraph,
  215. 'cover' => UploadedFile::fake()->image('file.png', 200, 200),
  216. 'quantity' => 10,
  217. 'price' => 9.95,
  218. 'status' => 1,
  219. 'categories' => $categories
  220. ];
  221. $this->actingAs($this->employee, 'employee')
  222. ->put(route('admin.products.update', $this->product->id), $params)
  223. ->assertStatus(302)
  224. ->assertRedirect(route('admin.products.edit', $this->product->id))
  225. ->assertSessionHas('message', 'Update successful');
  226. }
  227. }