CustomerOrderReorderTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
  3. use Webkul\BagistoApi\Tests\GraphQLTestCase;
  4. use Webkul\Core\Models\Channel;
  5. use Webkul\Customer\Models\Customer;
  6. use Webkul\Product\Models\Product;
  7. use Webkul\Sales\Models\Order;
  8. use Webkul\Sales\Models\OrderItem;
  9. use Webkul\Sales\Models\OrderPayment;
  10. class CustomerOrderReorderTest extends GraphQLTestCase
  11. {
  12. /**
  13. * Create test data — customer with completed order
  14. */
  15. private function createTestData(): array
  16. {
  17. $this->seedRequiredData();
  18. $customer = $this->createCustomer();
  19. $channel = Channel::first();
  20. // Create multiple products for testing
  21. $product1 = Product::factory()->create(['sku' => 'REORDER-PROD-1']);
  22. $product2 = Product::factory()->create(['sku' => 'REORDER-PROD-2']);
  23. // Create a completed order with multiple items
  24. $completedOrder = Order::factory()->create([
  25. 'customer_id' => $customer->id,
  26. 'customer_email' => $customer->email,
  27. 'customer_first_name' => $customer->first_name,
  28. 'customer_last_name' => $customer->last_name,
  29. 'channel_id' => $channel->id,
  30. 'status' => 'completed',
  31. ]);
  32. OrderItem::factory()->create([
  33. 'order_id' => $completedOrder->id,
  34. 'product_id' => $product1->id,
  35. 'sku' => 'REORDER-PROD-1',
  36. 'type' => 'simple',
  37. 'name' => 'Reorder Product 1',
  38. 'qty_ordered' => 2,
  39. ]);
  40. OrderItem::factory()->create([
  41. 'order_id' => $completedOrder->id,
  42. 'product_id' => $product2->id,
  43. 'sku' => 'REORDER-PROD-2',
  44. 'type' => 'simple',
  45. 'name' => 'Reorder Product 2',
  46. 'qty_ordered' => 1,
  47. ]);
  48. OrderPayment::factory()->create([
  49. 'order_id' => $completedOrder->id,
  50. ]);
  51. // Create a pending order (also reorderable)
  52. $pendingOrder = Order::factory()->create([
  53. 'customer_id' => $customer->id,
  54. 'customer_email' => $customer->email,
  55. 'customer_first_name' => $customer->first_name,
  56. 'customer_last_name' => $customer->last_name,
  57. 'channel_id' => $channel->id,
  58. 'status' => 'pending',
  59. ]);
  60. OrderItem::factory()->create([
  61. 'order_id' => $pendingOrder->id,
  62. 'product_id' => $product1->id,
  63. 'sku' => 'REORDER-PROD-1',
  64. 'type' => 'simple',
  65. 'name' => 'Reorder Product 1',
  66. 'qty_ordered' => 1,
  67. ]);
  68. OrderPayment::factory()->create([
  69. 'order_id' => $pendingOrder->id,
  70. ]);
  71. return compact('customer', 'completedOrder', 'pendingOrder', 'product1', 'product2', 'channel');
  72. }
  73. // ── Reorder Mutation Tests ────────────────────────────────────
  74. /**
  75. * Test: Reorder a completed order with available products
  76. */
  77. public function test_reorder_completed_order_success(): void
  78. {
  79. $testData = $this->createTestData();
  80. $mutation = <<<'GQL'
  81. mutation reorderOrder($input: ReorderInput!) {
  82. reorderOrder(input: $input) {
  83. success
  84. message
  85. orderId
  86. itemsAddedCount
  87. }
  88. }
  89. GQL;
  90. $variables = [
  91. 'input' => [
  92. 'orderId' => $testData['completedOrder']->id,
  93. ],
  94. ];
  95. $response = $this->authenticatedGraphQL(
  96. $testData['customer'],
  97. $mutation,
  98. $variables
  99. );
  100. $response->assertJson([
  101. 'data' => [
  102. 'reorderOrder' => [
  103. 'success' => true,
  104. 'orderId' => $testData['completedOrder']->id,
  105. ],
  106. ],
  107. ]);
  108. // Verify items were added to cart
  109. $this->assertGreaterThan(0, $response['data']['reorderOrder']['itemsAddedCount']);
  110. }
  111. /**
  112. * Test: Reorder returns correct item count
  113. */
  114. public function test_reorder_returns_correct_item_count(): void
  115. {
  116. $testData = $this->createTestData();
  117. $mutation = <<<'GQL'
  118. mutation reorderOrder($input: ReorderInput!) {
  119. reorderOrder(input: $input) {
  120. success
  121. message
  122. orderId
  123. itemsAddedCount
  124. }
  125. }
  126. GQL;
  127. $variables = [
  128. 'input' => [
  129. 'orderId' => $testData['completedOrder']->id,
  130. ],
  131. ];
  132. $response = $this->authenticatedGraphQL(
  133. $testData['customer'],
  134. $mutation,
  135. $variables
  136. );
  137. // The completed order has 2 items
  138. $itemsAddedCount = $response['data']['reorderOrder']['itemsAddedCount'];
  139. $this->assertEquals(2, $itemsAddedCount);
  140. }
  141. /**
  142. * Test: Cannot reorder non-existent order
  143. */
  144. public function test_cannot_reorder_non_existent_order(): void
  145. {
  146. $testData = $this->createTestData();
  147. $mutation = <<<'GQL'
  148. mutation reorderOrder($input: ReorderInput!) {
  149. reorderOrder(input: $input) {
  150. success
  151. message
  152. orderId
  153. }
  154. }
  155. GQL;
  156. $variables = [
  157. 'input' => [
  158. 'orderId' => 99999, // Non-existent ID
  159. ],
  160. ];
  161. $response = $this->authenticatedGraphQL(
  162. $testData['customer'],
  163. $mutation,
  164. $variables
  165. );
  166. // Should return an error
  167. $this->assertArrayHasKey('errors', $response);
  168. }
  169. /**
  170. * Test: Cannot reorder order of another customer
  171. */
  172. public function test_cannot_reorder_other_customers_order(): void
  173. {
  174. $testData = $this->createTestData();
  175. $otherCustomer = $this->createCustomer();
  176. $mutation = <<<'GQL'
  177. mutation reorderOrder($input: ReorderInput!) {
  178. reorderOrder(input: $input) {
  179. success
  180. message
  181. orderId
  182. }
  183. }
  184. GQL;
  185. $variables = [
  186. 'input' => [
  187. 'orderId' => $testData['completedOrder']->id,
  188. ],
  189. ];
  190. $response = $this->authenticatedGraphQL(
  191. $otherCustomer,
  192. $mutation,
  193. $variables
  194. );
  195. // Should return an error
  196. $this->assertArrayHasKey('errors', $response);
  197. }
  198. /**
  199. * Test: Missing order ID should return error
  200. */
  201. public function test_reorder_without_order_id(): void
  202. {
  203. $testData = $this->createTestData();
  204. $mutation = <<<'GQL'
  205. mutation reorderOrder($input: ReorderInput!) {
  206. reorderOrder(input: $input) {
  207. success
  208. message
  209. }
  210. }
  211. GQL;
  212. $variables = [
  213. 'input' => [],
  214. ];
  215. $response = $this->authenticatedGraphQL(
  216. $testData['customer'],
  217. $mutation,
  218. $variables
  219. );
  220. // Should return an error for missing orderId
  221. $this->assertArrayHasKey('errors', $response);
  222. }
  223. /**
  224. * Test: Unauthenticated reorder request fails
  225. */
  226. public function test_unauthenticated_reorder_fails(): void
  227. {
  228. $testData = $this->createTestData();
  229. $mutation = <<<'GQL'
  230. mutation reorderOrder($input: ReorderInput!) {
  231. reorderOrder(input: $input) {
  232. success
  233. message
  234. }
  235. }
  236. GQL;
  237. $variables = [
  238. 'input' => [
  239. 'orderId' => $testData['completedOrder']->id,
  240. ],
  241. ];
  242. // Call without authentication
  243. $response = $this->graphQL($mutation, $variables);
  244. // GraphQL returns response as array from postJson
  245. $data = $response->json();
  246. $this->assertArrayHasKey('errors', $data);
  247. $this->assertTrue(
  248. isset($data['errors'][0]['message']),
  249. 'Expected GraphQL error message to be present'
  250. );
  251. }
  252. /**
  253. * Test: Reorder response includes all expected fields
  254. */
  255. public function test_reorder_response_includes_all_fields(): void
  256. {
  257. $testData = $this->createTestData();
  258. }
  259. /**
  260. * Test: Reorder a pending order
  261. */
  262. public function test_reorder_pending_order_success(): void
  263. {
  264. $testData = $this->createTestData();
  265. $mutation = <<<'GQL'
  266. mutation reorderOrder($input: ReorderInput!) {
  267. reorderOrder(input: $input) {
  268. success
  269. message
  270. orderId
  271. itemsAddedCount
  272. }
  273. }
  274. GQL;
  275. $variables = [
  276. 'input' => [
  277. 'orderId' => $testData['pendingOrder']->id,
  278. ],
  279. ];
  280. $response = $this->authenticatedGraphQL(
  281. $testData['customer'],
  282. $mutation,
  283. $variables
  284. );
  285. $response->assertJson([
  286. 'data' => [
  287. 'reorderOrder' => [
  288. 'success' => true,
  289. 'orderId' => $testData['pendingOrder']->id,
  290. ],
  291. ],
  292. ]);
  293. // Should have added 1 item
  294. $this->assertEquals(1, $response['data']['reorderOrder']['itemsAddedCount']);
  295. }
  296. }