Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Prevent undefined index warning before setting shipping method #666

Merged
merged 1 commit into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$carrierTitle = null;
$methodTitle = null;

if (count($rates) > 0) {
if (count($rates) > 0 && !empty($address->getShippingMethod())) {
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);

/** @var Rate $rate */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ public function testGetSelectedShippingMethod()
self::assertEquals('USD', $baseAmount['currency']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetSelectedShippingMethodBeforeSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);

$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);

self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']);

self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['method_code']);

self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['carrier_title']);

self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['method_title']);
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
Expand Down Expand Up @@ -193,6 +227,10 @@ private function getQuery(string $maskedQuoteId): string
{
cart(cart_id: "$maskedQuoteId") {
shipping_addresses {
available_shipping_methods {
carrier_code
method_code
}
selected_shipping_method {
carrier_code
method_code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,39 @@ public function testGetSelectedShippingMethod()
self::assertEquals('USD', $baseAmount['currency']);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetSelectedShippingMethodBeforeSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);

$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);

self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']);

self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['method_code']);

self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['carrier_title']);

self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
self::assertNull($shippingAddress['selected_shipping_method']['method_title']);
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
Expand Down Expand Up @@ -149,6 +182,10 @@ private function getQuery(string $maskedQuoteId): string
{
cart(cart_id: "$maskedQuoteId") {
shipping_addresses {
available_shipping_methods {
carrier_code
method_code
}
selected_shipping_method {
carrier_code
method_code
Expand Down