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

Api-functional tests fixture refactoring #539

Merged
merged 7 commits into from
Mar 27, 2019
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,75 +23,71 @@ class GetAvailablePaymentMethodsTest extends GraphQlAbstract
private $customerTokenService;

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

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

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

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

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_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 testGetCartWithPaymentMethods()
public function testGetAvailablePaymentMethods()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_item_with_items');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

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

self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']);
self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']);
self::assertGreaterThan(
0,
count($response['cart']['available_payment_methods']),
'There are no available payment methods for customer cart!'
);
}

/**
* @security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.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 testGetPaymentMethodsFromGuestCart()
public function testGetAvailablePaymentMethodsFromGuestCart()
{
$guestQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId(
'test_order_with_virtual_product_without_address'
);
$query = $this->getQuery($guestQuoteMaskedId);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

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

/**
* @security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_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 testGetPaymentMethodsFromAnotherCustomerCart()
public function testGetAvailablePaymentMethodsFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_item_with_items');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
Expand All @@ -103,24 +97,31 @@ public function testGetPaymentMethodsFromAnotherCustomerCart()
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
* @magentoApiDataFixture Magento/Payment/_files/disable_all_active_payment_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_all_active_payment_methods.php
*/
public function testGetPaymentMethodsIfPaymentsAreNotSet()
public function testGetAvailablePaymentMethodsIfPaymentsAreNotPresent()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId('test_order_item_with_items');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

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

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
* @expectedException \Exception
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
*/
public function testGetPaymentMethodsOfNonExistentCart()
public function testGetAvailablePaymentMethodsOfNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);
Expand All @@ -132,9 +133,8 @@ public function testGetPaymentMethodsOfNonExistentCart()
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(
string $maskedQuoteId
): string {
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
Expand All @@ -158,16 +158,4 @@ private function getHeaderMap(string $username = '[email protected]', 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,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 @@ -20,19 +18,9 @@
class GetCartTest extends GraphQlAbstract
{
/**
* @var QuoteResource
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $quoteResource;

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

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

/**
* @var CustomerTokenServiceInterface
Expand All @@ -42,19 +30,22 @@ class GetCartTest extends GraphQlAbstract
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.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/add_virtual_product.php
*/
public function testGetCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_item_with_items');
$query = $this->getCartQuery($maskedQuoteId);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

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

Expand All @@ -63,22 +54,23 @@ public function testGetCart()
self::assertCount(2, $response['cart']['items']);

self::assertNotEmpty($response['cart']['items'][0]['id']);
self::assertEquals($response['cart']['items'][0]['qty'], 2);
self::assertEquals($response['cart']['items'][0]['product']['sku'], 'simple');
self::assertEquals(2, $response['cart']['items'][0]['qty']);
self::assertEquals('simple', $response['cart']['items'][0]['product']['sku']);

self::assertNotEmpty($response['cart']['items'][1]['id']);
self::assertEquals($response['cart']['items'][1]['qty'], 1);
self::assertEquals($response['cart']['items'][1]['product']['sku'], 'simple_one');
self::assertEquals(2, $response['cart']['items'][1]['qty']);
self::assertEquals('virtual-product', $response['cart']['items'][1]['product']['sku']);
}

/**
* @security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
*/
public function testGetGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
$query = $this->getCartQuery($maskedQuoteId);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
Expand All @@ -87,13 +79,14 @@ public function testGetGuestCart()
}

/**
* @security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testGetAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_item_with_items');
$query = $this->getCartQuery($maskedQuoteId);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"{$maskedQuoteId}\""
Expand All @@ -103,33 +96,30 @@ public function testGetAnotherCustomerCart()

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

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

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/make_cart_inactive.php
*
* @expectedException \Exception
* @expectedExceptionMessage Current user does not have an active cart.
*/
public function testGetInactiveCart()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id');
$quote->setCustomerId(1);
$quote->setIsActive(false);
$this->quoteResource->save($quote);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());

$query = $this->getCartQuery($maskedQuoteId);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}
Expand All @@ -138,12 +128,11 @@ public function testGetInactiveCart()
* @param string $maskedQuoteId
* @return string
*/
private function getCartQuery(
string $maskedQuoteId
) : string {
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
cart(cart_id: "{$maskedQuoteId}") {
items {
id
qty
Expand All @@ -156,18 +145,6 @@ private function getCartQuery(
QUERY;
}

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

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* @param string $username
* @param string $password
Expand Down
Loading