Skip to content

Commit 9000000

Browse files
author
Roman Glushko
committed
#481 Added more test cases covered
1 parent d7a5379 commit 9000000

File tree

1 file changed

+176
-2
lines changed

1 file changed

+176
-2
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveCouponFromCartTest.php

+176-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
namespace Magento\GraphQl\Quote\Customer;
99

1010
use Magento\Framework\Exception\AuthenticationException;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
1113
use Magento\Integration\Api\CustomerTokenServiceInterface;
1214
use Magento\Quote\Model\Quote;
1315
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1416
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
17+
use Magento\SalesRule\Api\CouponRepositoryInterface;
18+
use Magento\SalesRule\Model\Coupon;
1519
use Magento\TestFramework\Helper\Bootstrap;
1620
use Magento\TestFramework\TestCase\GraphQlAbstract;
1721

@@ -40,13 +44,25 @@ class RemoveCouponFromCartTest extends GraphQlAbstract
4044
*/
4145
private $customerTokenService;
4246

47+
/**
48+
* @var CouponRepositoryInterface
49+
*/
50+
private $couponRepository;
51+
52+
/**
53+
* @var Coupon
54+
*/
55+
private $coupon;
56+
4357
protected function setUp()
4458
{
4559
$objectManager = Bootstrap::getObjectManager();
4660
$this->quoteResource = $objectManager->create(QuoteResource::class);
4761
$this->quote = $objectManager->create(Quote::class);
4862
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
4963
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
64+
$this->couponRepository = $objectManager->get(CouponRepositoryInterface::class);
65+
$this->coupon = $objectManager->create(Coupon::class);
5066
}
5167

5268
/**
@@ -81,16 +97,119 @@ public function testRemoveCouponFromCart()
8197
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
8298
}
8399

100+
/**
101+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
102+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
103+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
104+
*/
105+
public function testRemoveCouponFromCartTwice()
106+
{
107+
$couponCode = '2?ds5!2d';
108+
109+
/* Apply coupon to the customer quote */
110+
$this->quoteResource->load(
111+
$this->quote,
112+
'test_order_with_simple_product_without_address',
113+
'reserved_order_id'
114+
);
115+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
116+
117+
$this->quote->setCustomerId(1);
118+
$this->quoteResource->save($this->quote);
119+
120+
$queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password');
121+
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
122+
$this->graphQlQuery($query, [], '', $queryHeaders);
123+
124+
/* Remove coupon from the quote */
125+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
126+
$response = $this->graphQlQuery($query, [], '', $queryHeaders);
127+
128+
self::assertArrayHasKey('removeCouponFromCart', $response);
129+
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
130+
131+
/* Remove coupon from the quote the second time */
132+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
133+
$response = $this->graphQlQuery($query, [], '', $queryHeaders);
134+
135+
self::assertArrayHasKey('removeCouponFromCart', $response);
136+
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
137+
}
138+
139+
/**
140+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
141+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
142+
*/
143+
public function testRemoveCouponFromEmptyCart()
144+
{
145+
/* Assign the empty quote to the customer */
146+
$this->quoteResource->load(
147+
$this->quote,
148+
'test_order_1',
149+
'reserved_order_id'
150+
);
151+
$quoteId = (int)$this->quote->getId();
152+
$maskedQuoteId = $this->quoteIdToMaskedId->execute($quoteId);
153+
154+
$this->quote->setCustomerId(1);
155+
$this->quoteResource->save($this->quote);
156+
157+
/* Remove coupon from the empty quote */
158+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
159+
$queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password');
160+
161+
$this->expectExceptionMessage("The \"$quoteId\" Cart doesn't contain products");
162+
$this->graphQlQuery($query, [], '', $queryHeaders);
163+
}
164+
165+
/**
166+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
167+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
168+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
169+
*/
170+
public function testRemoveCouponFromCartWithoutItems()
171+
{
172+
$couponCode = '2?ds5!2d';
173+
174+
/* Assign the quote to the customer */
175+
$this->quoteResource->load(
176+
$this->quote,
177+
'test_order_with_simple_product_without_address',
178+
'reserved_order_id'
179+
);
180+
181+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
182+
183+
$this->quote->setCustomerId(1);
184+
$this->quoteResource->save($this->quote);
185+
186+
/* Apply coupon to the customer quote */
187+
$queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password');
188+
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
189+
$this->graphQlQuery($query, [], '', $queryHeaders);
190+
191+
/* Clear the quote */
192+
$this->quote->removeAllItems();
193+
$this->quoteResource->save($this->quote);
194+
195+
/* Remove coupon from the customer quote */
196+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
197+
$response = $this->graphQlQuery($query, [], '', $queryHeaders);
198+
199+
self::assertArrayHasKey('removeCouponFromCart', $response);
200+
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
201+
}
202+
84203
/**
85204
* @magentoApiDataFixture Magento/Customer/_files/two_customers.php
86205
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
87206
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
88207
*/
89-
public function testRemoveCouponFromAonotherCustomerCart()
208+
public function testRemoveCouponFromAnotherCustomerCart()
90209
{
91210
$couponCode = '2?ds5!2d';
92211

93-
/* Apply coupon to the first customer quote */
212+
/* Assign the quote to the customer */
94213
$this->quoteResource->load(
95214
$this->quote,
96215
'test_order_with_simple_product_without_address',
@@ -101,6 +220,7 @@ public function testRemoveCouponFromAonotherCustomerCart()
101220
$this->quote->setCustomerId(1);
102221
$this->quoteResource->save($this->quote);
103222

223+
/* Apply coupon to the first customer quote */
104224
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
105225
$queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password');
106226
$this->graphQlQuery($query, [], '', $queryHeaders);
@@ -141,6 +261,60 @@ public function testRemoveCouponFromGuestCart()
141261
$this->graphQlQuery($query, [], '', $queryHeaders);
142262
}
143263

264+
/**
265+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
266+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
267+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
268+
*/
269+
public function testRemoveNonExistentCouponFromCart()
270+
{
271+
$couponCode = '2?ds5!2d';
272+
273+
/* Assign the quote to the customer */
274+
$this->quoteResource->load(
275+
$this->quote,
276+
'test_order_with_simple_product_without_address',
277+
'reserved_order_id'
278+
);
279+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
280+
281+
$this->quote->setCustomerId(1);
282+
$this->quoteResource->save($this->quote);
283+
284+
/* Apply coupon to the customer quote */
285+
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
286+
$queryHeaders = $this->prepareAuthorizationHeaders('customer@example.com', 'password');
287+
$this->graphQlQuery($query, [], '', $queryHeaders);
288+
289+
/* Remove the coupon */
290+
$this->removeCoupon($couponCode);
291+
292+
/* Remove the non-existent coupon from the quote */
293+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
294+
295+
$response = $this->graphQlQuery($query, [], '', $queryHeaders);
296+
297+
self::assertArrayHasKey('removeCouponFromCart', $response);
298+
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
299+
}
300+
301+
/**
302+
* Remove the given coupon code from the database
303+
*
304+
* @param string $couponCode
305+
* @throws LocalizedException
306+
* @throws NoSuchEntityException
307+
*/
308+
private function removeCoupon(string $couponCode): void
309+
{
310+
$this->coupon->loadByCode($couponCode);
311+
$couponId = $this->coupon->getCouponId();
312+
313+
if ($couponId) {
314+
$this->couponRepository->deleteById($couponId);
315+
}
316+
}
317+
144318
/**
145319
* Retrieve customer authorization headers
146320
*

0 commit comments

Comments
 (0)