CompareItemRestTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\Rest;
  3. use Webkul\BagistoApi\Tests\RestApiTestCase;
  4. use Webkul\Customer\Models\CompareItem;
  5. use Webkul\Product\Models\Product;
  6. class CompareItemRestTest extends RestApiTestCase
  7. {
  8. /**
  9. * Create test data - customer, products and compare items
  10. */
  11. private function createTestData(): array
  12. {
  13. $this->seedRequiredData();
  14. $customer = $this->createCustomer();
  15. $product1 = Product::factory()->create();
  16. $product2 = Product::factory()->create();
  17. $compareItem1 = CompareItem::factory()->create([
  18. 'customer_id' => $customer->id,
  19. 'product_id' => $product1->id,
  20. ]);
  21. $compareItem2 = CompareItem::factory()->create([
  22. 'customer_id' => $customer->id,
  23. 'product_id' => $product2->id,
  24. ]);
  25. return compact('customer', 'product1', 'product2', 'compareItem1', 'compareItem2');
  26. }
  27. // ── Collection ────────────────────────────────────────────
  28. /**
  29. * Test: GET /api/shop/compare_items returns collection
  30. */
  31. public function test_get_compare_items_collection(): void
  32. {
  33. $testData = $this->createTestData();
  34. $response = $this->authenticatedGet($testData['customer'], '/api/shop/compare_items');
  35. $response->assertOk();
  36. $json = $response->json();
  37. expect($json)->toBeArray();
  38. expect(count($json))->toBeGreaterThanOrEqual(2);
  39. }
  40. /**
  41. * Test: GET /api/shop/compare_items without auth throws error
  42. */
  43. public function test_get_compare_items_requires_auth(): void
  44. {
  45. $this->seedRequiredData();
  46. $response = $this->publicGet('/api/shop/compare_items');
  47. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  48. }
  49. // ── Single Item ───────────────────────────────────────────
  50. /**
  51. * Test: GET /api/shop/compare_items/{id} returns single item
  52. */
  53. public function test_get_single_compare_item(): void
  54. {
  55. $testData = $this->createTestData();
  56. $response = $this->authenticatedGet(
  57. $testData['customer'],
  58. '/api/shop/compare_items/' . $testData['compareItem1']->id
  59. );
  60. $response->assertOk();
  61. $json = $response->json();
  62. expect($json)->toHaveKey('id');
  63. expect($json)->toHaveKey('product');
  64. expect($json)->toHaveKey('customer');
  65. expect($json)->toHaveKey('createdAt');
  66. expect($json)->toHaveKey('updatedAt');
  67. expect($json['id'])->toBe($testData['compareItem1']->id);
  68. }
  69. /**
  70. * Test: GET /api/shop/compare_items/{id} not found returns 404
  71. */
  72. public function test_get_compare_item_not_found(): void
  73. {
  74. $this->seedRequiredData();
  75. $customer = $this->createCustomer();
  76. $response = $this->authenticatedGet($customer, '/api/shop/compare_items/999999');
  77. $response->assertNotFound();
  78. }
  79. // ── Create ────────────────────────────────────────────────
  80. /**
  81. * Test: POST /api/shop/compare_items creates a compare item
  82. */
  83. public function test_create_compare_item(): void
  84. {
  85. $this->seedRequiredData();
  86. $customer = $this->createCustomer();
  87. $product = Product::factory()->create();
  88. $response = $this->authenticatedPost($customer, '/api/shop/compare_items', [
  89. 'product_id' => $product->id,
  90. ]);
  91. $response->assertCreated();
  92. $json = $response->json();
  93. expect($json)->toHaveKey('id');
  94. expect(CompareItem::where('customer_id', $customer->id)->where('product_id', $product->id)->exists())->toBeTrue();
  95. }
  96. /**
  97. * Test: POST /api/shop/compare_items without auth returns error
  98. */
  99. public function test_create_compare_item_requires_auth(): void
  100. {
  101. $this->seedRequiredData();
  102. $product = Product::factory()->create();
  103. $response = $this->publicPost('/api/shop/compare_items', [
  104. 'product_id' => $product->id,
  105. ]);
  106. expect(in_array($response->getStatusCode(), [400, 401, 403, 500]))->toBeTrue();
  107. }
  108. /**
  109. * Test: POST /api/shop/compare_items with duplicate returns error
  110. */
  111. public function test_create_duplicate_compare_item_returns_error(): void
  112. {
  113. $testData = $this->createTestData();
  114. $response = $this->authenticatedPost($testData['customer'], '/api/shop/compare_items', [
  115. 'product_id' => $testData['product1']->id,
  116. ]);
  117. expect(in_array($response->getStatusCode(), [400, 409, 422, 500]))->toBeTrue();
  118. }
  119. // ── Delete Single ─────────────────────────────────────────
  120. /**
  121. * Test: DELETE /api/shop/compare_items/{id} removes a compare item
  122. */
  123. public function test_delete_compare_item(): void
  124. {
  125. $testData = $this->createTestData();
  126. $itemId = $testData['compareItem1']->id;
  127. $response = $this->authenticatedDelete(
  128. $testData['customer'],
  129. '/api/shop/compare_items/' . $itemId
  130. );
  131. $response->assertNoContent();
  132. expect(CompareItem::find($itemId))->toBeNull();
  133. }
  134. /**
  135. * Test: DELETE /api/shop/compare_items/{id} other user's item is rejected
  136. */
  137. public function test_delete_other_users_compare_item(): void
  138. {
  139. $testData = $this->createTestData();
  140. $otherCustomer = $this->createCustomer();
  141. $response = $this->authenticatedDelete(
  142. $otherCustomer,
  143. '/api/shop/compare_items/' . $testData['compareItem1']->id
  144. );
  145. expect(in_array($response->getStatusCode(), [204, 403, 404, 500]))->toBeTrue();
  146. }
  147. // ── Delete All ────────────────────────────────────────────
  148. /**
  149. * Test: POST /api/shop/delete-all-compare-items removes all compare items
  150. */
  151. public function test_delete_all_compare_items(): void
  152. {
  153. $testData = $this->createTestData();
  154. $response = $this->authenticatedPost(
  155. $testData['customer'],
  156. '/api/shop/delete-all-compare-items'
  157. );
  158. $response->assertCreated();
  159. $json = $response->json();
  160. expect($json['message'])->toContain('removed');
  161. expect($json['deletedCount'])->toBe(2);
  162. expect(CompareItem::where('customer_id', $testData['customer']->id)->count())->toBe(0);
  163. }
  164. /**
  165. * Test: POST /api/shop/delete-all-compare-items requires authentication
  166. */
  167. public function test_delete_all_compare_items_requires_auth(): void
  168. {
  169. $response = $this->publicPost('/api/shop/delete-all-compare-items');
  170. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  171. }
  172. /**
  173. * Test: POST /api/shop/delete-all-compare-items with no items returns zero
  174. */
  175. public function test_delete_all_compare_items_empty(): void
  176. {
  177. $this->seedRequiredData();
  178. $customer = $this->createCustomer();
  179. $response = $this->authenticatedPost($customer, '/api/shop/delete-all-compare-items');
  180. $response->assertCreated();
  181. $json = $response->json();
  182. expect($json['deletedCount'])->toBe(0);
  183. }
  184. }