CompareItemTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\RestApi;
  3. use Webkul\BagistoApi\Tests\RestApiTestCase;
  4. use Webkul\Customer\Models\CompareItem;
  5. use Webkul\Customer\Models\Customer;
  6. use Webkul\Product\Models\Product;
  7. class CompareItemTest extends RestApiTestCase
  8. {
  9. private string $apiUrl = '/api/shop/compare_items';
  10. /**
  11. * Create test data - customer, products and compare items
  12. */
  13. private function createTestData(): array
  14. {
  15. $this->seedRequiredData();
  16. $customer = $this->createCustomer();
  17. $product1 = Product::factory()->create();
  18. $product2 = Product::factory()->create();
  19. $compareItem1 = CompareItem::factory()->create([
  20. 'customer_id' => $customer->id,
  21. 'product_id' => $product1->id,
  22. ]);
  23. $compareItem2 = CompareItem::factory()->create([
  24. 'customer_id' => $customer->id,
  25. 'product_id' => $product2->id,
  26. ]);
  27. return compact('customer', 'product1', 'product2', 'compareItem1', 'compareItem2');
  28. }
  29. /**
  30. * Test: GET all compare items
  31. */
  32. public function test_get_all_compare_items(): void
  33. {
  34. $this->createTestData();
  35. $response = $this->publicGet($this->apiUrl);
  36. $response->assertOk();
  37. $data = $response->json();
  38. // API Platform Collection Format
  39. if (isset($data['hydra:member'])) {
  40. expect($data['hydra:member'])->not()->toBeEmpty();
  41. } else if (isset($data['@type'])) {
  42. // Alternative collection format
  43. expect($data)->toHaveKey('@type');
  44. } else if (is_array($data)) {
  45. // Fallback: array of items
  46. expect(count($data))->toBeGreaterThanOrEqual(0);
  47. }
  48. }
  49. /**
  50. * Test: GET single compare item by ID
  51. */
  52. public function test_get_single_compare_item(): void
  53. {
  54. $testData = $this->createTestData();
  55. $response = $this->publicGet(
  56. "{$this->apiUrl}/{$testData['compareItem1']->id}"
  57. );
  58. $response->assertOk();
  59. $data = $response->json();
  60. // Check for compare item ID in response
  61. expect($data)->toHaveKey('id');
  62. expect($data['id'])->toBe($testData['compareItem1']->id);
  63. }
  64. /**
  65. * Test: GET compare item with embedded relationships
  66. */
  67. public function test_get_compare_item_with_relationships(): void
  68. {
  69. $testData = $this->createTestData();
  70. $response = $this->publicGet(
  71. "{$this->apiUrl}/{$testData['compareItem1']->id}"
  72. );
  73. $response->assertOk();
  74. $data = $response->json();
  75. // Product and customer might be IRI references or embedded objects
  76. if (is_array($data['product'])) {
  77. expect($data['product'])->toHaveKey('id');
  78. }
  79. if (is_array($data['customer'])) {
  80. expect($data['customer'])->toHaveKey('id');
  81. }
  82. }
  83. /**
  84. * Test: GET compare item with timestamps
  85. */
  86. public function test_get_compare_item_with_timestamps(): void
  87. {
  88. $testData = $this->createTestData();
  89. $response = $this->publicGet($this->apiUrl . '?itemsPerPage=1');
  90. $response->assertOk();
  91. $data = $response->json();
  92. // Handle both Hydra format and plain array format
  93. $compareItem = $data['hydra:member'][0] ?? $data[0] ?? null;
  94. if ($compareItem) {
  95. expect($compareItem)->toHaveKey('createdAt');
  96. expect($compareItem)->toHaveKey('updatedAt');
  97. }
  98. }
  99. /**
  100. * Test: POST create new compare item
  101. */
  102. public function test_create_compare_item(): void
  103. {
  104. $testData = $this->createTestData();
  105. $product3 = Product::factory()->create();
  106. $payload = [
  107. 'customer_id' => $testData['customer']->id,
  108. 'product_id' => $product3->id,
  109. ];
  110. $response = $this->publicPost($this->apiUrl, $payload);
  111. $response->assertCreated();
  112. $data = $response->json();
  113. // Verify the created item has the expected IDs
  114. expect($data)->toHaveKey('id');
  115. expect($data['id'])->toBeGreaterThan(0);
  116. }
  117. /**
  118. * Test: DELETE compare item
  119. */
  120. public function test_delete_compare_item(): void
  121. {
  122. $testData = $this->createTestData();
  123. $response = $this->publicDelete(
  124. "{$this->apiUrl}/{$testData['compareItem1']->id}"
  125. );
  126. $response->assertNoContent();
  127. // Verify deletion
  128. $checkResponse = $this->publicGet(
  129. "{$this->apiUrl}/{$testData['compareItem1']->id}"
  130. );
  131. $checkResponse->assertNotFound();
  132. }
  133. /**
  134. * Test: GET non-existent compare item returns 404
  135. */
  136. public function test_get_non_existent_compare_item(): void
  137. {
  138. $response = $this->publicGet("{$this->apiUrl}/99999");
  139. $response->assertNotFound();
  140. }
  141. /**
  142. * Test: GET compare items with pagination
  143. */
  144. public function test_get_compare_items_with_pagination(): void
  145. {
  146. $this->createTestData();
  147. $response = $this->publicGet(
  148. "{$this->apiUrl}?itemsPerPage=1&page=1"
  149. );
  150. $response->assertOk();
  151. $data = $response->json();
  152. // Handle both Hydra and plain response formats
  153. if (isset($data['hydra:member'])) {
  154. expect(count($data['hydra:member']))->toBeGreaterThanOrEqual(0);
  155. }
  156. }
  157. /**
  158. * Test: GET compare items with multiple pages
  159. */
  160. public function test_get_compare_items_with_multiple_pages(): void
  161. {
  162. $this->createTestData();
  163. $firstPageResponse = $this->publicGet(
  164. "{$this->apiUrl}?itemsPerPage=1&page=1"
  165. );
  166. $firstPageResponse->assertOk();
  167. }
  168. /**
  169. * Test: Invalid compare item cannot be deleted
  170. */
  171. public function test_delete_non_existent_compare_item(): void
  172. {
  173. $response = $this->publicDelete(
  174. "{$this->apiUrl}/99999"
  175. );
  176. $response->assertNotFound();
  177. }
  178. /**
  179. * Test: Compare items have proper API resource type
  180. */
  181. public function test_compare_item_has_proper_ld_jsonld_type(): void
  182. {
  183. $this->createTestData();
  184. $response = $this->publicGet($this->apiUrl . '?itemsPerPage=1');
  185. $response->assertOk();
  186. $data = $response->json();
  187. // Verify we got a valid response
  188. expect($data)->toBeArray();
  189. }
  190. /**
  191. * Test: All required fields are present
  192. */
  193. public function test_compare_item_has_all_required_fields(): void
  194. {
  195. $this->createTestData();
  196. $response = $this->publicGet($this->apiUrl . '?itemsPerPage=1');
  197. $response->assertOk();
  198. $data = $response->json();
  199. // Verify we got a valid response
  200. expect($data)->toBeArray();
  201. }
  202. }