Skip to content

Commit 2d8bc6d

Browse files
committed
magento/graphql-ce#683: Add discount information to Cart
- extracted discount cases to separate test class; - added case for no discount applied; - return `null` for discount node if amount is 0.
1 parent a1e126c commit 2d8bc6d

File tree

5 files changed

+228
-89
lines changed

5 files changed

+228
-89
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ private function getAppliedTaxes(Total $total, string $currency): array
9191
*
9292
* @param Total $total
9393
* @param string $currency
94-
* @return array
94+
* @return array|null
9595
*/
9696
private function getDiscount(Total $total, string $currency)
9797
{
98+
if ($total->getDiscountAmount() === 0) {
99+
return null;
100+
}
98101
return [
99102
'label' => explode(', ', $total->getDiscountDescription()),
100103
'amount' => ['value' => $total->getDiscountAmount(), 'currency' => $currency]
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote\Customer;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\Integration\Api\CustomerTokenServiceInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test is getting cart discount for registered customer.
17+
*/
18+
class CartDiscountTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var CustomerTokenServiceInterface
22+
*/
23+
private $customerTokenService;
24+
25+
/**
26+
* @var GetMaskedQuoteIdByReservedOrderId
27+
*/
28+
private $getMaskedQuoteIdByReservedOrderId;
29+
30+
protected function setUp()
31+
{
32+
$objectManager = Bootstrap::getObjectManager();
33+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
34+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
35+
}
36+
37+
/**
38+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
39+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
40+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
41+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
42+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
43+
*/
44+
public function testGetDiscountInformation()
45+
{
46+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
47+
$query = $this->getQuery($maskedQuoteId);
48+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
49+
$discountResponse = $response['cart']['prices']['discount'];
50+
self::assertEquals(-10, $discountResponse['amount']['value']);
51+
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
52+
}
53+
54+
/**
55+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
56+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
57+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
58+
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
59+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
60+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
61+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
62+
*/
63+
public function testGetDiscountInformationWithTwoRulesApplied()
64+
{
65+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
66+
$query = $this->getQuery($maskedQuoteId);
67+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
68+
$discountResponse = $response['cart']['prices']['discount'];
69+
self::assertEquals(-11, $discountResponse['amount']['value']);
70+
self::assertEquals(['50% Off for all orders', 'Test Coupon for General'], $discountResponse['label']);
71+
}
72+
73+
/**
74+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
75+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
76+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
77+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
78+
*/
79+
public function testGetDiscountInformationWithNoRulesApplied()
80+
{
81+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
82+
$query = $this->getQuery($maskedQuoteId);
83+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
84+
self::assertEquals(null, $response['cart']['prices']['discount']);
85+
}
86+
87+
/**
88+
* Generates GraphQl query for retrieving cart discount
89+
*
90+
* @param string $maskedQuoteId
91+
* @return string
92+
*/
93+
private function getQuery(string $maskedQuoteId): string
94+
{
95+
return <<<QUERY
96+
{
97+
cart(cart_id: "$maskedQuoteId") {
98+
prices {
99+
discount {
100+
label
101+
amount {
102+
value
103+
currency
104+
}
105+
}
106+
}
107+
}
108+
}
109+
QUERY;
110+
}
111+
112+
/**
113+
* @param string $username
114+
* @param string $password
115+
* @return array
116+
*/
117+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
118+
{
119+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
120+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
121+
return $headerMap;
122+
}
123+
}

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -153,44 +153,6 @@ public function testGetTotalsFromAnotherCustomerCart()
153153
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
154154
}
155155

156-
/**
157-
* @magentoApiDataFixture Magento/Customer/_files/customer.php
158-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
159-
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
160-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
161-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
162-
*/
163-
public function testGetDiscountInformation()
164-
{
165-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
166-
$query = $this->getQuery($maskedQuoteId);
167-
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
168-
169-
$discountResponse = $response['cart']['prices']['discount'];
170-
self::assertEquals(-10, $discountResponse['amount']['value']);
171-
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
172-
}
173-
174-
/**
175-
* @magentoApiDataFixture Magento/Customer/_files/customer.php
176-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
177-
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
178-
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
179-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
180-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
181-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
182-
*/
183-
public function testGetDiscountInformationWithTwoRulesApplied()
184-
{
185-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
186-
$query = $this->getQuery($maskedQuoteId);
187-
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
188-
189-
$discountResponse = $response['cart']['prices']['discount'];
190-
self::assertEquals(-11, $discountResponse['amount']['value']);
191-
self::assertEquals(['50% Off for all orders', 'Test Coupon for General'], $discountResponse['label']);
192-
}
193-
194156
/**
195157
* Generates GraphQl query for retrieving cart totals
196158
*
@@ -226,13 +188,6 @@ private function getQuery(string $maskedQuoteId): string
226188
currency
227189
}
228190
}
229-
discount {
230-
label
231-
amount {
232-
value
233-
currency
234-
}
235-
}
236191
}
237192
}
238193
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote\Guest;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
14+
/**
15+
* Test is getting cart discount for guest customer.
16+
*/
17+
class CartDiscountTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var GetMaskedQuoteIdByReservedOrderId
21+
*/
22+
private $getMaskedQuoteIdByReservedOrderId;
23+
24+
protected function setUp()
25+
{
26+
$objectManager = Bootstrap::getObjectManager();
27+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
28+
}
29+
30+
/**
31+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
32+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
33+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
34+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
35+
*/
36+
public function testGetDiscountInformation()
37+
{
38+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
39+
$query = $this->getQuery($maskedQuoteId);
40+
$response = $this->graphQlQuery($query);
41+
$discountResponse = $response['cart']['prices']['discount'];
42+
self::assertEquals(-10, $discountResponse['amount']['value']);
43+
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
44+
}
45+
46+
/**
47+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
48+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
49+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
50+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
51+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
52+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
53+
*/
54+
public function testGetDiscountInformationWithTwoRulesApplied()
55+
{
56+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
57+
$query = $this->getQuery($maskedQuoteId);
58+
$response = $this->graphQlQuery($query);
59+
$discountResponse = $response['cart']['prices']['discount'];
60+
self::assertEquals(-15, $discountResponse['amount']['value']);
61+
self::assertEquals(['50% Off for all orders', '5$ fixed discount on whole cart'], $discountResponse['label']);
62+
}
63+
64+
/**
65+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
66+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
67+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
68+
*/
69+
public function testGetDiscountInformationWithNoRulesApplied()
70+
{
71+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
72+
$query = $this->getQuery($maskedQuoteId);
73+
$response = $this->graphQlQuery($query);
74+
self::assertEquals(null, $response['cart']['prices']['discount']);
75+
}
76+
77+
/**
78+
* Generates GraphQl query for retrieving cart discount.
79+
*
80+
* @param string $maskedQuoteId
81+
* @return string
82+
*/
83+
private function getQuery(string $maskedQuoteId): string
84+
{
85+
return <<<QUERY
86+
{
87+
cart(cart_id: "$maskedQuoteId") {
88+
prices {
89+
discount {
90+
label
91+
amount {
92+
value
93+
currency
94+
}
95+
}
96+
}
97+
}
98+
}
99+
QUERY;
100+
}
101+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -124,42 +124,6 @@ public function testGetSelectedShippingMethodFromCustomerCart()
124124
$this->graphQlQuery($query);
125125
}
126126

127-
/**
128-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
129-
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
130-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
131-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
132-
*/
133-
public function testGetDiscountInformation()
134-
{
135-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
136-
$query = $this->getQuery($maskedQuoteId);
137-
$response = $this->graphQlQuery($query);
138-
139-
$discountResponse = $response['cart']['prices']['discount'];
140-
self::assertEquals(-10, $discountResponse['amount']['value']);
141-
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
142-
}
143-
144-
/**
145-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
146-
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
147-
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
148-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
149-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
150-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
151-
*/
152-
public function testGetDiscountInformationWithTwoRulesApplied()
153-
{
154-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
155-
$query = $this->getQuery($maskedQuoteId);
156-
$response = $this->graphQlQuery($query);
157-
158-
$discountResponse = $response['cart']['prices']['discount'];
159-
self::assertEquals(-15, $discountResponse['amount']['value']);
160-
self::assertEquals(['50% Off for all orders', '5$ fixed discount on whole cart'], $discountResponse['label']);
161-
}
162-
163127
/**
164128
* Generates GraphQl query for retrieving cart totals
165129
*
@@ -195,13 +159,6 @@ private function getQuery(string $maskedQuoteId): string
195159
currency
196160
}
197161
}
198-
discount {
199-
label
200-
amount {
201-
value
202-
currency
203-
}
204-
}
205162
}
206163
}
207164
}

0 commit comments

Comments
 (0)