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

Commit

Permalink
GraphQL-478: [Test Coverage] 'Selected Shipping Method' functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Mar 29, 2019
1 parent 2ceceb7 commit 84a21a4
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 443 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Customer;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

class GetSelectedShippingMethodTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

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

/**
* @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/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethod()
{
$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::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);

self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
}

/**
* _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
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodFromGuestCart()
{
$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());
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.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/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodFromAnotherCustomerCart()
{
$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('customer3@search.example.com'));
}

/**
* @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 testGetGetSelectedShippingMethodIfShippingMethodIsNotSet()
{
$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::assertNull($shippingAddress['selected_shipping_method']['carrier_code']);
self::assertNull($shippingAddress['selected_shipping_method']['method_code']);
self::assertNull($shippingAddress['selected_shipping_method']['label']);
self::assertNull($shippingAddress['selected_shipping_method']['amount']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
* @expectedException \Exception
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
*/
public function testGetGetSelectedShippingMethodOfNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);

$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}

/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
label
amount
}
}
}
}
QUERY;
}
}
Loading

0 comments on commit 84a21a4

Please sign in to comment.