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

Commit

Permalink
ENGCOM-4704: #544: Replace deprecated fixtures in RemoveItemFromCartT…
Browse files Browse the repository at this point in the history
…est #545
  • Loading branch information
naydav authored Apr 8, 2019
2 parents 1d0e0ad + 22c2b57 commit e7953a7
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

namespace Magento\GraphQl\Quote\Customer;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku;
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 @@ -26,46 +24,37 @@ class RemoveItemFromCartTest extends GraphQlAbstract
private $customerTokenService;

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

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

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

/**
* @var ProductRepositoryInterface
*/
private $productRepository;
private $getQuoteItemIdByReservedQuoteIdAndSku;

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->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->getQuoteItemIdByReservedQuoteIdAndSku = $objectManager->get(
GetQuoteItemIdByReservedQuoteIdAndSku::class
);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @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
*/
public function testRemoveItemFromCart()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
$itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId();
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$itemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');

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

$this->assertArrayHasKey('removeItemFromCart', $response);
Expand All @@ -80,105 +69,24 @@ public function testRemoveItemFromCart()
*/
public function testRemoveItemFromNonExistentCart()
{
$query = $this->prepareMutationQuery('non_existent_masked_id', 1);
$query = $this->getQuery('non_existent_masked_id', 1);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @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
*/
public function testRemoveNonExistentItem()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$notExistentItemId = 999;

$this->expectExceptionMessage("Cart doesn't contain the {$notExistentItemId} item.");

$query = $this->prepareMutationQuery($maskedQuoteId, $notExistentItemId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
*/
public function testRemoveItemIfItemIsNotBelongToCart()
{
$firstQuote = $this->quoteFactory->create();
$this->quoteResource->load($firstQuote, 'test_order_1', 'reserved_order_id');
$firstQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$firstQuote->getId());

$secondQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$secondQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$secondQuote->setCustomerId(1);
$this->quoteResource->save($secondQuote);
$secondQuoteItemId = (int)$secondQuote
->getItemByProduct($this->productRepository->get('virtual-product'))
->getId();

$this->expectExceptionMessage("Cart doesn't contain the {$secondQuoteItemId} item.");

$query = $this->prepareMutationQuery($firstQuoteMaskedId, $secondQuoteItemId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
*/
public function testRemoveItemFromGuestCart()
{
$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
$guestQuoteItemId = (int)$guestQuote
->getItemByProduct($this->productRepository->get('virtual-product'))
->getId();

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

$query = $this->prepareMutationQuery($guestQuoteMaskedId, $guestQuoteItemId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
*/
public function testRemoveItemFromAnotherCustomerCart()
{
$anotherCustomerQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$anotherCustomerQuote,
'test_order_with_virtual_product_without_address',
'reserved_order_id'
);
$anotherCustomerQuote->setCustomerId(2);
$this->quoteResource->save($anotherCustomerQuote);

$anotherCustomerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$anotherCustomerQuote->getId());
$anotherCustomerQuoteItemId = (int)$anotherCustomerQuote
->getItemByProduct($this->productRepository->get('virtual-product'))
->getId();

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

$query = $this->prepareMutationQuery($anotherCustomerQuoteMaskedId, $anotherCustomerQuoteItemId);
$query = $this->getQuery($maskedQuoteId, $notExistentItemId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

Expand Down Expand Up @@ -226,12 +134,77 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
];
}

/**
* _security
* @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/Checkout/_files/quote_with_virtual_product_and_address.php
*/
public function testRemoveItemIfItemIsNotBelongToCart()
{
$firstQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$secondQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute(
'test_order_with_virtual_product',
'virtual-product'
);

$this->expectExceptionMessage("Cart doesn't contain the {$secondQuoteItemId} item.");

$query = $this->getQuery($firstQuoteMaskedId, $secondQuoteItemId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @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
*/
public function testRemoveItemFromGuestCart()
{
$guestQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$guestQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');

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

$query = $this->getQuery($guestQuoteMaskedId, $guestQuoteItemId);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.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
*/
public function testRemoveItemFromAnotherCustomerCart()
{
$anotherCustomerQuoteMaskedId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$anotherCustomerQuoteItemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute(
'test_quote',
'simple_product'
);

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

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

/**
* @param string $maskedQuoteId
* @param int $itemId
* @return string
*/
private function prepareMutationQuery(string $maskedQuoteId, int $itemId): string
private function getQuery(string $maskedQuoteId, int $itemId): string
{
return <<<QUERY
mutation {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\Quote\Model\QuoteFactory;

/**
* Get quote item id by reserved order id and product sku
*/
class GetQuoteItemIdByReservedQuoteIdAndSku
{
/**
* @var QuoteFactory
*/
private $quoteFactory;

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

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @param QuoteFactory $quoteFactory
* @param QuoteResource $quoteResource
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
QuoteFactory $quoteFactory,
QuoteResource $quoteResource,
ProductRepositoryInterface $productRepository
) {
$this->quoteFactory = $quoteFactory;
$this->quoteResource = $quoteResource;
$this->productRepository = $productRepository;
}

/**
* Get quote item id by reserved order id and product sku
*
* @param string $reservedOrderId
* @param string $sku
* @return int
* @throws NoSuchEntityException
*/
public function execute(string $reservedOrderId, string $sku): int
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id');
$product = $this->productRepository->get($sku);

return (int)$quote->getItemByProduct($product)->getId();
}
}
Loading

0 comments on commit e7953a7

Please sign in to comment.