| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- <?php
- namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
- use Webkul\BagistoApi\Tests\GraphQLTestCase;
- class GuestCartTest extends GraphQLTestCase
- {
- /**
- * Get guest cart token from the createCart mutation response
- * This token is used as Bearer token for subsequent operations
- */
- private function getGuestCartToken(): string
- {
- $mutation = <<<'GQL'
- mutation createCart {
- createCartToken(input: {}) {
- cartToken {
- id
- _id
- cartToken
- customerId
- channelId
- itemsCount
- subtotal
- baseSubtotal
- discountAmount
- baseDiscountAmount
- taxAmount
- baseTaxAmount
- shippingAmount
- baseShippingAmount
- grandTotal
- baseGrandTotal
- formattedSubtotal
- formattedDiscountAmount
- formattedTaxAmount
- formattedShippingAmount
- formattedGrandTotal
- couponCode
- success
- message
- sessionToken
- isGuest
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation);
- $response->assertSuccessful();
- $data = $response->json('data.createCartToken.cartToken');
- $this->assertNotNull($data, 'cartToken response is null');
- $this->assertTrue((bool) ($data['success'] ?? false));
- // Use cartToken as the bearer token
- $token = $data['cartToken'] ?? null;
- $this->assertNotEmpty($token, 'guest cart token is missing');
- return $token;
- }
- /**
- * Helper method to get authorization headers with guest cart token
- */
- private function guestHeaders(string $token): array
- {
- return [
- 'Authorization' => 'Bearer ' . $token,
- ];
- }
- /**
- * Create Simple Cart (Guest)
- */
- public function test_create_simple_cart(): void
- {
- $token = $this->getGuestCartToken();
- $this->assertNotEmpty($token);
- }
- /**
- * Add Product In Cart (Guest)
- */
- public function test_create_add_product_in_cart_as_guest(): void
- {
- $token = $this->getGuestCartToken();
- $headers = $this->guestHeaders($token);
- // Use test product helper to get a product with inventory
- $productData = $this->createTestProduct();
- $product = $productData['product'];
- $mutation = <<<'GQL'
- mutation createAddProductInCart($productId: Int!, $quantity: Int!) {
- createAddProductInCart(input: {productId: $productId, quantity: $quantity}) {
- addProductInCart {
- id
- _id
- cartToken
- customerId
- channelId
- subtotal
- baseSubtotal
- discountAmount
- baseDiscountAmount
- taxAmount
- baseTaxAmount
- shippingAmount
- baseShippingAmount
- grandTotal
- baseGrandTotal
- formattedSubtotal
- formattedDiscountAmount
- formattedTaxAmount
- formattedShippingAmount
- formattedGrandTotal
- couponCode
- success
- message
- sessionToken
- isGuest
- itemsQty
- itemsCount
- haveStockableItems
- items {
- totalCount
- pageInfo {
- startCursor
- endCursor
- hasNextPage
- hasPreviousPage
- }
- edges {
- cursor
- node {
- id
- cartId
- productId
- name
- sku
- quantity
- price
- basePrice
- total
- baseTotal
- discountAmount
- baseDiscountAmount
- taxAmount
- baseTaxAmount
- type
- formattedPrice
- formattedTotal
- priceInclTax
- basePriceInclTax
- formattedPriceInclTax
- totalInclTax
- baseTotalInclTax
- formattedTotalInclTax
- productUrlKey
- canChangeQty
- }
- }
- }
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation, [
- 'productId' => $product->id,
- 'quantity' => 1,
- ], $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createAddProductInCart.addProductInCart');
- $this->assertNotNull($data);
- $this->assertGreaterThan(0, (int) ($data['itemsCount'] ?? 0));
- $this->assertSame($product->id, $data['items']['edges'][0]['node']['productId'] ?? null);
- $this->assertSame(1, $data['items']['edges'][0]['node']['quantity'] ?? null);
- }
- /**
- * Update Cart Item Quantity (Guest)
- */
- public function test_update_cart_item_quantity_as_guest(): void
- {
- $token = $this->getGuestCartToken();
- $headers = $this->guestHeaders($token);
- // Use test product helper to get a product with inventory
- $productData = $this->createTestProduct();
- $product = $productData['product'];
- $addMutation = <<<'GQL'
- mutation createAddProductInCart($productId: Int!, $quantity: Int!) {
- createAddProductInCart(input: {productId: $productId, quantity: $quantity}) {
- addProductInCart {
- items {
- edges {
- node {
- id
- productId
- quantity
- }
- }
- }
- }
- }
- }
- GQL;
- $addResponse = $this->graphQL($addMutation, [
- 'productId' => $product->id,
- 'quantity' => 9,
- ], $headers);
- $addResponse->assertSuccessful();
- $cartItemId = $addResponse->json('data.createAddProductInCart.addProductInCart.items.edges.0.node.id');
- $this->assertNotNull($cartItemId, 'cart item id is missing for update test');
- $updateMutation = <<<'GQL'
- mutation createUpdateCartItem($cartItemId: Int!, $quantity: Int!) {
- createUpdateCartItem(input: {cartItemId: $cartItemId, quantity: $quantity}) {
- updateCartItem {
- id
- _id
- cartToken
- customerId
- channelId
- subtotal
- baseSubtotal
- discountAmount
- baseDiscountAmount
- taxAmount
- baseTaxAmount
- shippingAmount
- baseShippingAmount
- grandTotal
- baseGrandTotal
- formattedSubtotal
- formattedDiscountAmount
- formattedTaxAmount
- formattedShippingAmount
- formattedGrandTotal
- couponCode
- items {
- totalCount
- pageInfo {
- startCursor
- endCursor
- hasNextPage
- hasPreviousPage
- }
- edges {
- cursor
- node {
- id
- cartId
- productId
- name
- sku
- quantity
- price
- basePrice
- total
- baseTotal
- discountAmount
- baseDiscountAmount
- taxAmount
- baseTaxAmount
- type
- formattedPrice
- formattedTotal
- priceInclTax
- basePriceInclTax
- formattedPriceInclTax
- totalInclTax
- baseTotalInclTax
- formattedTotalInclTax
- productUrlKey
- canChangeQty
- }
- }
- }
- success
- message
- sessionToken
- isGuest
- itemsQty
- itemsCount
- haveStockableItems
- paymentMethod
- paymentMethodTitle
- subTotalInclTax
- baseSubTotalInclTax
- formattedSubTotalInclTax
- taxTotal
- formattedTaxTotal
- shippingAmountInclTax
- baseShippingAmountInclTax
- formattedShippingAmountInclTax
- }
- }
- }
- GQL;
- $updateResponse = $this->graphQL($updateMutation, [
- 'cartItemId' => (int) $cartItemId,
- 'quantity' => 1,
- ], $headers);
- $updateResponse->assertSuccessful();
- $data = $updateResponse->json('data.createUpdateCartItem.updateCartItem');
- $this->assertNotNull($data);
- $this->assertGreaterThan(0, (int) ($data['itemsCount'] ?? 0));
- $this->assertSame($product->id, $data['items']['edges'][0]['node']['productId'] ?? null);
- $this->assertSame(1, $data['items']['edges'][0]['node']['quantity'] ?? null);
- }
- /**
- * Remove Cart Item (Guest)
- */
- public function test_remove_cart_item_as_guest(): void
- {
- $token = $this->getGuestCartToken();
- $headers = $this->guestHeaders($token);
- // Use test product helper to get a product with inventory
- $productData = $this->createTestProduct();
- $product = $productData['product'];
- // First add product to cart
- $addMutation = <<<'GQL'
- mutation createAddProductInCart($productId: Int!, $quantity: Int!) {
- createAddProductInCart(input: {productId: $productId, quantity: $quantity}) {
- addProductInCart {
- id
- itemsCount
- items {
- edges {
- node {
- id
- productId
- quantity
- }
- }
- }
- }
- }
- }
- GQL;
- $addResponse = $this->graphQL($addMutation, [
- 'productId' => $product->id,
- 'quantity' => 2,
- ], $headers);
- $addResponse->assertSuccessful();
- $cartItemId = $addResponse->json('data.createAddProductInCart.addProductInCart.items.edges.0.node.id');
- $this->assertNotNull($cartItemId);
- // Now remove the item
- $removeMutation = <<<'GQL'
- mutation createRemoveCartItem($cartItemId: Int!) {
- createRemoveCartItem(input: {cartItemId: $cartItemId}) {
- removeCartItem {
- id
- _id
- cartToken
- items {
- totalCount
- edges {
- node {
- id
- cartId
- productId
- name
- sku
- quantity
- price
- basePrice
- total
- baseTotal
- productUrlKey
- canChangeQty
- }
- }
- }
- }
- }
- }
- GQL;
- $response = $this->graphQL($removeMutation, [
- 'cartItemId' => (int) $cartItemId,
- ], $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createRemoveCartItem.removeCartItem');
- $this->assertNotNull($data);
- $this->assertLessThanOrEqual(0, (int) ($data['itemsCount'] ?? 0));
- }
- /**
- * Apply Coupon (Guest)
- */
- public function test_apply_coupon_as_guest(): void
- {
- $token = $this->getGuestCartToken();
- $headers = $this->guestHeaders($token);
- // Use test product helper to get a product with inventory
- $productData = $this->createTestProduct();
- $product = $productData['product'];
- // First add product to cart
- $addMutation = <<<'GQL'
- mutation createAddProductInCart($productId: Int!, $quantity: Int!) {
- createAddProductInCart(input: {productId: $productId, quantity: $quantity}) {
- addProductInCart {
- itemsCount
- }
- }
- }
- GQL;
- $this->graphQL($addMutation, [
- 'productId' => $product->id,
- 'quantity' => 1,
- ], $headers);
- // Apply coupon
- $couponMutation = <<<'GQL'
- mutation createApplyCoupon($couponCode: String!) {
- createApplyCoupon(input: {couponCode: $couponCode}) {
- applyCoupon {
- id
- discountAmount
- grandTotal
- }
- }
- }
- GQL;
- $response = $this->graphQL($couponMutation, [
- 'couponCode' => 'SAVE10',
- ], $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createApplyCoupon.applyCoupon');
- $this->assertNotNull($data);
- }
- /**
- * Remove Coupon (Guest)
- */
- public function test_remove_coupon_as_guest(): void
- {
- $token = $this->getGuestCartToken();
- $headers = $this->guestHeaders($token);
- // Use test product helper to get a product with inventory
- $productData = $this->createTestProduct();
- $product = $productData['product'];
- // First add product to cart
- $addMutation = <<<'GQL'
- mutation createAddProductInCart($productId: Int!, $quantity: Int!) {
- createAddProductInCart(input: {productId: $productId, quantity: $quantity}) {
- addProductInCart {
- itemsCount
- }
- }
- }
- GQL;
- $this->graphQL($addMutation, [
- 'productId' => $product->id,
- 'quantity' => 1,
- ], $headers);
- // Remove coupon
- $removeCouponMutation = <<<'GQL'
- mutation createRemoveCoupon {
- createRemoveCoupon(input: {}) {
- removeCoupon {
- id
- discountAmount
- grandTotal
- }
- }
- }
- GQL;
- $response = $this->graphQL($removeCouponMutation, [], $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createRemoveCoupon.removeCoupon');
- $this->assertNotNull($data);
- }
- }
|