| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- <?php
- namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
- use Webkul\BagistoApi\Tests\GraphQLTestCase;
- class CustomerCartTest extends GraphQLTestCase
- {
- private function loginCustomerAndGetToken(): string
- {
- // Use our test customer helper to create a customer with proper token
- $customerData = $this->createTestCustomer();
- return $customerData['token'];
- }
- private function customerHeaders(string $token): array
- {
- return [
- 'Authorization' => 'Bearer '.$token,
- ];
- }
- private function createCustomerCartWithItem(string $token, int $quantity = 2): array
- {
- // Use our 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
- itemsCount
- items {
- edges {
- node {
- id
- productId
- quantity
- }
- }
- }
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation, [
- 'productId' => $product->id,
- 'quantity' => $quantity,
- ], $this->customerHeaders($token));
- $response->assertSuccessful();
- $cart = $response->json('data.createAddProductInCart.addProductInCart');
- $cartItemId = $response->json('data.createAddProductInCart.addProductInCart.items.edges.0.node.id');
- $this->assertNotNull($cart);
- $this->assertNotNull($cartItemId);
- return [
- 'product' => $product->id,
- 'cartId' => (int) ($cart['_id'] ?? $cart['id'] ?? 0),
- 'cartItemId' => (int) $cartItemId,
- ];
- }
- /**
- * Create Simple Cart (Customer)
- */
- public function test_create_simple_cart_as_customer(): void
- {
- $token = $this->loginCustomerAndGetToken();
- $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, [], $this->customerHeaders($token));
- $response->assertSuccessful();
- $data = $response->json('data.createCartToken.cartToken');
- $this->assertNotNull($data);
- $this->assertTrue((bool) ($data['success'] ?? false));
- $this->assertFalse((bool) ($data['isGuest'] ?? true));
- $this->assertNotNull($data['customerId'] ?? null);
- }
- /**
- * Add Product In Cart (Customer) -- Failed
- */
- public function test_create_add_product_in_cart_as_customer(): void
- {
- // Use our helper method to create a fake customer
- $customerData = $this->createTestCustomer();
- $customer = $customerData['customer'];
- $token = $customerData['token'];
- // Use our helper method to create a complete test product
- $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
- 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;
- $response = $this->graphQL($mutation, [
- 'productId' => $product->id,
- 'quantity' => 1,
- ], $this->customerHeaders($token));
-
- $response->assertSuccessful();
- $data = $response->json('data.createAddProductInCart.addProductInCart');
- $this->assertNotNull($data);
- $this->assertNotNull($data['customerId'] ?? null);
- $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 (Customer) -- Failed
- */
- public function test_update_cart_item_quantity_as_customer(): void
- {
- $token = $this->loginCustomerAndGetToken();
- $headers = $this->customerHeaders($token);
-
- // Use our 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
- customerId
- itemsCount
- items {
- edges {
- node {
- id
- productId
- quantity
- }
- }
- }
- }
- }
- }
- GQL;
- $updateResponse = $this->graphQL($updateMutation, [
- 'cartItemId' => (int) $cartItemId,
- 'quantity' => 1,
- ], $headers);
- $updateResponse->assertSuccessful();
- $data = $updateResponse->json('data.createUpdateCartItem.updateCartItem');
- $this->assertNotNull($data);
- $this->assertNotNull($data['customerId'] ?? null);
- $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);
- }
- /**
- * Get Cart Details (Customer)
- */
- public function test_read_cart_as_customer(): void
- {
- $token = $this->loginCustomerAndGetToken();
- $this->createCustomerCartWithItem($token, 3);
- $mutation = <<<'GQL'
- mutation createReadCart {
- createReadCart(input: {}) {
- readCart {
- id
- _id
- customerId
- itemsCount
- itemsQty
- items {
- totalCount
- edges {
- node {
- id
- productId
- quantity
- }
- }
- }
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation, [], $this->customerHeaders($token));
- $response->assertSuccessful();
- $data = $response->json('data.createReadCart.readCart');
- $this->assertNotNull($data);
- $this->assertNotNull($data['customerId'] ?? null);
- $this->assertGreaterThan(0, (int) ($data['itemsCount'] ?? 0));
- $this->assertGreaterThan(0, (int) ($data['items']['totalCount'] ?? 0));
- }
- /**
- * Remove Cart Item (Customer) -- Failed
- */
- public function test_remove_cart_item_as_customer(): void
- {
- $token = $this->loginCustomerAndGetToken();
- $cart = $this->createCustomerCartWithItem($token, 2);
- $mutation = <<<'GQL'
- mutation createRemoveCartItem($cartItemId: Int!) {
- createRemoveCartItem(input: {cartItemId: $cartItemId}) {
- removeCartItem {
- id
- itemsCount
- subtotal
- grandTotal
- discountAmount
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation, [
- 'cartItemId' => $cart['cartItemId'],
- ], $this->customerHeaders($token));
- $response->assertSuccessful();
- $data = $response->json('data.createRemoveCartItem.removeCartItem');
- $this->assertNotNull($data);
- $this->assertLessThanOrEqual(0, (int) ($data['itemsCount'] ?? 0));
- }
- /**
- * Apply Coupon (Customer)
- */
- public function test_apply_coupon_as_customer(): void
- {
- $token = $this->loginCustomerAndGetToken();
- $this->createCustomerCartWithItem($token, 1);
- $mutation = <<<'GQL'
- mutation createApplyCoupon($couponCode: String!) {
- createApplyCoupon(input: {couponCode: $couponCode}) {
- applyCoupon {
- id
- couponCode
- discountAmount
- formattedDiscountAmount
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation, [
- 'couponCode' => 'DISCOUNT10',
- ], $this->customerHeaders($token));
- $response->assertSuccessful();
- $json = $response->json();
- if (isset($json['errors'])) {
- $this->assertNotEmpty($json['errors']);
- return;
- }
- $data = $response->json('data.createApplyCoupon.applyCoupon');
- $this->assertNotNull($data);
- $this->assertArrayHasKey('couponCode', $data);
- }
- /**
- * Remove Coupon (Customer)
- */
- public function test_remove_coupon_as_customer(): void
- {
- $token = $this->loginCustomerAndGetToken();
- $this->createCustomerCartWithItem($token, 1);
- $mutation = <<<'GQL'
- mutation createRemoveCoupon {
- createRemoveCoupon(input: {}) {
- removeCoupon {
- id
- couponCode
- discountAmount
- formattedDiscountAmount
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation, [], $this->customerHeaders($token));
- $response->assertSuccessful();
- $json = $response->json();
- if (isset($json['errors'])) {
- $this->assertNotEmpty($json['errors']);
- return;
- }
- $data = $response->json('data.createRemoveCoupon.removeCoupon');
- $this->assertNotNull($data);
- $this->assertArrayHasKey('couponCode', $data);
- }
- }
|