|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\FedEx; |
| 9 | + |
| 10 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 11 | +use Magento\GraphQl\Quote\GetQuoteShippingAddressIdByReservedQuoteId; |
| 12 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test for setting FedEx shipping method on cart. |
| 18 | + * Current class covers following base FedEx shipping methods: |
| 19 | + * |
| 20 | + * | Code | Label |
| 21 | + * -------------------------------------- |
| 22 | + * | FEDEX_GROUND | Ground |
| 23 | + * | SMART_POST | Smart Post |
| 24 | + * | FEDEX_EXPRESS_SAVER | Express Saver |
| 25 | + * | PRIORITY_OVERNIGHT | Priority Overnight |
| 26 | + * | FEDEX_2_DAY | 2 Day |
| 27 | + * | FIRST_OVERNIGHT | First Overnight |
| 28 | + * | INTERNATIONAL_ECONOMY |International Economy |
| 29 | + * | INTERNATIONAL_PRIORITY | International Priority |
| 30 | + */ |
| 31 | +class SetFedExShippingMethodsOnCartTest extends GraphQlAbstract |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Defines carrier label for "FedEx" shipping method |
| 35 | + */ |
| 36 | + const CARRIER_LABEL = 'Federal Express'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Defines carrier code for "FedEx" shipping method |
| 40 | + */ |
| 41 | + const CARRIER_CODE = 'fedex'; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var CustomerTokenServiceInterface |
| 45 | + */ |
| 46 | + private $customerTokenService; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 50 | + */ |
| 51 | + private $getMaskedQuoteIdByReservedOrderId; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var GetQuoteShippingAddressIdByReservedQuoteId |
| 55 | + */ |
| 56 | + private $getQuoteShippingAddressIdByReservedQuoteId; |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritdoc |
| 60 | + */ |
| 61 | + protected function setUp() |
| 62 | + { |
| 63 | + $this->markTestSkipped( |
| 64 | + 'Need to implement mock instead of real carrier service call ' . |
| 65 | + 'https://github.com/magento/graphql-ce/issues/740' |
| 66 | + ); |
| 67 | + $objectManager = Bootstrap::getObjectManager(); |
| 68 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 69 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 70 | + $this->getQuoteShippingAddressIdByReservedQuoteId = $objectManager->get( |
| 71 | + GetQuoteShippingAddressIdByReservedQuoteId::class |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 77 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 78 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_weight_to_simple_product.php |
| 79 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 80 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 81 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 82 | + * @magentoApiDataFixture Magento/GraphQl/FedEx/_files/enable_fedex_shipping_method.php |
| 83 | + * |
| 84 | + * @dataProvider dataProviderShippingMethods |
| 85 | + * @param string $methodCode |
| 86 | + * @param string $methodLabel |
| 87 | + */ |
| 88 | + public function testSetFedExShippingMethod(string $methodCode, string $methodLabel) |
| 89 | + { |
| 90 | + $quoteReservedId = 'test_quote'; |
| 91 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($quoteReservedId); |
| 92 | + $shippingAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute($quoteReservedId); |
| 93 | + |
| 94 | + $query = $this->getQuery($maskedQuoteId, $shippingAddressId, self::CARRIER_CODE, $methodCode); |
| 95 | + $response = $this->sendRequestWithToken($query); |
| 96 | + |
| 97 | + self::assertArrayHasKey('setShippingMethodsOnCart', $response); |
| 98 | + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); |
| 99 | + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); |
| 100 | + self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 101 | + |
| 102 | + $shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 103 | + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); |
| 104 | + |
| 105 | + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); |
| 106 | + self::assertEquals(self::CARRIER_CODE, $shippingAddress['selected_shipping_method']['carrier_code']); |
| 107 | + |
| 108 | + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); |
| 109 | + self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']); |
| 110 | + |
| 111 | + self::assertArrayHasKey('label', $shippingAddress['selected_shipping_method']); |
| 112 | + self::assertEquals( |
| 113 | + self::CARRIER_LABEL . ' - ' . $methodLabel, |
| 114 | + $shippingAddress['selected_shipping_method']['label'] |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @return array |
| 120 | + */ |
| 121 | + public function dataProviderShippingMethods(): array |
| 122 | + { |
| 123 | + return [ |
| 124 | + 'Ground' => ['FEDEX_GROUND', 'Ground'], |
| 125 | + 'Smart Post' => ['SMART_POST', 'Smart Post'], |
| 126 | + 'Express Saver' => ['FEDEX_EXPRESS_SAVER', 'Express Saver'], |
| 127 | + 'Priority Overnight' => ['PRIORITY_OVERNIGHT', 'Priority Overnight'], |
| 128 | + '2 Day' => ['FEDEX_2_DAY', '2 Day'], |
| 129 | + 'First Overnight' => ['FIRST_OVERNIGHT', 'First Overnight'], |
| 130 | + ]; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 135 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 136 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_weight_to_simple_product.php |
| 137 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 138 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 139 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_canada_address.php |
| 140 | + * @magentoApiDataFixture Magento/GraphQl/FedEx/_files/enable_fedex_shipping_method.php |
| 141 | + * |
| 142 | + * @dataProvider dataProviderShippingMethodsBasedOnCanadaAddress |
| 143 | + * @param string $methodCode |
| 144 | + * @param string $methodLabel |
| 145 | + */ |
| 146 | + public function testSetFedExShippingMethodBasedOnCanadaAddress(string $methodCode, string $methodLabel) |
| 147 | + { |
| 148 | + $quoteReservedId = 'test_quote'; |
| 149 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($quoteReservedId); |
| 150 | + $shippingAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute($quoteReservedId); |
| 151 | + |
| 152 | + $query = $this->getQuery($maskedQuoteId, $shippingAddressId, self::CARRIER_CODE, $methodCode); |
| 153 | + $response = $this->sendRequestWithToken($query); |
| 154 | + |
| 155 | + self::assertArrayHasKey('setShippingMethodsOnCart', $response); |
| 156 | + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); |
| 157 | + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); |
| 158 | + self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 159 | + |
| 160 | + $shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 161 | + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); |
| 162 | + |
| 163 | + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); |
| 164 | + self::assertEquals(self::CARRIER_CODE, $shippingAddress['selected_shipping_method']['carrier_code']); |
| 165 | + |
| 166 | + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); |
| 167 | + self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']); |
| 168 | + |
| 169 | + self::assertArrayHasKey('label', $shippingAddress['selected_shipping_method']); |
| 170 | + self::assertEquals( |
| 171 | + self::CARRIER_LABEL . ' - ' . $methodLabel, |
| 172 | + $shippingAddress['selected_shipping_method']['label'] |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return array |
| 178 | + */ |
| 179 | + public function dataProviderShippingMethodsBasedOnCanadaAddress(): array |
| 180 | + { |
| 181 | + return [ |
| 182 | + 'Ground' => ['FEDEX_GROUND', 'Ground'], |
| 183 | + 'International Economy' => ['INTERNATIONAL_ECONOMY', 'International Economy'], |
| 184 | + 'International Priority' => ['INTERNATIONAL_PRIORITY', 'International Priority'], |
| 185 | + ]; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Generates query for setting the specified shipping method on cart |
| 190 | + * |
| 191 | + * @param int $shippingAddressId |
| 192 | + * @param string $maskedQuoteId |
| 193 | + * @param string $carrierCode |
| 194 | + * @param string $methodCode |
| 195 | + * @return string |
| 196 | + */ |
| 197 | + private function getQuery( |
| 198 | + string $maskedQuoteId, |
| 199 | + int $shippingAddressId, |
| 200 | + string $carrierCode, |
| 201 | + string $methodCode |
| 202 | + ): string { |
| 203 | + return <<<QUERY |
| 204 | +mutation { |
| 205 | + setShippingMethodsOnCart(input: { |
| 206 | + cart_id: "$maskedQuoteId" |
| 207 | + shipping_methods: [ |
| 208 | + { |
| 209 | + cart_address_id: $shippingAddressId |
| 210 | + carrier_code: "$carrierCode" |
| 211 | + method_code: "$methodCode" |
| 212 | + } |
| 213 | + ] |
| 214 | + }) { |
| 215 | + cart { |
| 216 | + shipping_addresses { |
| 217 | + selected_shipping_method { |
| 218 | + carrier_code |
| 219 | + method_code |
| 220 | + label |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | +} |
| 226 | +QUERY; |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Sends a GraphQL request with using a bearer token |
| 231 | + * |
| 232 | + * @param string $query |
| 233 | + * @return array |
| 234 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 235 | + */ |
| 236 | + private function sendRequestWithToken(string $query): array |
| 237 | + { |
| 238 | + $customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); |
| 239 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 240 | + |
| 241 | + return $this->graphQlMutation($query, [], '', $headerMap); |
| 242 | + } |
| 243 | +} |
0 commit comments