CartRemoveTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 CartRemoveTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. private string $graphqlUrl = '/graphql';
  13. /**
  14. * Create customer with cart and 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' => 2,
  26. ]);
  27. return compact('customer', 'token', 'cart', 'product', 'cartItem');
  28. }
  29. /**
  30. * Remove cart item mutation
  31. */
  32. private function removeCartItem(string $token, int $cartItemId): array
  33. {
  34. $mutation = <<<'GQL'
  35. mutation removeCartItem($token: String!, $cartItemId: Int!) {
  36. removeItemCartToken(input: {
  37. token: $token
  38. cartItemId: $cartItemId
  39. }) {
  40. success
  41. message
  42. cart {
  43. id
  44. itemsCount
  45. items {
  46. id
  47. productId
  48. quantity
  49. }
  50. formattedGrandTotal
  51. }
  52. }
  53. }
  54. GQL;
  55. return $this->postJson($this->graphqlUrl, [
  56. 'query' => $mutation,
  57. 'variables' => compact('token', 'cartItemId'),
  58. ])->json();
  59. }
  60. /**
  61. * Test: Remove item from cart
  62. */
  63. public function test_remove_item_from_cart(): void
  64. {
  65. ['token' => $token, 'cartItem' => $cartItem, 'cart' => $cart] = $this->createCustomerWithCart();
  66. $response = $this->removeCartItem($token, $cartItem->id);
  67. $this->assertTrue($response['data']['removeItemCartToken']['success']);
  68. $this->assertDatabaseMissing('cart_items', [
  69. 'id' => $cartItem->id,
  70. ]);
  71. }
  72. /**
  73. * Test: Items count decreases after removal
  74. */
  75. public function test_items_count_decreases_after_removal(): void
  76. {
  77. ['token' => $token, 'cartItem' => $cartItem, 'cart' => $cart] = $this->createCustomerWithCart();
  78. // Add second item
  79. $product2 = Product::factory()->create();
  80. $cartItem2 = CartItem::factory()->create([
  81. 'cart_id' => $cart->id,
  82. 'product_id' => $product2->id,
  83. 'quantity' => 1,
  84. ]);
  85. $response = $this->removeCartItem($token, $cartItem->id);
  86. // Items count should be reduced
  87. $this->assertTrue($response['data']['removeItemCartToken']['success']);
  88. $this->assertCount(1, $response['data']['removeItemCartToken']['cart']['items']);
  89. }
  90. /**
  91. * Test: Fails to remove with invalid token
  92. */
  93. public function test_fails_to_remove_with_invalid_token(): void
  94. {
  95. ['cartItem' => $cartItem] = $this->createCustomerWithCart();
  96. $response = $this->removeCartItem('invalid-token', $cartItem->id);
  97. $this->assertArrayHasKey('errors', $response);
  98. }
  99. /**
  100. * Test: Fails to remove non-existent item
  101. */
  102. public function test_fails_to_remove_non_existent_item(): void
  103. {
  104. ['token' => $token] = $this->createCustomerWithCart();
  105. $response = $this->removeCartItem($token, 99999);
  106. $this->assertArrayHasKey('errors', $response);
  107. }
  108. /**
  109. * Test: Fails to remove other customer's item
  110. */
  111. public function test_fails_to_remove_other_customer_item(): void
  112. {
  113. ['cartItem' => $cartItem] = $this->createCustomerWithCart();
  114. $otherCustomer = Customer::factory()->create();
  115. $otherToken = $otherCustomer->createToken('api-token')->plainTextToken;
  116. $response = $this->removeCartItem($otherToken, $cartItem->id);
  117. $this->assertArrayHasKey('errors', $response);
  118. }
  119. /**
  120. * Test: Remove preserves other items
  121. */
  122. public function test_remove_preserves_other_items(): void
  123. {
  124. ['token' => $token, 'cartItem' => $cartItem, 'cart' => $cart] = $this->createCustomerWithCart();
  125. // Add second item
  126. $product2 = Product::factory()->create();
  127. $cartItem2 = CartItem::factory()->create([
  128. 'cart_id' => $cart->id,
  129. 'product_id' => $product2->id,
  130. 'quantity' => 3,
  131. ]);
  132. $response = $this->removeCartItem($token, $cartItem->id);
  133. // Other item should still exist
  134. $remainingItems = $response['data']['removeItemCartToken']['cart']['items'];
  135. $this->assertCount(1, $remainingItems);
  136. $this->assertEquals($product2->id, $remainingItems[0]['productId']);
  137. }
  138. /**
  139. * Test: Cart totals update after removal
  140. */
  141. public function test_cart_totals_update_after_removal(): void
  142. {
  143. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  144. $response = $this->removeCartItem($token, $cartItem->id);
  145. $this->assertArrayHasKey('formattedGrandTotal', $response['data']['removeItemCartToken']['cart']);
  146. }
  147. /**
  148. * Test: Removing all items shows empty cart
  149. */
  150. public function test_removing_all_items_shows_empty_cart(): void
  151. {
  152. ['token' => $token, 'cartItem' => $cartItem] = $this->createCustomerWithCart();
  153. $response = $this->removeCartItem($token, $cartItem->id);
  154. $this->assertTrue($response['data']['removeItemCartToken']['success']);
  155. $this->assertCount(0, $response['data']['removeItemCartToken']['cart']['items']);
  156. }
  157. }