magento2-docker/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/ApplyCouponsToCartTest.php

168 lines
5.8 KiB
PHP
Executable File

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Quote\Guest;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test Apply Coupons to Cart functionality for guest
*/
class ApplyCouponsToCartTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}
/**
* @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
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testApplyCouponsToCart()
{
$couponCode = '2?ds5!2d';
$expectedGrandTotal = 15.00;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$response = $this->graphQlMutation($query);
self::assertArrayHasKey('applyCouponToCart', $response);
self::assertEquals($couponCode, $response['applyCouponToCart']['cart']['applied_coupons'][0]['code']);
self::assertEquals(
$expectedGrandTotal,
$response['applyCouponToCart']['cart']['prices']['grand_total']['value']
);
}
/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testApplyCouponsToCartWithoutItems()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cart does not contain products.');
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$this->graphQlMutation($query);
}
/**
* _security
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
*/
public function testApplyCouponsToCustomerCart()
{
$this->expectException(\Exception::class);
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
$this->graphQlMutation($query);
}
/**
* @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 testApplyNonExistentCouponToCart()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The coupon code isn\'t valid. Verify the code and try again.');
$couponCode = 'non_existent_coupon_code';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$this->graphQlMutation($query);
}
/**
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
*/
public function testApplyCouponsToNonExistentCart()
{
$this->expectException(\Exception::class);
$couponCode = '2?ds5!2d';
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId, $couponCode);
self::expectExceptionMessage('Could not find a cart with ID "' . $maskedQuoteId . '"');
$this->graphQlMutation($query);
}
/**
* Products in cart don't fit to the coupon
*
* @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
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/restrict_coupon_usage_for_simple_product.php
*/
public function testApplyCouponsWhichIsNotApplicable()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The coupon code isn\'t valid. Verify the code and try again.');
$couponCode = '2?ds5!2d';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $couponCode);
$this->graphQlMutation($query);
}
/**
* @param string $maskedQuoteId
* @param string $couponCode
* @return string
*/
private function getQuery(string $maskedQuoteId, string $couponCode): string
{
return <<<QUERY
mutation {
applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) {
cart {
applied_coupons {
code
}
prices {
grand_total {
value
}
}
}
}
}
QUERY;
}
}