|
| 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\Guest; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 12 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 13 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 14 | +use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 20 | + |
| 21 | +/** |
| 22 | + * Get estimate for cart with GraphQl query and variables |
| 23 | + */ |
| 24 | +class SetShippingAddressForEstimateWithVariablesTest extends GraphQlAbstract |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ProductRepositoryInterface |
| 28 | + */ |
| 29 | + private $productRepository; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var DataFixtureStorage |
| 33 | + */ |
| 34 | + private $fixtures; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 38 | + */ |
| 39 | + private $quoteIdToMaskedQuoteIdInterface; |
| 40 | + |
| 41 | + /** |
| 42 | + * @inheritdoc |
| 43 | + */ |
| 44 | + protected function setUp(): void |
| 45 | + { |
| 46 | + $objectManager = Bootstrap::getObjectManager(); |
| 47 | + |
| 48 | + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 49 | + $this->quoteIdToMaskedQuoteIdInterface = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); |
| 50 | + $this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @throws NoSuchEntityException |
| 55 | + * @throws \Exception |
| 56 | + */ |
| 57 | + #[ |
| 58 | + DataFixture(ProductFixture::class, as: 'product'), |
| 59 | + DataFixture(GuestCartFixture::class, as: 'cart'), |
| 60 | + ] |
| 61 | + public function testAddProductsToEmptyCartWithVariables(): void |
| 62 | + { |
| 63 | + $product = $this->fixtures->get('product'); |
| 64 | + $cart = $this->fixtures->get('cart'); |
| 65 | + |
| 66 | + $maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId()); |
| 67 | + $query = $this->getAddToCartMutation(); |
| 68 | + $variables = $this->getAddToCartVariables($maskedQuoteId, 1, $product->getSku()); |
| 69 | + $response = $this->graphQlMutation($query, $variables); |
| 70 | + $result = $response['addProductsToCart']; |
| 71 | + |
| 72 | + self::assertEmpty($result['user_errors']); |
| 73 | + self::assertCount(1, $result['cart']['items']); |
| 74 | + |
| 75 | + $query = $this->getSetShippingAddressForEstimateMutation(); |
| 76 | + $variables = $this->getSetShippingAddressForEstimateVariables($maskedQuoteId); |
| 77 | + $response = $this->graphQlMutation($query, $variables); |
| 78 | + $result = $response['setShippingAddressesOnCart']; |
| 79 | + |
| 80 | + $cartItem = $result['cart']['items'][0]; |
| 81 | + self::assertEquals($product->getSku(), $cartItem['product']['sku']); |
| 82 | + self::assertEquals(1, $cartItem['quantity']); |
| 83 | + self::assertEquals("SetShippingAddressesOnCartOutput", $result['__typename']); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns GraphQl mutation for adding item to cart |
| 88 | + * |
| 89 | + * @return string |
| 90 | + */ |
| 91 | + private function getSetShippingAddressForEstimateMutation(): string |
| 92 | + { |
| 93 | + return <<<MUTATION |
| 94 | +mutation SetShippingAddressForEstimate(\$cartId: String!, \$address: CartAddressInput!) { |
| 95 | + setShippingAddressesOnCart( |
| 96 | + input: {cart_id: \$cartId, shipping_addresses: [{address: \$address}]} |
| 97 | + ) { |
| 98 | + cart { |
| 99 | + id |
| 100 | + __typename |
| 101 | + items { |
| 102 | + id |
| 103 | + product { |
| 104 | + name |
| 105 | + sku |
| 106 | + } |
| 107 | + quantity |
| 108 | + } |
| 109 | + } |
| 110 | + __typename |
| 111 | + } |
| 112 | +} |
| 113 | +MUTATION; |
| 114 | + } |
| 115 | + |
| 116 | + private function getSetShippingAddressForEstimateVariables( |
| 117 | + string $maskedQuoteId |
| 118 | + ): array { |
| 119 | + return |
| 120 | + [ |
| 121 | + 'cartId' => $maskedQuoteId, |
| 122 | + 'address' => |
| 123 | + [ |
| 124 | + 'city' => 'New York', |
| 125 | + 'firstname' => 'Test', |
| 126 | + 'lastname' => 'Test', |
| 127 | + 'street' => ['line 1', 'line 2'], |
| 128 | + 'telephone' => '1234567890', |
| 129 | + 'postcode' => '11371', |
| 130 | + 'region' => 'NY', |
| 131 | + 'country_code' => 'US' |
| 132 | + ] |
| 133 | + ]; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns GraphQl mutation for adding item to cart |
| 138 | + * |
| 139 | + * @return string |
| 140 | + */ |
| 141 | + private function getAddToCartMutation(): string |
| 142 | + { |
| 143 | + return <<<MUTATION |
| 144 | +mutation (\$cartId: String!, \$products: [CartItemInput!]!) { |
| 145 | + addProductsToCart(cartId: \$cartId, cartItems: \$products) { |
| 146 | + cart { |
| 147 | + id |
| 148 | + items { |
| 149 | + uid |
| 150 | + quantity |
| 151 | + product { |
| 152 | + sku |
| 153 | + name |
| 154 | + thumbnail { |
| 155 | + url |
| 156 | + __typename |
| 157 | + } |
| 158 | + __typename |
| 159 | + } |
| 160 | + prices { |
| 161 | + price { |
| 162 | + value |
| 163 | + currency |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + user_errors { |
| 169 | + code |
| 170 | + message |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | +MUTATION; |
| 175 | + } |
| 176 | + |
| 177 | + private function getAddToCartVariables( |
| 178 | + string $maskedQuoteId, |
| 179 | + int $qty, |
| 180 | + string $sku |
| 181 | + ): array { |
| 182 | + return |
| 183 | + [ |
| 184 | + 'cartId' => $maskedQuoteId, |
| 185 | + 'products' => [ |
| 186 | + [ |
| 187 | + 'sku' => $sku, |
| 188 | + 'parent_sku' => $sku, |
| 189 | + 'quantity' => $qty |
| 190 | + ] |
| 191 | + ] |
| 192 | + ]; |
| 193 | + } |
| 194 | +} |
0 commit comments