|
| 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\Usps; |
| 9 | + |
| 10 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test for setting USPS shipping method on cart. |
| 17 | + * Current class covers following base USPS shipping methods: |
| 18 | + * |
| 19 | + * | Code | Label |
| 20 | + * -------------------------------------- |
| 21 | + * | 1 | Priority Mail |
| 22 | + * | 2 | Priority Mail Express Hold For Pickup |
| 23 | + * | 3 | Priority Mail Express |
| 24 | + * | 6 | Media Mail |
| 25 | + * | 7 | Library Mail |
| 26 | + * | 13 | Priority Mail Express Flat Rate Envelope |
| 27 | + * | 16 | Priority Mail Flat Rate Envelope |
| 28 | + * | 17 | Priority Mail Medium Flat Rate Box |
| 29 | + * | 22 | Priority Mail Large Flat Rate Box |
| 30 | + * | 27 | Priority Mail Express Flat Rate Envelope Hold For Pickup |
| 31 | + * | 28 | Priority Mail Small Flat Rate Box |
| 32 | + * | INT_1 | Priority Mail Express International |
| 33 | + * | INT_2 | Priority Mail International |
| 34 | + * | INT_8 | Priority Mail International Flat Rate Envelope |
| 35 | + * | INT_9 | Priority Mail International Medium Flat Rate Box |
| 36 | + * | INT_10 | Priority Mail Express International Flat Rate Envelope |
| 37 | + * | INT_11 | Priority Mail International Large Flat Rate Box |
| 38 | + * | INT_12 | USPS GXG Envelopes |
| 39 | + * | INT_15 | First-Class Package International Service |
| 40 | + * | INT_16 | Priority Mail International Small Flat Rate Box |
| 41 | + * | INT_20 | Priority Mail International Small Flat Rate Envelope |
| 42 | + */ |
| 43 | +class SetUspsShippingMethodsOnCartTest extends GraphQlAbstract |
| 44 | +{ |
| 45 | + /** |
| 46 | + * Defines carrier label for "USPS" shipping method |
| 47 | + */ |
| 48 | + const CARRIER_TITLE = 'United States Postal Service'; |
| 49 | + |
| 50 | + /** |
| 51 | + * Defines carrier code for "USPS" shipping method |
| 52 | + */ |
| 53 | + const CARRIER_CODE = 'usps'; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var CustomerTokenServiceInterface |
| 57 | + */ |
| 58 | + private $customerTokenService; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 62 | + */ |
| 63 | + private $getMaskedQuoteIdByReservedOrderId; |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritdoc |
| 67 | + */ |
| 68 | + protected function setUp() |
| 69 | + { |
| 70 | + $this->markTestSkipped( |
| 71 | + 'Need to implement mock instead of real carrier service call ' . |
| 72 | + 'https://github.com/magento/graphql-ce/issues/739' |
| 73 | + ); |
| 74 | + $objectManager = Bootstrap::getObjectManager(); |
| 75 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 76 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 81 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 82 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_weight_to_simple_product.php |
| 83 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 84 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 85 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 86 | + * @magentoApiDataFixture Magento/GraphQl/Usps/_files/enable_usps_shipping_method.php |
| 87 | + * |
| 88 | + * @dataProvider dataProviderShippingMethods |
| 89 | + * @param string $methodCode |
| 90 | + * @param string $methodLabel |
| 91 | + */ |
| 92 | + public function testSetUspsShippingMethod(string $methodCode, string $methodLabel) |
| 93 | + { |
| 94 | + $quoteReservedId = 'test_quote'; |
| 95 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($quoteReservedId); |
| 96 | + |
| 97 | + $query = $this->getQuery($maskedQuoteId, self::CARRIER_CODE, $methodCode); |
| 98 | + $response = $this->sendRequestWithToken($query); |
| 99 | + |
| 100 | + self::assertArrayHasKey('setShippingMethodsOnCart', $response); |
| 101 | + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); |
| 102 | + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); |
| 103 | + self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 104 | + |
| 105 | + $shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 106 | + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); |
| 107 | + |
| 108 | + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); |
| 109 | + self::assertEquals(self::CARRIER_CODE, $shippingAddress['selected_shipping_method']['carrier_code']); |
| 110 | + |
| 111 | + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); |
| 112 | + self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']); |
| 113 | + |
| 114 | + self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']); |
| 115 | + self::assertEquals(self::CARRIER_TITLE, $shippingAddress['selected_shipping_method']['carrier_title']); |
| 116 | + |
| 117 | + self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']); |
| 118 | + self::assertEquals( |
| 119 | + self::CARRIER_TITLE . ' - ' . $methodLabel, |
| 120 | + $shippingAddress['selected_shipping_method']['method_title'] |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + public function dataProviderShippingMethods(): array |
| 128 | + { |
| 129 | + return [ |
| 130 | + 'Library Mail Parcel' => |
| 131 | + ['7', 'Library Mail Parcel'], |
| 132 | + 'Media Mail Parcel' => ['6', 'Media Mail Parcel'], |
| 133 | + 'Priority Mail 3-Day Small Flat Rate Box' => |
| 134 | + ['28', 'Priority Mail 3-Day Small Flat Rate Box'], |
| 135 | + 'Priority Mail 3-Day Flat Rate Envelope' => |
| 136 | + ['16', 'Priority Mail 3-Day Flat Rate Envelope'], |
| 137 | + 'Priority Mail 3-Day' => ['1', 'Priority Mail 3-Day'], |
| 138 | + 'Priority Mail 3-Day Small Flat Rate Envelope' => |
| 139 | + ['42', 'Priority Mail 3-Day Small Flat Rate Envelope'], |
| 140 | + 'Priority Mail 3-Day Medium Flat Rate Box' => |
| 141 | + ['17', 'Priority Mail 3-Day Medium Flat Rate Box'], |
| 142 | + 'Priority Mail 3-Day Large Flat Rate Box' => |
| 143 | + ['22', 'Priority Mail 3-Day Large Flat Rate Box'], |
| 144 | + 'Priority Mail Express 2-Day Flat Rate Envelope' => |
| 145 | + ['13', 'Priority Mail Express 2-Day Flat Rate Envelope'], |
| 146 | + 'Priority Mail Express 2-Day Flat Rate Envelope Hold For Pickup' => |
| 147 | + ['27', 'Priority Mail Express 2-Day Flat Rate Envelope Hold For Pickup'], |
| 148 | + 'Priority Mail Express 2-Day' => |
| 149 | + ['3', 'Priority Mail Express 2-Day'], |
| 150 | + 'Priority Mail Express 2-Day Hold For Pickup' => |
| 151 | + ['2', 'Priority Mail Express 2-Day Hold For Pickup'], |
| 152 | + ]; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 157 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 158 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_weight_to_simple_product.php |
| 159 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 160 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 161 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_canada_address.php |
| 162 | + * @magentoApiDataFixture Magento/GraphQl/Usps/_files/enable_usps_shipping_method.php |
| 163 | + * |
| 164 | + * @dataProvider dataProviderShippingMethodsBasedOnCanadaAddress |
| 165 | + * @param string $methodCode |
| 166 | + * @param string $methodLabel |
| 167 | + */ |
| 168 | + public function testSetUspsShippingMethodBasedOnCanadaAddress(string $methodCode, string $methodLabel) |
| 169 | + { |
| 170 | + $quoteReservedId = 'test_quote'; |
| 171 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($quoteReservedId); |
| 172 | + |
| 173 | + $query = $this->getQuery($maskedQuoteId, self::CARRIER_CODE, $methodCode); |
| 174 | + $response = $this->sendRequestWithToken($query); |
| 175 | + |
| 176 | + self::assertArrayHasKey('setShippingMethodsOnCart', $response); |
| 177 | + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); |
| 178 | + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); |
| 179 | + self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 180 | + |
| 181 | + $shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 182 | + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); |
| 183 | + |
| 184 | + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); |
| 185 | + self::assertEquals(self::CARRIER_CODE, $shippingAddress['selected_shipping_method']['carrier_code']); |
| 186 | + |
| 187 | + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); |
| 188 | + self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']); |
| 189 | + |
| 190 | + self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']); |
| 191 | + self::assertEquals(self::CARRIER_TITLE, $shippingAddress['selected_shipping_method']['carrier_title']); |
| 192 | + |
| 193 | + self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']); |
| 194 | + self::assertEquals( |
| 195 | + self::CARRIER_TITLE . ' - ' . $methodLabel, |
| 196 | + $shippingAddress['selected_shipping_method']['method_title'] |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * @return array |
| 202 | + */ |
| 203 | + public function dataProviderShippingMethodsBasedOnCanadaAddress(): array |
| 204 | + { |
| 205 | + return [ |
| 206 | + 'First-Class Package International Service' => |
| 207 | + ['INT_15', 'First-Class Package International Service'], |
| 208 | + 'Priority Mail International Small Flat Rate Envelope' => |
| 209 | + ['INT_20', 'Priority Mail International Small Flat Rate Envelope'], |
| 210 | + 'Priority Mail International Flat Rate Envelope' => |
| 211 | + ['INT_8', 'Priority Mail International Flat Rate Envelope'], |
| 212 | + 'Priority Mail International Small Flat Rate Box' => |
| 213 | + ['INT_16', 'Priority Mail International Small Flat Rate Box'], |
| 214 | + 'Priority Mail International' => |
| 215 | + ['INT_2', 'Priority Mail International'], |
| 216 | + 'Priority Mail Express International Flat Rate Envelope' => |
| 217 | + ['INT_10', 'Priority Mail Express International Flat Rate Envelope'], |
| 218 | + 'Priority Mail Express International' => |
| 219 | + ['INT_1', 'Priority Mail Express International'], |
| 220 | + 'Priority Mail International Medium Flat Rate Box' => |
| 221 | + ['INT_9', 'Priority Mail International Medium Flat Rate Box'], |
| 222 | + 'Priority Mail International Large Flat Rate Box' => |
| 223 | + ['INT_11', 'Priority Mail International Large Flat Rate Box'], |
| 224 | + 'USPS GXG Envelopes' => |
| 225 | + ['INT_12', 'USPS GXG Envelopes'], |
| 226 | + ]; |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Generates query for setting the specified shipping method on cart |
| 231 | + * |
| 232 | + * @param string $maskedQuoteId |
| 233 | + * @param string $carrierCode |
| 234 | + * @param string $methodCode |
| 235 | + * @return string |
| 236 | + */ |
| 237 | + private function getQuery( |
| 238 | + string $maskedQuoteId, |
| 239 | + string $carrierCode, |
| 240 | + string $methodCode |
| 241 | + ): string { |
| 242 | + return <<<QUERY |
| 243 | +mutation { |
| 244 | + setShippingMethodsOnCart(input: { |
| 245 | + cart_id: "$maskedQuoteId" |
| 246 | + shipping_methods: [ |
| 247 | + { |
| 248 | + carrier_code: "$carrierCode" |
| 249 | + method_code: "$methodCode" |
| 250 | + } |
| 251 | + ] |
| 252 | + }) { |
| 253 | + cart { |
| 254 | + shipping_addresses { |
| 255 | + selected_shipping_method { |
| 256 | + carrier_code |
| 257 | + method_code |
| 258 | + carrier_title |
| 259 | + method_title |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | +} |
| 265 | +QUERY; |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Sends a GraphQL request with using a bearer token |
| 270 | + * |
| 271 | + * @param string $query |
| 272 | + * @return array |
| 273 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 274 | + */ |
| 275 | + private function sendRequestWithToken(string $query): array |
| 276 | + { |
| 277 | + $customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); |
| 278 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 279 | + |
| 280 | + return $this->graphQlMutation($query, [], '', $headerMap); |
| 281 | + } |
| 282 | +} |
0 commit comments