Skip to content

Commit

Permalink
GraphQL-538: Api-functional tests fixture refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Mar 27, 2019
1 parent ef61afe commit 4285ac5
Show file tree
Hide file tree
Showing 26 changed files with 792 additions and 547 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,56 @@ protected function setUp()
}

/**
* @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->execute('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->execute(
'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->execute('test_order_item_with_items');
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
Expand All @@ -89,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->execute('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 @@ -118,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

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,21 +22,6 @@ class GetCartTest extends GraphQlAbstract
*/
private $getMaskedQuoteIdByReservedOrderId;

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

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

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

/**
* @var CustomerTokenServiceInterface
*/
Expand All @@ -49,19 +31,21 @@ protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::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->getMaskedQuoteIdByReservedOrderId->execute('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 @@ -70,24 +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->getMaskedQuoteIdByReservedOrderId->execute(
'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 @@ -96,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->getMaskedQuoteIdByReservedOrderId->execute('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 @@ -112,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 @@ -147,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 Down

0 comments on commit 4285ac5

Please sign in to comment.