CustomerInvoiceRestTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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\Invoice;
  7. use Webkul\Sales\Models\InvoiceItem;
  8. use Webkul\Sales\Models\Order;
  9. use Webkul\Sales\Models\OrderItem;
  10. use Webkul\Sales\Models\OrderPayment;
  11. class CustomerInvoiceRestTest extends RestApiTestCase
  12. {
  13. /**
  14. * Create test data — customer with order and invoices
  15. */
  16. private function createTestData(): array
  17. {
  18. $this->seedRequiredData();
  19. $customer = $this->createCustomer();
  20. $channel = Channel::first();
  21. $product = Product::factory()->create();
  22. $order = Order::factory()->create([
  23. 'customer_id' => $customer->id,
  24. 'customer_email' => $customer->email,
  25. 'customer_first_name' => $customer->first_name,
  26. 'customer_last_name' => $customer->last_name,
  27. 'channel_id' => $channel->id,
  28. 'status' => 'completed',
  29. ]);
  30. $orderItem = OrderItem::factory()->create([
  31. 'order_id' => $order->id,
  32. 'product_id' => $product->id,
  33. 'sku' => 'TEST-INV-SKU-001',
  34. 'type' => 'simple',
  35. 'name' => 'Test Invoice Product',
  36. ]);
  37. OrderPayment::factory()->create([
  38. 'order_id' => $order->id,
  39. ]);
  40. $invoice1 = Invoice::factory()->create([
  41. 'order_id' => $order->id,
  42. 'state' => 'paid',
  43. 'total_qty' => 2,
  44. 'sub_total' => 100.00,
  45. 'base_sub_total' => 100.00,
  46. 'grand_total' => 110.00,
  47. 'base_grand_total' => 110.00,
  48. 'shipping_amount' => 5.00,
  49. 'base_shipping_amount' => 5.00,
  50. 'tax_amount' => 5.00,
  51. 'base_tax_amount' => 5.00,
  52. 'discount_amount' => 0.00,
  53. 'base_discount_amount' => 0.00,
  54. 'base_currency_code' => 'USD',
  55. 'order_currency_code' => 'USD',
  56. 'increment_id' => 'INV-REST-001',
  57. ]);
  58. InvoiceItem::factory()->create([
  59. 'invoice_id' => $invoice1->id,
  60. 'order_item_id' => $orderItem->id,
  61. 'name' => 'Test Invoice Product',
  62. 'sku' => 'TEST-INV-SKU-001',
  63. 'qty' => 2,
  64. 'price' => 50.00,
  65. 'base_price' => 50.00,
  66. 'total' => 100.00,
  67. 'base_total' => 100.00,
  68. ]);
  69. $invoice2 = Invoice::factory()->create([
  70. 'order_id' => $order->id,
  71. 'state' => 'pending',
  72. 'total_qty' => 1,
  73. 'sub_total' => 50.00,
  74. 'base_sub_total' => 50.00,
  75. 'grand_total' => 55.00,
  76. 'base_grand_total' => 55.00,
  77. 'shipping_amount' => 3.00,
  78. 'base_shipping_amount' => 3.00,
  79. 'tax_amount' => 2.00,
  80. 'base_tax_amount' => 2.00,
  81. 'discount_amount' => 0.00,
  82. 'base_discount_amount' => 0.00,
  83. 'base_currency_code' => 'USD',
  84. 'order_currency_code' => 'USD',
  85. 'increment_id' => 'INV-REST-002',
  86. ]);
  87. InvoiceItem::factory()->create([
  88. 'invoice_id' => $invoice2->id,
  89. 'order_item_id' => $orderItem->id,
  90. 'name' => 'Test Invoice Product',
  91. 'sku' => 'TEST-INV-SKU-001',
  92. 'qty' => 1,
  93. 'price' => 50.00,
  94. 'base_price' => 50.00,
  95. 'total' => 50.00,
  96. 'base_total' => 50.00,
  97. ]);
  98. return compact('customer', 'channel', 'product', 'order', 'orderItem', 'invoice1', 'invoice2');
  99. }
  100. // ── Collection ────────────────────────────────────────────
  101. /**
  102. * Test: GET /api/shop/customer-invoices returns collection
  103. */
  104. public function test_get_customer_invoices_collection(): void
  105. {
  106. $testData = $this->createTestData();
  107. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-invoices');
  108. $response->assertOk();
  109. $json = $response->json();
  110. expect($json)->toBeArray();
  111. expect(count($json))->toBeGreaterThanOrEqual(2);
  112. }
  113. /**
  114. * Test: GET /api/shop/customer-invoices without auth returns error
  115. */
  116. public function test_get_customer_invoices_requires_auth(): void
  117. {
  118. $this->seedRequiredData();
  119. $response = $this->publicGet('/api/shop/customer-invoices');
  120. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  121. }
  122. /**
  123. * Test: Customer only sees invoices from own orders
  124. */
  125. public function test_customer_only_sees_own_invoices(): void
  126. {
  127. $testData = $this->createTestData();
  128. /** Create another customer with their own order and invoice */
  129. $otherCustomer = $this->createCustomer();
  130. $channel = Channel::first();
  131. $otherOrder = Order::factory()->create([
  132. 'customer_id' => $otherCustomer->id,
  133. 'customer_email' => $otherCustomer->email,
  134. 'customer_first_name' => $otherCustomer->first_name,
  135. 'customer_last_name' => $otherCustomer->last_name,
  136. 'channel_id' => $channel->id,
  137. 'status' => 'completed',
  138. ]);
  139. Invoice::factory()->create([
  140. 'order_id' => $otherOrder->id,
  141. 'state' => 'paid',
  142. 'grand_total' => 200.00,
  143. 'base_grand_total' => 200.00,
  144. ]);
  145. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-invoices');
  146. $response->assertOk();
  147. $json = $response->json();
  148. /** Should only see the 2 invoices belonging to testData customer's orders */
  149. expect(count($json))->toBe(2);
  150. }
  151. /**
  152. * Test: Customer with no invoices returns empty collection
  153. */
  154. public function test_customer_with_no_invoices_returns_empty(): void
  155. {
  156. $this->seedRequiredData();
  157. $customer = $this->createCustomer();
  158. $response = $this->authenticatedGet($customer, '/api/shop/customer-invoices');
  159. $response->assertOk();
  160. $json = $response->json();
  161. expect($json)->toBeArray();
  162. expect(count($json))->toBe(0);
  163. }
  164. // ── Single Item ───────────────────────────────────────────
  165. /**
  166. * Test: GET /api/shop/customer-invoices/{id} returns single invoice
  167. */
  168. public function test_get_single_customer_invoice(): void
  169. {
  170. $testData = $this->createTestData();
  171. $response = $this->authenticatedGet(
  172. $testData['customer'],
  173. '/api/shop/customer-invoices/'.$testData['invoice1']->id
  174. );
  175. $response->assertOk();
  176. $json = $response->json();
  177. expect($json)->toHaveKey('id');
  178. expect($json)->toHaveKey('incrementId');
  179. expect($json)->toHaveKey('state');
  180. expect($json)->toHaveKey('totalQty');
  181. expect($json)->toHaveKey('grandTotal');
  182. expect($json)->toHaveKey('subTotal');
  183. expect($json)->toHaveKey('shippingAmount');
  184. expect($json)->toHaveKey('taxAmount');
  185. expect($json)->toHaveKey('discountAmount');
  186. expect($json)->toHaveKey('baseCurrencyCode');
  187. expect($json)->toHaveKey('orderCurrencyCode');
  188. expect($json)->toHaveKey('createdAt');
  189. expect($json['id'])->toBe($testData['invoice1']->id);
  190. expect($json['state'])->toBe('paid');
  191. }
  192. /**
  193. * Test: GET /api/shop/customer-invoices/{id} with invalid id returns 404
  194. */
  195. public function test_get_customer_invoice_not_found(): void
  196. {
  197. $this->seedRequiredData();
  198. $customer = $this->createCustomer();
  199. $response = $this->authenticatedGet($customer, '/api/shop/customer-invoices/999999');
  200. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  201. }
  202. /**
  203. * Test: Cannot access another customer's invoice by ID
  204. */
  205. public function test_cannot_access_other_customers_invoice(): void
  206. {
  207. $testData = $this->createTestData();
  208. $otherCustomer = $this->createCustomer();
  209. $response = $this->authenticatedGet(
  210. $otherCustomer,
  211. '/api/shop/customer-invoices/'.$testData['invoice1']->id
  212. );
  213. /** Should return 404/500 because the invoice doesn't belong to otherCustomer */
  214. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  215. }
  216. /**
  217. * Test: Single invoice without auth returns error
  218. */
  219. public function test_get_single_invoice_requires_auth(): void
  220. {
  221. $testData = $this->createTestData();
  222. $response = $this->publicGet(
  223. '/api/shop/customer-invoices/'.$testData['invoice1']->id
  224. );
  225. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  226. }
  227. // ── Response Shape ────────────────────────────────────────
  228. /**
  229. * Test: Invoice response includes financial fields
  230. */
  231. public function test_invoice_response_includes_financial_fields(): void
  232. {
  233. $testData = $this->createTestData();
  234. $response = $this->authenticatedGet(
  235. $testData['customer'],
  236. '/api/shop/customer-invoices/'.$testData['invoice1']->id
  237. );
  238. $response->assertOk();
  239. $json = $response->json();
  240. expect($json)->toHaveKey('grandTotal');
  241. expect($json)->toHaveKey('baseGrandTotal');
  242. expect($json)->toHaveKey('subTotal');
  243. expect($json)->toHaveKey('baseSubTotal');
  244. expect($json)->toHaveKey('taxAmount');
  245. expect($json)->toHaveKey('shippingAmount');
  246. expect($json)->toHaveKey('discountAmount');
  247. }
  248. /**
  249. * Test: Collection returns invoices with correct states
  250. */
  251. public function test_collection_returns_invoices_with_correct_states(): void
  252. {
  253. $testData = $this->createTestData();
  254. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-invoices');
  255. $response->assertOk();
  256. $json = $response->json();
  257. $states = array_column($json, 'state');
  258. expect($states)->toContain('paid');
  259. expect($states)->toContain('pending');
  260. }
  261. // ── PDF Download ──────────────────────────────────────────
  262. /**
  263. * Test: PDF download endpoint returns PDF response
  264. */
  265. public function test_pdf_download_returns_pdf(): void
  266. {
  267. $testData = $this->createTestData();
  268. $response = $this->authenticatedGet(
  269. $testData['customer'],
  270. '/api/shop/customer-invoices/'.$testData['invoice1']->id.'/pdf'
  271. );
  272. $response->assertOk();
  273. $contentType = $response->headers->get('Content-Type');
  274. expect($contentType)->toContain('pdf');
  275. }
  276. /**
  277. * Test: PDF download without auth returns error
  278. */
  279. public function test_pdf_download_requires_auth(): void
  280. {
  281. $testData = $this->createTestData();
  282. $response = $this->publicGet(
  283. '/api/shop/customer-invoices/'.$testData['invoice1']->id.'/pdf'
  284. );
  285. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  286. }
  287. /**
  288. * Test: PDF download for non-existent invoice returns error
  289. */
  290. public function test_pdf_download_invoice_not_found(): void
  291. {
  292. $this->seedRequiredData();
  293. $customer = $this->createCustomer();
  294. $response = $this->authenticatedGet($customer, '/api/shop/customer-invoices/999999/pdf');
  295. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  296. }
  297. /**
  298. * Test: Cannot download another customer's invoice PDF
  299. */
  300. public function test_cannot_download_other_customers_invoice_pdf(): void
  301. {
  302. $testData = $this->createTestData();
  303. $otherCustomer = $this->createCustomer();
  304. $response = $this->authenticatedGet(
  305. $otherCustomer,
  306. '/api/shop/customer-invoices/'.$testData['invoice1']->id.'/pdf'
  307. );
  308. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  309. }
  310. }