CategoryUnitTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace Tests\Unit\Categories;
  3. use App\Shop\Categories\Category;
  4. use App\Shop\Categories\Exceptions\CategoryInvalidArgumentException;
  5. use App\Shop\Categories\Exceptions\CategoryNotFoundException;
  6. use App\Shop\Categories\Repositories\CategoryRepository;
  7. use Illuminate\Http\UploadedFile;
  8. use Tests\TestCase;
  9. class CategoryUnitTest extends TestCase
  10. {
  11. /** @test */
  12. public function it_can_get_the_child_categories()
  13. {
  14. $parent = factory(Category::class)->create();
  15. $child = factory(Category::class)->create([
  16. 'parent_id' => $parent->id
  17. ]);
  18. $categoryRepo = new CategoryRepository($parent);
  19. $children = $categoryRepo->findChildren();
  20. foreach ($children as $c) {
  21. $this->assertInstanceOf(Category::class, $c);
  22. $this->assertEquals($child->id, $c->id);
  23. }
  24. }
  25. /** @test */
  26. public function it_can_get_the_parent_category()
  27. {
  28. $parent = factory(Category::class)->create();
  29. $child = factory(Category::class)->create([
  30. 'parent_id' => $parent->id
  31. ]);
  32. $categoryRepo = new CategoryRepository($child);
  33. $found = $categoryRepo->findParentCategory();
  34. $this->assertInstanceOf(Category::class, $found);
  35. $this->assertEquals($parent->id, $child->parent_id);
  36. }
  37. /** @test */
  38. public function it_can_return_products_in_the_category()
  39. {
  40. $category = new CategoryRepository($this->category);
  41. $category->syncProducts([$this->product->id]);
  42. $products = $category->findProducts();
  43. foreach ($products as $product) {
  44. $this->assertEquals($this->product->id, $product->id);
  45. }
  46. }
  47. /** @test */
  48. public function it_errors_looking_for_the_category_if_the_slug_is_not_found()
  49. {
  50. $this->expectException(CategoryNotFoundException::class);
  51. $category = new CategoryRepository($this->category);
  52. $category->findCategoryBySlug(['slug' => 'unknown']);
  53. }
  54. /** @test */
  55. public function it_can_get_the_category_by_slug()
  56. {
  57. $category = new CategoryRepository($this->category);
  58. $cat = $category->findCategoryBySlug(['slug' => $this->category->slug]);
  59. $this->assertEquals($this->category->name, $cat->name);
  60. }
  61. /** @test */
  62. public function it_can_delete_file_only_in_the_database()
  63. {
  64. $category = new CategoryRepository($this->category);
  65. $category->deleteFile(['category' => $this->category->id]);
  66. $this->assertDatabaseHas('categories', ['cover' => null]);
  67. }
  68. /** @test */
  69. public function it_can_detach_the_products()
  70. {
  71. $category = new CategoryRepository($this->category);
  72. $category->syncProducts([$this->product->id]);
  73. $category->detachProducts();
  74. $products = $category->findProducts();
  75. $this->assertCount(0, $products);
  76. }
  77. /** @test */
  78. public function it_can_sync_products_in_the_category()
  79. {
  80. $category = new CategoryRepository($this->category);
  81. $category->syncProducts([$this->product->id]);
  82. $products = $category->findProducts();
  83. foreach ($products as $product) {
  84. $this->assertEquals($this->product->name, $product->name);
  85. }
  86. }
  87. /** @test */
  88. public function it_errors_creating_the_category_when_required_fields_are_not_passed()
  89. {
  90. $this->expectException(CategoryInvalidArgumentException::class);
  91. $product = new CategoryRepository(new Category);
  92. $product->createCategory([]);
  93. }
  94. /** @test */
  95. public function it_can_delete_a_category()
  96. {
  97. $category = new CategoryRepository($this->category);
  98. $category->deleteCategory();
  99. $this->assertDatabaseMissing('categories', collect($this->category)->all());
  100. }
  101. /** @test */
  102. public function it_can_list_all_the_categories()
  103. {
  104. $category = factory(Category::class)->create();
  105. $attributes = $category->getFillable();
  106. $categoryRepo = new CategoryRepository(new Category);
  107. $categories = $categoryRepo->listCategories();
  108. $categories->each(function ($category, $key) use ($attributes) {
  109. foreach ($category->getFillable() as $key => $value) {
  110. $this->assertArrayHasKey($key, $attributes);
  111. }
  112. });
  113. }
  114. /** @test */
  115. public function it_errors_finding_a_category()
  116. {
  117. $this->expectException(CategoryNotFoundException::class);
  118. $category = new CategoryRepository(new Category);
  119. $category->findCategoryById(999);
  120. }
  121. /** @test */
  122. public function it_can_find_the_category()
  123. {
  124. $category = new CategoryRepository(new Category);
  125. $found = $category->findCategoryById($this->category->id);
  126. $this->assertEquals($this->category->name, $found->name);
  127. $this->assertEquals($this->category->slug, $found->slug);
  128. $this->assertEquals($this->category->description, $found->description);
  129. $this->assertEquals($this->category->cover, $found->cover);
  130. $this->assertEquals($this->category->status, $found->status);
  131. }
  132. /** @test */
  133. public function it_can_update_the_category()
  134. {
  135. $cover = UploadedFile::fake()->image('file.png', 600, 600);
  136. $parent = factory(Category::class)->create();
  137. $params = [
  138. 'name' => 'Boys',
  139. 'slug' => 'boys',
  140. 'description' => $this->faker->paragraph,
  141. 'status' => 1,
  142. 'parent' => $parent->id,
  143. 'cover' => $cover
  144. ];
  145. $category = new CategoryRepository($this->category);
  146. $updated = $category->updateCategory($params);
  147. $this->assertInstanceOf(Category::class, $updated);
  148. $this->assertEquals($params['name'], $updated->name);
  149. $this->assertEquals($params['slug'], $updated->slug);
  150. $this->assertEquals($params['description'], $updated->description);
  151. $this->assertEquals($params['status'], $updated->status);
  152. $this->assertEquals($params['parent'], $updated->parent_id);
  153. $this->assertEquals($params['cover'], $cover);
  154. }
  155. /** @test */
  156. public function it_can_create_a_category()
  157. {
  158. $cover = UploadedFile::fake()->image('file.png', 600, 600);
  159. $parent = factory(Category::class)->create();
  160. $params = [
  161. 'name' => 'Boys',
  162. 'slug' => 'boys',
  163. 'cover' => $cover,
  164. 'description' => $this->faker->paragraph,
  165. 'status' => 1,
  166. 'parent' => $parent->id
  167. ];
  168. $category = new CategoryRepository(new Category);
  169. $created = $category->createCategory($params);
  170. $this->assertInstanceOf(Category::class, $created);
  171. $this->assertEquals($params['name'], $created->name);
  172. $this->assertEquals($params['slug'], $created->slug);
  173. $this->assertEquals($params['description'], $created->description);
  174. $this->assertEquals($params['status'], $created->status);
  175. $this->assertEquals($params['parent'], $created->parent_id);
  176. $this->assertEquals($params['cover'], $cover);
  177. }
  178. /** @test */
  179. public function it_can_create_root_category()
  180. {
  181. $params = [
  182. 'name' => 'Boys',
  183. 'slug' => 'boys',
  184. 'description' => $this->faker->paragraph,
  185. 'status' => 1
  186. ];
  187. $category = new CategoryRepository(new Category);
  188. $created = $category->createCategory($params);
  189. $this->assertTrue($created->isRoot());
  190. }
  191. /** @test */
  192. public function it_can_update_child_category_to_root_category()
  193. {
  194. // suppose to have a child category
  195. [$child, $parent] = factory(Category::class, 2)->create();
  196. $child->parent()->associate($parent)->save();
  197. // send params without parent
  198. $category = new CategoryRepository($child);
  199. $updated = $category->updateCategory([
  200. 'name' => 'Boys',
  201. 'slug' => 'boys'
  202. ]);
  203. // check if updated category is root
  204. $this->assertTrue($updated->isRoot());
  205. }
  206. /** @test */
  207. public function it_can_update_root_category_to_child()
  208. {
  209. [$child, $parent] = factory(Category::class, 2)->create();
  210. // set parent category via repository
  211. $category = new CategoryRepository($child);
  212. $updated = $category->updateCategory([
  213. 'name' => 'Boys',
  214. 'slug' => 'boys',
  215. 'parent' => $parent->id
  216. ]);
  217. // check if updated category is root
  218. $this->assertTrue( $updated->parent->is($parent) );
  219. }
  220. }