CustomerOrderRestTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\Rest;
  3. use Webkul\BagistoApi\Tests\RestApiTestCase;
  4. use Webkul\Core\Models\Channel;
  5. use Webkul\Product\Models\Product;
  6. use Webkul\Sales\Models\Order;
  7. use Webkul\Sales\Models\OrderItem;
  8. use Webkul\Sales\Models\OrderPayment;
  9. class CustomerOrderRestTest extends RestApiTestCase
  10. {
  11. /**
  12. * Create test data — customer with orders
  13. */
  14. private function createTestData(): array
  15. {
  16. $this->seedRequiredData();
  17. $customer = $this->createCustomer();
  18. $channel = Channel::first();
  19. $product = Product::factory()->create();
  20. $order1 = Order::factory()->create([
  21. 'customer_id' => $customer->id,
  22. 'customer_email' => $customer->email,
  23. 'customer_first_name' => $customer->first_name,
  24. 'customer_last_name' => $customer->last_name,
  25. 'channel_id' => $channel->id,
  26. 'status' => 'pending',
  27. ]);
  28. OrderItem::factory()->create([
  29. 'order_id' => $order1->id,
  30. 'product_id' => $product->id,
  31. 'sku' => 'TEST-SKU-001',
  32. 'type' => 'simple',
  33. 'name' => 'Test Product One',
  34. ]);
  35. OrderPayment::factory()->create([
  36. 'order_id' => $order1->id,
  37. ]);
  38. $order2 = Order::factory()->create([
  39. 'customer_id' => $customer->id,
  40. 'customer_email' => $customer->email,
  41. 'customer_first_name' => $customer->first_name,
  42. 'customer_last_name' => $customer->last_name,
  43. 'channel_id' => $channel->id,
  44. 'status' => 'completed',
  45. ]);
  46. OrderItem::factory()->create([
  47. 'order_id' => $order2->id,
  48. 'product_id' => $product->id,
  49. 'sku' => 'TEST-SKU-002',
  50. 'type' => 'simple',
  51. 'name' => 'Test Product Two',
  52. ]);
  53. OrderPayment::factory()->create([
  54. 'order_id' => $order2->id,
  55. ]);
  56. return compact('customer', 'channel', 'product', 'order1', 'order2');
  57. }
  58. // ── Collection ────────────────────────────────────────────
  59. /**
  60. * Test: GET /api/shop/customer-orders returns collection
  61. */
  62. public function test_get_customer_orders_collection(): void
  63. {
  64. $testData = $this->createTestData();
  65. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-orders');
  66. $response->assertOk();
  67. $json = $response->json();
  68. expect($json)->toBeArray();
  69. expect(count($json))->toBeGreaterThanOrEqual(2);
  70. }
  71. /**
  72. * Test: GET /api/shop/customer-orders without auth returns error
  73. */
  74. public function test_get_customer_orders_requires_auth(): void
  75. {
  76. $this->seedRequiredData();
  77. $response = $this->publicGet('/api/shop/customer-orders');
  78. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  79. }
  80. /**
  81. * Test: Customer only sees own orders
  82. */
  83. public function test_customer_only_sees_own_orders(): void
  84. {
  85. $testData = $this->createTestData();
  86. /** Create another customer with their own order */
  87. $otherCustomer = $this->createCustomer();
  88. $channel = Channel::first();
  89. Order::factory()->create([
  90. 'customer_id' => $otherCustomer->id,
  91. 'customer_email' => $otherCustomer->email,
  92. 'customer_first_name' => $otherCustomer->first_name,
  93. 'customer_last_name' => $otherCustomer->last_name,
  94. 'channel_id' => $channel->id,
  95. 'status' => 'pending',
  96. ]);
  97. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-orders');
  98. $response->assertOk();
  99. $json = $response->json();
  100. /** Should only see the 2 orders belonging to testData customer */
  101. expect(count($json))->toBe(2);
  102. }
  103. /**
  104. * Test: Customer with no orders returns empty collection
  105. */
  106. public function test_customer_with_no_orders_returns_empty(): void
  107. {
  108. $this->seedRequiredData();
  109. $customer = $this->createCustomer();
  110. $response = $this->authenticatedGet($customer, '/api/shop/customer-orders');
  111. $response->assertOk();
  112. $json = $response->json();
  113. expect($json)->toBeArray();
  114. expect(count($json))->toBe(0);
  115. }
  116. // ── Single Item ───────────────────────────────────────────
  117. /**
  118. * Test: GET /api/shop/customer-orders/{id} returns single order
  119. */
  120. public function test_get_single_customer_order(): void
  121. {
  122. $testData = $this->createTestData();
  123. $response = $this->authenticatedGet(
  124. $testData['customer'],
  125. '/api/shop/customer-orders/'.$testData['order1']->id
  126. );
  127. $response->assertOk();
  128. $json = $response->json();
  129. expect($json)->toHaveKey('id');
  130. expect($json)->toHaveKey('incrementId');
  131. expect($json)->toHaveKey('status');
  132. expect($json)->toHaveKey('customerEmail');
  133. expect($json)->toHaveKey('customerFirstName');
  134. expect($json)->toHaveKey('customerLastName');
  135. expect($json)->toHaveKey('grandTotal');
  136. expect($json)->toHaveKey('subTotal');
  137. expect($json)->toHaveKey('shippingMethod');
  138. expect($json)->toHaveKey('shippingTitle');
  139. expect($json)->toHaveKey('baseCurrencyCode');
  140. expect($json)->toHaveKey('orderCurrencyCode');
  141. expect($json)->toHaveKey('totalItemCount');
  142. expect($json)->toHaveKey('totalQtyOrdered');
  143. expect($json)->toHaveKey('createdAt');
  144. expect($json['id'])->toBe($testData['order1']->id);
  145. expect($json['status'])->toBe('pending');
  146. expect($json['customerEmail'])->toBe($testData['customer']->email);
  147. }
  148. /**
  149. * Test: GET /api/shop/customer-orders/{id} with invalid id returns 404
  150. */
  151. public function test_get_customer_order_not_found(): void
  152. {
  153. $this->seedRequiredData();
  154. $customer = $this->createCustomer();
  155. $response = $this->authenticatedGet($customer, '/api/shop/customer-orders/999999');
  156. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  157. }
  158. /**
  159. * Test: Cannot access another customer's order by ID
  160. */
  161. public function test_cannot_access_other_customers_order(): void
  162. {
  163. $testData = $this->createTestData();
  164. $otherCustomer = $this->createCustomer();
  165. $response = $this->authenticatedGet(
  166. $otherCustomer,
  167. '/api/shop/customer-orders/'.$testData['order1']->id
  168. );
  169. /** Should return 404/500 because the order doesn't belong to otherCustomer */
  170. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  171. }
  172. /**
  173. * Test: Single order without auth returns error
  174. */
  175. public function test_get_single_order_requires_auth(): void
  176. {
  177. $testData = $this->createTestData();
  178. $response = $this->publicGet(
  179. '/api/shop/customer-orders/'.$testData['order1']->id
  180. );
  181. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  182. }
  183. // ── Response Shape ────────────────────────────────────────
  184. /**
  185. * Test: Order response includes financial fields
  186. */
  187. public function test_order_response_includes_financial_fields(): void
  188. {
  189. $testData = $this->createTestData();
  190. $response = $this->authenticatedGet(
  191. $testData['customer'],
  192. '/api/shop/customer-orders/'.$testData['order1']->id
  193. );
  194. $response->assertOk();
  195. $json = $response->json();
  196. expect($json)->toHaveKey('grandTotal');
  197. expect($json)->toHaveKey('baseGrandTotal');
  198. expect($json)->toHaveKey('subTotal');
  199. expect($json)->toHaveKey('baseSubTotal');
  200. expect($json)->toHaveKey('taxAmount');
  201. expect($json)->toHaveKey('shippingAmount');
  202. expect($json)->toHaveKey('discountAmount');
  203. }
  204. /**
  205. * Test: Collection returns orders with correct statuses
  206. */
  207. public function test_collection_returns_orders_with_correct_statuses(): void
  208. {
  209. $testData = $this->createTestData();
  210. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-orders');
  211. $response->assertOk();
  212. $json = $response->json();
  213. $statuses = array_column($json, 'status');
  214. expect($statuses)->toContain('pending');
  215. expect($statuses)->toContain('completed');
  216. }
  217. }