CartReadTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 CartReadTest 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. * Get cart query
  31. */
  32. private function getCart(string $token, int $cartId): array
  33. {
  34. $mutation = <<<'GQL'
  35. query getCart($token: String!, $cartId: Int!) {
  36. readCartToken(input: {
  37. token: $token
  38. cartId: $cartId
  39. }) {
  40. cart {
  41. id
  42. cartToken
  43. customerId
  44. itemsCount
  45. items {
  46. id
  47. productId
  48. name
  49. quantity
  50. price
  51. formattedPrice
  52. formattedTotal
  53. options
  54. }
  55. subtotal
  56. discountAmount
  57. taxAmount
  58. shippingAmount
  59. grandTotal
  60. formattedSubtotal
  61. formattedDiscountAmount
  62. formattedTaxAmount
  63. formattedShippingAmount
  64. formattedGrandTotal
  65. couponCode
  66. }
  67. }
  68. }
  69. GQL;
  70. return $this->postJson($this->graphqlUrl, [
  71. 'query' => $mutation,
  72. 'variables' => compact('token', 'cartId'),
  73. ])->json();
  74. }
  75. /**
  76. * Get all carts query
  77. */
  78. private function getAllCarts(string $token): array
  79. {
  80. $mutation = <<<'GQL'
  81. query getAllCarts($token: String!) {
  82. cartCollectionCartToken(input: {
  83. token: $token
  84. }) {
  85. carts {
  86. id
  87. cartToken
  88. customerId
  89. itemsCount
  90. items {
  91. name
  92. quantity
  93. }
  94. formattedGrandTotal
  95. }
  96. }
  97. }
  98. GQL;
  99. return $this->postJson($this->graphqlUrl, [
  100. 'query' => $mutation,
  101. 'variables' => compact('token'),
  102. ])->json();
  103. }
  104. /**
  105. * Test: Read single cart
  106. */
  107. public function test_read_single_cart(): void
  108. {
  109. ['token' => $token, 'cart' => $cart] = $this->createCustomerWithCart();
  110. $response = $this->getCart($token, $cart->id);
  111. $this->assertArrayHasKey('data', $response);
  112. $this->assertArrayHasKey('cart', $response['data']['readCartToken']);
  113. $cartData = $response['data']['readCartToken']['cart'];
  114. $this->assertEquals($cart->id, $cartData['id']);
  115. $this->assertCount(1, $cartData['items']);
  116. }
  117. /**
  118. * Test: Cart includes all required fields
  119. */
  120. public function test_cart_includes_all_required_fields(): void
  121. {
  122. ['token' => $token, 'cart' => $cart] = $this->createCustomerWithCart();
  123. $response = $this->getCart($token, $cart->id);
  124. $cartData = $response['data']['readCartToken']['cart'];
  125. $requiredFields = ['id', 'cartToken', 'customerId', 'itemsCount', 'items', 'subtotal', 'grandTotal', 'formattedGrandTotal'];
  126. foreach ($requiredFields as $field) {
  127. $this->assertArrayHasKey($field, $cartData);
  128. }
  129. }
  130. /**
  131. * Test: Cart items are properly formatted
  132. */
  133. public function test_cart_items_are_properly_formatted(): void
  134. {
  135. ['token' => $token, 'cart' => $cart] = $this->createCustomerWithCart();
  136. $response = $this->getCart($token, $cart->id);
  137. $item = $response['data']['readCartToken']['cart']['items'][0];
  138. $this->assertArrayHasKey('id', $item);
  139. $this->assertArrayHasKey('productId', $item);
  140. $this->assertArrayHasKey('name', $item);
  141. $this->assertArrayHasKey('quantity', $item);
  142. $this->assertArrayHasKey('formattedPrice', $item);
  143. $this->assertArrayHasKey('formattedTotal', $item);
  144. }
  145. /**
  146. * Test: Fails to read with invalid token
  147. */
  148. public function test_fails_to_read_with_invalid_token(): void
  149. {
  150. ['cart' => $cart] = $this->createCustomerWithCart();
  151. $response = $this->getCart('invalid-token', $cart->id);
  152. $this->assertArrayHasKey('errors', $response);
  153. }
  154. /**
  155. * Test: Fails to read non-existent cart
  156. */
  157. public function test_fails_to_read_non_existent_cart(): void
  158. {
  159. ['token' => $token] = $this->createCustomerWithCart();
  160. $response = $this->getCart($token, 99999);
  161. $this->assertArrayHasKey('errors', $response);
  162. }
  163. /**
  164. * Test: Fails to read other customer's cart
  165. */
  166. public function test_fails_to_read_other_customer_cart(): void
  167. {
  168. ['cart' => $cart] = $this->createCustomerWithCart();
  169. $otherCustomer = Customer::factory()->create();
  170. $otherToken = $otherCustomer->createToken('api-token')->plainTextToken;
  171. $response = $this->getCart($otherToken, $cart->id);
  172. $this->assertArrayHasKey('errors', $response);
  173. }
  174. /**
  175. * Test: Get all carts for authenticated user
  176. */
  177. public function test_get_all_carts_for_authenticated_user(): void
  178. {
  179. ['token' => $token, 'customer' => $customer] = $this->createCustomerWithCart();
  180. // Create second cart
  181. $cart2 = Cart::factory()->create(['customer_id' => $customer->id]);
  182. $product2 = Product::factory()->create();
  183. CartItem::factory()->create([
  184. 'cart_id' => $cart2->id,
  185. 'product_id' => $product2->id,
  186. 'quantity' => 1,
  187. ]);
  188. $response = $this->getAllCarts($token);
  189. $this->assertArrayHasKey('carts', $response['data']['cartCollectionCartToken']);
  190. $carts = $response['data']['cartCollectionCartToken']['carts'];
  191. $this->assertCount(2, $carts);
  192. }
  193. /**
  194. * Test: Get carts returns correct structure
  195. */
  196. public function test_get_carts_returns_correct_structure(): void
  197. {
  198. ['token' => $token, 'customer' => $customer] = $this->createCustomerWithCart();
  199. $response = $this->getAllCarts($token);
  200. $this->assertArrayHasKey('carts', $response['data']['cartCollectionCartToken']);
  201. $carts = $response['data']['cartCollectionCartToken']['carts'];
  202. $this->assertCount(1, $carts);
  203. $cart = $carts[0];
  204. $requiredFields = ['id', 'cartToken', 'customerId', 'itemsCount', 'items', 'formattedGrandTotal'];
  205. foreach ($requiredFields as $field) {
  206. $this->assertArrayHasKey($field, $cart);
  207. }
  208. }
  209. /**
  210. * Test: Guest cart can be read with cart id as token
  211. */
  212. public function test_guest_cart_can_be_read_with_cart_id_as_token(): void
  213. {
  214. $guestCart = Cart::factory()->create(['customer_id' => null]);
  215. $product = Product::factory()->create();
  216. CartItem::factory()->create([
  217. 'cart_id' => $guestCart->id,
  218. 'product_id' => $product->id,
  219. 'quantity' => 1,
  220. ]);
  221. $response = $this->getCart((string) $guestCart->id, $guestCart->id);
  222. $this->assertArrayHasKey('data', $response);
  223. $this->assertEquals($guestCart->id, $response['data']['readCartToken']['cart']['id']);
  224. }
  225. /**
  226. * Test: Cart totals are correct
  227. */
  228. public function test_cart_totals_are_correct(): void
  229. {
  230. ['token' => $token, 'cart' => $cart] = $this->createCustomerWithCart();
  231. $response = $this->getCart($token, $cart->id);
  232. $cartData = $response['data']['readCartToken']['cart'];
  233. $this->assertIsString($cartData['formattedGrandTotal']);
  234. $this->assertNotEmpty($cartData['grandTotal']);
  235. }
  236. }