CartUpdateTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature;
  3. use Illuminate\Foundation\Testing\RefreshDatabase;
  4. use Tests\TestCase;
  5. use Webkul\Checkout\Models\Cart;
  6. use Webkul\Checkout\Models\CartItem;
  7. use Webkul\Customer\Models\Customer;
  8. use Webkul\Product\Models\Product;
  9. class CartUpdateTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. private string $graphqlUrl = '/graphql';
  13. /**
  14. * Create customer with Sanctum token and cart with items
  15. */
  16. private function createCustomerWithCart(): array
  17. {
  18. $customer = Customer::factory()->create();
  19. $token = $customer->createToken('api-token')->plainTextToken;
  20. $cart = Cart::factory()->create(['customer_id' => $customer->id]);
  21. $product = Product::factory()->create();
  22. $cartItem = CartItem::factory()->create([
  23. 'cart_id' => $cart->id,
  24. 'product_id' => $product->id,
  25. 'quantity' => 1,
  26. ]);
  27. return compact('customer', 'token', 'cart', 'product', 'cartItem');
  28. }
  29. /**
  30. * Update cart item mutation
  31. */
  32. private function updateCartItem(string $token, int $cartItemId, int $quantity): array
  33. {
  34. $mutation = <<<'GQL'
  35. mutation updateCartItem($token: String!, $cartItemId: Int!, $quantity: Int!) {
  36. updateItemCartToken(input: {
  37. token: $token
  38. cartItemId: $cartItemId
  39. quantity: $quantity
  40. }) {
  41. success
  42. message
  43. cart {
  44. id
  45. itemsCount
  46. items {
  47. id
  48. quantity
  49. formattedTotal
  50. }
  51. formattedGrandTotal
  52. }
  53. }
  54. }
  55. GQL;
  56. return $this->postJson($this->graphqlUrl, [
  57. 'query' => $mutation,
  58. 'variables' => compact('token', 'cartItemId', 'quantity'),
  59. ])->json();
  60. }
  61. /**
  62. * Test: Update cart item quantity
  63. */
  64. public function test_update_cart_item_quantity(): void
  65. {
  66. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  67. $response = $this->updateCartItem($token, $cartItem->id, 5);
  68. $this->assertTrue($response['data']['updateItemCartToken']['success']);
  69. $this->assertEquals(5, $response['data']['updateItemCartToken']['cart']['items'][0]['quantity']);
  70. $this->assertDatabaseHas('cart_items', [
  71. 'id' => $cartItem->id,
  72. 'quantity' => 5,
  73. ]);
  74. }
  75. /**
  76. * Test: Update multiple items in cart
  77. */
  78. public function test_update_multiple_items_in_cart(): void
  79. {
  80. ['customer' => $customer, 'token' => $token, 'cart' => $cart] = $this->createCustomerWithCart();
  81. $product2 = Product::factory()->create();
  82. $cartItem2 = CartItem::factory()->create([
  83. 'cart_id' => $cart->id,
  84. 'product_id' => $product2->id,
  85. 'quantity' => 2,
  86. ]);
  87. // Update first item
  88. $this->updateCartItem($token, 1, 3);
  89. // Update second item
  90. $response = $this->updateCartItem($token, $cartItem2->id, 4);
  91. $this->assertCount(2, $response['data']['updateItemCartToken']['cart']['items']);
  92. }
  93. /**
  94. * Test: Fails to update with invalid token
  95. */
  96. public function test_fails_to_update_with_invalid_token(): void
  97. {
  98. ['cartItem' => $cartItem] = $this->createCustomerWithCart();
  99. $response = $this->updateCartItem('invalid-token', $cartItem->id, 5);
  100. $this->assertArrayHasKey('errors', $response);
  101. }
  102. /**
  103. * Test: Fails to update non-existent item
  104. */
  105. public function test_fails_to_update_non_existent_item(): void
  106. {
  107. ['token' => $token] = $this->createCustomerWithCart();
  108. $response = $this->updateCartItem($token, 99999, 5);
  109. $this->assertArrayHasKey('errors', $response);
  110. }
  111. /**
  112. * Test: Fails to update with zero quantity
  113. */
  114. public function test_fails_to_update_with_zero_quantity(): void
  115. {
  116. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  117. $response = $this->updateCartItem($token, $cartItem->id, 0);
  118. // Should fail or handle gracefully
  119. $this->assertTrue(
  120. isset($response['errors']) || ! $response['data']['updateItemCartToken']['success']
  121. );
  122. }
  123. /**
  124. * Test: Fails to update other customer's item
  125. */
  126. public function test_fails_to_update_other_customer_item(): void
  127. {
  128. ['cartItem' => $cartItem] = $this->createCustomerWithCart();
  129. $otherCustomer = Customer::factory()->create();
  130. $otherToken = $otherCustomer->createToken('api-token')->plainTextToken;
  131. $response = $this->updateCartItem($otherToken, $cartItem->id, 5);
  132. $this->assertArrayHasKey('errors', $response);
  133. }
  134. /**
  135. * Test: Update preserves cart structure
  136. */
  137. public function test_update_preserves_cart_structure(): void
  138. {
  139. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  140. $response = $this->updateCartItem($token, $cartItem->id, 10);
  141. $cart = $response['data']['updateItemCartToken']['cart'];
  142. $this->assertArrayHasKey('id', $cart);
  143. $this->assertArrayHasKey('itemsCount', $cart);
  144. $this->assertArrayHasKey('items', $cart);
  145. $this->assertArrayHasKey('formattedGrandTotal', $cart);
  146. }
  147. /**
  148. * Test: Update decreases quantity
  149. */
  150. public function test_update_decreases_quantity(): void
  151. {
  152. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  153. // First increase quantity
  154. $this->updateCartItem($token, $cartItem->id, 10);
  155. // Then decrease it
  156. $response = $this->updateCartItem($token, $cartItem->id, 3);
  157. $this->assertEquals(3, $response['data']['updateItemCartToken']['cart']['items'][0]['quantity']);
  158. }
  159. /**
  160. * Test: Update with large quantity
  161. */
  162. public function test_update_with_large_quantity(): void
  163. {
  164. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  165. $response = $this->updateCartItem($token, $cartItem->id, 1000);
  166. $this->assertEquals(1000, $response['data']['updateItemCartToken']['cart']['items'][0]['quantity']);
  167. }
  168. /**
  169. * Test: Cart totals update correctly
  170. */
  171. public function test_cart_totals_update_correctly(): void
  172. {
  173. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  174. $response = $this->updateCartItem($token, $cartItem->id, 5);
  175. $this->assertArrayHasKey('formattedGrandTotal', $response['data']['updateItemCartToken']['cart']);
  176. $this->assertNotEmpty($response['data']['updateItemCartToken']['cart']['formattedGrandTotal']);
  177. }
  178. }