Skip to content

Commit

Permalink
GraphQL-428: Test coverage: GetAvailableShippingMethodsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Mar 29, 2019
1 parent d4c2b89 commit b64170a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

namespace Magento\GraphQl\Quote\Customer;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

Expand All @@ -25,40 +23,32 @@ class GetAvailableShippingMethodsTest extends GraphQlAbstract
private $customerTokenService;

/**
* @var QuoteIdToMaskedQuoteIdInterface
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $quoteIdToMaskedId;

/**
* @var QuoteFactory
*/
private $quoteFactory;

/**
* @var QuoteResource
*/
private $quoteResource;
private $getMaskedQuoteIdByReservedOrderId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
}

/**
* Test case: get available shipping methods from current customer quote
*
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 testGetAvailableShippingMethods()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());

self::assertArrayHasKey('cart', $response);
Expand All @@ -84,40 +74,62 @@ public function testGetAvailableShippingMethods()
);
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 testGetAvailableShippingMethodsFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* Test case: get available shipping methods from quote of another customer
*
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 testGetAvailableShippingMethodsFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);

$this->graphQlQuery(
$this->getQuery($maskedQuoteId),
[],
'',
$this->getHeaderMap('customer2@search.example.com')
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}

/**
* Test case: get available shipping methods when all shipping methods are disabled
*
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php
*/
public function testGetAvailableShippingMethodsIfAllShippingsAreDisabled()
public function testGetAvailableShippingMethodsIfShippingMethodsAreNotPresent()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());

self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods']));
self::assertEmpty($response['cart']['shipping_addresses'][0]['available_shipping_methods']);
}

/**
Expand Down Expand Up @@ -173,16 +185,4 @@ private function getHeaderMap(string $username = 'customer@example.com', string
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}

/**
* @param string $reservedOrderId
* @return string
*/
private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id');

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

namespace Magento\GraphQl\Quote\Guest;

use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

Expand All @@ -19,39 +17,30 @@
class GetAvailableShippingMethodsTest extends GraphQlAbstract
{
/**
* @var QuoteIdToMaskedQuoteIdInterface
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $quoteIdToMaskedId;

/**
* @var QuoteFactory
*/
private $quoteFactory;

/**
* @var QuoteResource
*/
private $quoteResource;
private $getMaskedQuoteIdByReservedOrderId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
* Test case: get available shipping methods from current customer quote
*
* @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 testGetAvailableShippingMethods()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId));

self::assertArrayHasKey('cart', $response);
Expand All @@ -61,15 +50,15 @@ public function testGetAvailableShippingMethods()
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);

$expectedAddressData = [
'amount' => 5,
'base_amount' => 5,
'amount' => 10,
'base_amount' => 10,
'carrier_code' => 'flatrate',
'carrier_title' => 'Flat Rate',
'error_message' => '',
'method_code' => 'flatrate',
'method_title' => 'Fixed',
'price_incl_tax' => 5,
'price_excl_tax' => 5,
'price_incl_tax' => 10,
'price_excl_tax' => 10,
];
self::assertEquals(
$expectedAddressData,
Expand All @@ -78,33 +67,38 @@ public function testGetAvailableShippingMethods()
}

/**
* Test case: get available shipping methods from customer's quote
*
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 testGetAvailableShippingMethodsFromCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_1');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);

$this->graphQlQuery($this->getQuery($maskedQuoteId));
}

/**
* Test case: get available shipping methods when all shipping methods are disabled
*
* @magentoApiDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
* @magentoApiDataFixture Magento/OfflineShipping/_files/disable_offline_shipping_methods.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.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
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php
*/
public function testGetAvailableShippingMethodsIfShippingsAreDisabled()
public function testGetAvailableShippingMethodsIfShippingMethodsAreNotPresent()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('guest_quote');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId));

self::assertEquals(0, count($response['cart']['shipping_addresses'][0]['available_shipping_methods']));
self::assertEmpty($response['cart']['shipping_addresses'][0]['available_shipping_methods']);
}

/**
Expand All @@ -116,8 +110,9 @@ public function testGetAvailableShippingMethodsIfShippingsAreDisabled()
public function testGetAvailableShippingMethodsOfNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);

$this->graphQlQuery($this->getQuery($maskedQuoteId));
$this->graphQlQuery($query);
}

/**
Expand Down Expand Up @@ -146,16 +141,4 @@ private function getQuery(string $maskedQuoteId): string
}
QUERY;
}

/**
* @param string $reservedOrderId
* @return string
*/
private function getMaskedQuoteIdByReservedOrderId(string $reservedOrderId): string
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id');

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php
/**
* Rollback for quote_with_simple_product_saved_rollback.php fixture.
* Rollback for quote_with_payment_saved.php fixture.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

require 'simple_product_rollback.php';

/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$registry->register('isSecureArea', true);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);

try {
$product = $productRepository->get('simple', false, null, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167
declare(strict_types=1);

use Magento\Framework\App\Config\Storage\Writer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167
declare(strict_types=1);

use Magento\Framework\App\Config\Storage\Writer;
Expand Down

0 comments on commit b64170a

Please sign in to comment.