|
| 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\Quote\Customer; |
| 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 offline shipping methods on cart |
| 18 | + */ |
| 19 | +class SetOfflineShippingMethodsOnCartTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 23 | + */ |
| 24 | + private $getMaskedQuoteIdByReservedOrderId; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var GetQuoteShippingAddressIdByReservedQuoteId |
| 28 | + */ |
| 29 | + private $getQuoteShippingAddressIdByReservedQuoteId; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var CustomerTokenServiceInterface |
| 33 | + */ |
| 34 | + private $customerTokenService; |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritdoc |
| 38 | + */ |
| 39 | + protected function setUp() |
| 40 | + { |
| 41 | + $objectManager = Bootstrap::getObjectManager(); |
| 42 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 43 | + $this->getQuoteShippingAddressIdByReservedQuoteId = $objectManager->get( |
| 44 | + GetQuoteShippingAddressIdByReservedQuoteId::class |
| 45 | + ); |
| 46 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 51 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 52 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 53 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 54 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php |
| 55 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php |
| 56 | + * @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php |
| 57 | + * |
| 58 | + * @param string $carrierCode |
| 59 | + * @param string $methodCode |
| 60 | + * @param float $amount |
| 61 | + * @param string $label |
| 62 | + * @dataProvider offlineShippingMethodDataProvider |
| 63 | + */ |
| 64 | + public function testSetOfflineShippingMethod(string $carrierCode, string $methodCode, float $amount, string $label) |
| 65 | + { |
| 66 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 67 | + $quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote'); |
| 68 | + |
| 69 | + $query = $this->getQuery( |
| 70 | + $maskedQuoteId, |
| 71 | + $methodCode, |
| 72 | + $carrierCode, |
| 73 | + $quoteAddressId |
| 74 | + ); |
| 75 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 76 | + |
| 77 | + self::assertArrayHasKey('setShippingMethodsOnCart', $response); |
| 78 | + self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']); |
| 79 | + self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']); |
| 80 | + self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 81 | + |
| 82 | + $shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']); |
| 83 | + self::assertArrayHasKey('selected_shipping_method', $shippingAddress); |
| 84 | + |
| 85 | + self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']); |
| 86 | + self::assertEquals($carrierCode, $shippingAddress['selected_shipping_method']['carrier_code']); |
| 87 | + |
| 88 | + self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']); |
| 89 | + self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']); |
| 90 | + |
| 91 | + self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']); |
| 92 | + self::assertEquals($amount, $shippingAddress['selected_shipping_method']['amount']); |
| 93 | + |
| 94 | + self::assertArrayHasKey('label', $shippingAddress['selected_shipping_method']); |
| 95 | + self::assertEquals($label, $shippingAddress['selected_shipping_method']['label']); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function offlineShippingMethodDataProvider(): array |
| 102 | + { |
| 103 | + return [ |
| 104 | + 'flatrate_flatrate' => ['flatrate', 'flatrate', 10, 'Flat Rate - Fixed'], |
| 105 | + 'tablerate_bestway' => ['tablerate', 'bestway', 10, 'Best Way - Table Rate'], |
| 106 | + 'freeshipping_freeshipping' => ['freeshipping', 'freeshipping', 0, 'Free Shipping - Free'], |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param string $maskedQuoteId |
| 112 | + * @param string $shippingMethodCode |
| 113 | + * @param string $shippingCarrierCode |
| 114 | + * @param int $shippingAddressId |
| 115 | + * @return string |
| 116 | + */ |
| 117 | + private function getQuery( |
| 118 | + string $maskedQuoteId, |
| 119 | + string $shippingMethodCode, |
| 120 | + string $shippingCarrierCode, |
| 121 | + int $shippingAddressId |
| 122 | + ): string { |
| 123 | + return <<<QUERY |
| 124 | +mutation { |
| 125 | + setShippingMethodsOnCart(input: |
| 126 | + { |
| 127 | + cart_id: "$maskedQuoteId", |
| 128 | + shipping_methods: [{ |
| 129 | + cart_address_id: $shippingAddressId |
| 130 | + carrier_code: "$shippingCarrierCode" |
| 131 | + method_code: "$shippingMethodCode" |
| 132 | + }] |
| 133 | + }) { |
| 134 | + cart { |
| 135 | + shipping_addresses { |
| 136 | + selected_shipping_method { |
| 137 | + carrier_code |
| 138 | + method_code |
| 139 | + amount |
| 140 | + label |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | +QUERY; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param string $username |
| 151 | + * @param string $password |
| 152 | + * @return array |
| 153 | + */ |
| 154 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 155 | + { |
| 156 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 157 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 158 | + return $headerMap; |
| 159 | + } |
| 160 | +} |
0 commit comments