| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
- use Webkul\BagistoApi\Tests\GraphQLTestCase;
- class CustomerCheckoutTest extends GraphQLTestCase
- {
- /**
- * Helper method to get authorization headers with customer token
- */
- private function customerHeaders(string $token): array
- {
- return [
- 'Authorization' => 'Bearer ' . $token,
- ];
- }
- /**
- * Add product to cart first for checkout tests
- */
- private function addProductToCart(string $token): int
- {
- // 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
- itemsCount
- }
- }
- }
- GQL;
- $response = $this->graphQL($mutation, [
- 'productId' => $product->id,
- 'quantity' => 1,
- ], $this->customerHeaders($token));
- $response->assertSuccessful();
- return $product->id;
- }
- /**
- * Get Checkout Addresses (Customer)
- */
- public function test_get_checkout_addresses(): void
- {
- // First add product to cart to create a cart
- $customerData = $this->createTestCustomer();
- $token = $customerData['token'];
-
- // Add product to cart first to create a cart
- $this->addProductToCart($token);
-
- $query = <<<'GQL'
- query collectionGetCheckoutAddresses {
- collectionGetCheckoutAddresses {
- edges {
- node {
- id
- _id
- addressType
- parentAddressId
- firstName
- lastName
- gender
- companyName
- address
- city
- state
- country
- postcode
- email
- phone
- vatId
- defaultAddress
- useForShipping
- additional
- createdAt
- updatedAt
- name
- }
- }
- }
- }
- GQL;
- $response = $this->graphQL($query, [], $this->customerHeaders($token));
- $response->assertSuccessful();
- $data = $response->json('data.collectionGetCheckoutAddresses');
- $this->assertNotNull($data, 'checkout addresses response is null');
- $this->assertIsArray($data['edges'] ?? [], 'edges is not an array');
- }
- /**
- * Get Shipping Methods (Customer)
- */
- public function test_get_shipping_methods(): void
- {
- // Use createTestCustomer to get customer token
- $customerData = $this->createTestCustomer();
- $token = $customerData['token'];
-
- // Add product to cart first
- $this->addProductToCart($token);
-
- $query = <<<'GQL'
- query checkoutShippingRates {
- collectionShippingRates {
- _id
- id
- code
- description
- method
- price
- label
- }
- }
- GQL;
- $response = $this->graphQL($query, [], $this->customerHeaders($token));
- $response->assertSuccessful();
- $data = $response->json('data.collectionShippingRates');
- $this->assertNotNull($data, 'shipping rates response is null');
- $this->assertIsArray($data, 'shipping rates is not an array');
- }
- /**
- * Get Payment Methods (Customer)
- */
- public function test_get_payment_methods(): void
- {
- // Use createTestCustomer to get customer token
- $customerData = $this->createTestCustomer();
- $token = $customerData['token'];
-
- // Add product to cart first
- $this->addProductToCart($token);
-
- $query = <<<'GQL'
- query checkoutPaymentMethods {
- collectionPaymentMethods {
- id
- _id
- method
- title
- description
- icon
- isAllowed
- }
- }
- GQL;
- $response = $this->graphQL($query, [], $this->customerHeaders($token));
- $response->assertSuccessful();
- $data = $response->json('data.collectionPaymentMethods');
- $this->assertNotNull($data, 'payment methods response is null');
- $this->assertIsArray($data, 'payment methods is not an array');
- }
- /**
- * Set Billing/Shipping Address (Customer)
- */
- public function test_set_checkout_address(): void
- {
- // Use createTestCustomer to get customer token
- $customerData = $this->createTestCustomer();
- $token = $customerData['token'];
-
- // First add product to cart
- $this->addProductToCart($token);
-
- $headers = $this->customerHeaders($token);
- $query = <<<'GQL'
- mutation createCheckoutAddress(
- $billingFirstName: String!
- $billingLastName: String!
- $billingEmail: String!
- $billingAddress: String!
- $billingCity: String!
- $billingCountry: String!
- $billingState: String!
- $billingPostcode: String!
- $billingPhoneNumber: String!
- $useForShipping: Boolean
- ) {
- createCheckoutAddress(
- input: {
- billingFirstName: $billingFirstName
- billingLastName: $billingLastName
- billingEmail: $billingEmail
- billingAddress: $billingAddress
- billingCity: $billingCity
- billingCountry: $billingCountry
- billingState: $billingState
- billingPostcode: $billingPostcode
- billingPhoneNumber: $billingPhoneNumber
- useForShipping: $useForShipping
- }
- ) {
- checkoutAddress {
- _id
- success
- message
- id
- cartToken
- billingFirstName
- billingLastName
- billingAddress
- billingCity
- billingState
- billingPostcode
- billingPhoneNumber
- shippingFirstName
- shippingLastName
- shippingCity
- }
- }
- }
- GQL;
- $variables = [
- 'billingFirstName' => 'John',
- 'billingLastName' => 'Doe',
- 'billingEmail' => 'john@example.com',
- 'billingAddress' => '123 Main St',
- 'billingCity' => 'Los Angeles',
- 'billingCountry' => 'IN',
- 'billingState' => 'UP',
- 'billingPostcode' => '201301',
- 'billingPhoneNumber' => '2125551234',
- 'useForShipping' => true,
- ];
- $response = $this->graphQL($query, $variables, $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createCheckoutAddress.checkoutAddress');
- $this->assertNotNull($data, 'checkout address response is null');
- $this->assertArrayHasKey('_id', $data);
- $this->assertArrayHasKey('success', $data);
- }
- /**
- * Set Shipping Method (Customer)
- */
- public function test_set_shipping_method(): void
- {
- // Use createTestCustomer to get customer token
- $customerData = $this->createTestCustomer();
- $token = $customerData['token'];
-
- // First add product to cart and set address
- $this->addProductToCart($token);
- $this->setCheckoutAddress($token);
-
- $headers = $this->customerHeaders($token);
- $query = <<<'GQL'
- mutation createCheckoutShippingMethod(
- $shippingMethod: String!
- ) {
- createCheckoutShippingMethod(
- input: {
- shippingMethod: $shippingMethod
- }
- ) {
- checkoutShippingMethod {
- success
- id
- message
- }
- }
- }
- GQL;
- $variables = [
- 'shippingMethod' => 'flatrate_flatrate',
- ];
- $response = $this->graphQL($query, $variables, $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createCheckoutShippingMethod.checkoutShippingMethod');
- $this->assertNotNull($data, 'checkout shipping method response is null');
- $this->assertArrayHasKey('success', $data);
- }
- /**
- * Set Payment Method (Customer)
- */
- public function test_set_payment_method(): void
- {
- // Use createTestCustomer to get customer token
- $customerData = $this->createTestCustomer();
- $token = $customerData['token'];
-
- // First add product to cart, set address, and set shipping
- $this->addProductToCart($token);
- $this->setCheckoutAddress($token);
- $this->setShippingMethod($token);
-
- $headers = $this->customerHeaders($token);
- $query = <<<'GQL'
- mutation createCheckoutPaymentMethod(
- $paymentMethod: String!,
- $successUrl: String,
- $failureUrl: String,
- $cancelUrl: String
- ) {
- createCheckoutPaymentMethod(
- input: {
- paymentMethod: $paymentMethod,
- paymentSuccessUrl: $successUrl,
- paymentFailureUrl: $failureUrl,
- paymentCancelUrl: $cancelUrl
- }
- ) {
- checkoutPaymentMethod {
- success
- message
- paymentGatewayUrl
- paymentData
- }
- }
- }
- GQL;
- $variables = [
- 'paymentMethod' => 'moneytransfer',
- 'successUrl' => 'https://myapp.com/payment/success',
- 'failureUrl' => 'https://myapp.com/payment/failure',
- 'cancelUrl' => 'https://myapp.com/payment/cancel',
- ];
- $response = $this->graphQL($query, $variables, $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createCheckoutPaymentMethod.checkoutPaymentMethod');
- $this->assertNotNull($data, 'checkout payment method response is null');
- $this->assertArrayHasKey('success', $data);
- }
- /**
- * Place Order (Customer)
- */
- public function test_place_order(): void
- {
- // Use createTestCustomer to get customer token
- $customerData = $this->createTestCustomer();
- $token = $customerData['token'];
-
- // First add product to cart, set address, shipping and payment
- $this->addProductToCart($token);
- $this->setCheckoutAddress($token);
- $this->setShippingMethod($token);
- $this->setPaymentMethod($token);
-
- $headers = $this->customerHeaders($token);
- $query = <<<'GQL'
- mutation createCheckoutOrder {
- createCheckoutOrder(input:{}) {
- checkoutOrder {
- id
- orderId
- }
- }
- }
- GQL;
- $response = $this->graphQL($query, [], $headers);
- $response->assertSuccessful();
- $data = $response->json('data.createCheckoutOrder.checkoutOrder');
- $this->assertNotNull($data, 'checkout order response is null');
- $this->assertArrayHasKey('id', $data);
- $this->assertArrayHasKey('orderId', $data);
- }
- /**
- * Helper to set checkout address
- */
- private function setCheckoutAddress(string $token): void
- {
- $headers = $this->customerHeaders($token);
- $query = <<<'GQL'
- mutation createCheckoutAddress(
- $billingFirstName: String!
- $billingLastName: String!
- $billingEmail: String!
- $billingAddress: String!
- $billingCity: String!
- $billingCountry: String!
- $billingState: String!
- $billingPostcode: String!
- $billingPhoneNumber: String!
- $useForShipping: Boolean
- ) {
- createCheckoutAddress(
- input: {
- billingFirstName: $billingFirstName
- billingLastName: $billingLastName
- billingEmail: $billingEmail
- billingAddress: $billingAddress
- billingCity: $billingCity
- billingCountry: $billingCountry
- billingState: $billingState
- billingPostcode: $billingPostcode
- billingPhoneNumber: $billingPhoneNumber
- useForShipping: $useForShipping
- }
- ) {
- checkoutAddress {
- _id
- success
- }
- }
- }
- GQL;
- $variables = [
- 'billingFirstName' => 'John',
- 'billingLastName' => 'Doe',
- 'billingEmail' => 'john@example.com',
- 'billingAddress' => '123 Main St',
- 'billingCity' => 'Los Angeles',
- 'billingCountry' => 'IN',
- 'billingState' => 'UP',
- 'billingPostcode' => '201301',
- 'billingPhoneNumber' => '2125551234',
- 'useForShipping' => true,
- ];
- $response = $this->graphQL($query, $variables, $headers);
- $response->assertSuccessful();
- }
- /**
- * Helper to set shipping method
- */
- private function setShippingMethod(string $token): void
- {
- $headers = $this->customerHeaders($token);
- $query = <<<'GQL'
- mutation createCheckoutShippingMethod(
- $shippingMethod: String!
- ) {
- createCheckoutShippingMethod(
- input: {
- shippingMethod: $shippingMethod
- }
- ) {
- checkoutShippingMethod {
- success
- id
- }
- }
- }
- GQL;
- $variables = [
- 'shippingMethod' => 'flatrate_flatrate',
- ];
- $response = $this->graphQL($query, $variables, $headers);
- $response->assertSuccessful();
- }
- /**
- * Helper to set payment method
- */
- private function setPaymentMethod(string $token): void
- {
- $headers = $this->customerHeaders($token);
- $query = <<<'GQL'
- mutation createCheckoutPaymentMethod(
- $paymentMethod: String!,
- $successUrl: String,
- $failureUrl: String,
- $cancelUrl: String
- ) {
- createCheckoutPaymentMethod(
- input: {
- paymentMethod: $paymentMethod,
- paymentSuccessUrl: $successUrl,
- paymentFailureUrl: $failureUrl,
- paymentCancelUrl: $cancelUrl
- }
- ) {
- checkoutPaymentMethod {
- success
- }
- }
- }
- GQL;
- $variables = [
- 'paymentMethod' => 'moneytransfer',
- 'successUrl' => 'https://myapp.com/payment/success',
- 'failureUrl' => 'https://myapp.com/payment/failure',
- 'cancelUrl' => 'https://myapp.com/payment/cancel',
- ];
- $response = $this->graphQL($query, $variables, $headers);
- $response->assertSuccessful();
- }
- }
|