Skip to content

Commit

Permalink
#481 Added cases to the guest coupon test
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Glushko committed Mar 17, 2019
1 parent a8b4cc9 commit 7677da6
Showing 1 changed file with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

namespace Magento\GraphQl\Quote\Guest;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\SalesRule\Api\CouponRepositoryInterface;
use Magento\SalesRule\Model\Coupon;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

Expand All @@ -33,12 +37,24 @@ class RemoveCouponFromCartTest extends GraphQlAbstract
*/
private $quoteIdToMaskedId;

/**
* @var CouponRepositoryInterface
*/
private $couponRepository;

/**
* @var Coupon
*/
private $coupon;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->create(QuoteResource::class);
$this->quote = $objectManager->create(Quote::class);
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
$this->couponRepository = $objectManager->get(CouponRepositoryInterface::class);
$this->coupon = $objectManager->create(Coupon::class);
}

/**
Expand Down Expand Up @@ -102,6 +118,106 @@ public function testRemoveCouponFromNonExistentCart()
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testRemoveCouponFromEmptyCart()
{
$this->quoteResource->load(
$this->quote,
'test_order_1',
'reserved_order_id'
);
$quoteId = (int)$this->quote->getId();
$maskedQuoteId = $this->quoteIdToMaskedId->execute($quoteId);

/* Remove coupon from the empty quote */
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);

$this->expectExceptionMessage("The \"$quoteId\" Cart doesn't contain products");
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testRemoveCouponFromCartWithoutItems()
{
$couponCode = '2?ds5!2d';

$this->quoteResource->load(
$this->quote,
'test_order_with_simple_product_without_address',
'reserved_order_id'
);

$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());

/* Apply coupon to the guest quote */
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
$this->graphQlQuery($query);

/* Clear the quote */
$this->quote->removeAllItems();
$this->quoteResource->save($this->quote);

/* Remove coupon from the guest quote */
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('removeCouponFromCart', $response);
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testRemoveNonExistentCouponFromCart()
{
$couponCode = '2?ds5!2d';

$this->quoteResource->load(
$this->quote,
'test_order_with_simple_product_without_address',
'reserved_order_id'
);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());

/* Apply coupon to the guest quote */
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
$this->graphQlQuery($query);

/* Remove the coupon */
$this->removeCoupon($couponCode);

/* Remove the non-existent coupon from the quote */
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);

$response = $this->graphQlQuery($query);

self::assertArrayHasKey('removeCouponFromCart', $response);
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
}

/**
* Remove the given coupon code from the database
*
* @param string $couponCode
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function removeCoupon(string $couponCode): void
{
$this->coupon->loadByCode($couponCode);
$couponId = $this->coupon->getCouponId();

if ($couponId) {
$this->couponRepository->deleteById($couponId);
}
}

/**
* @param string $maskedQuoteId
* @param string $couponCode
Expand Down

0 comments on commit 7677da6

Please sign in to comment.