Skip to content

Commit

Permalink
magento/graphql-ce#683: Add discount information to Cart
Browse files Browse the repository at this point in the history
- extracted discount cases to separate test class;
- added case for no discount applied;
- return `null` for discount node if amount is 0.
  • Loading branch information
lenaorobei committed Jun 12, 2019
1 parent a1e126c commit 2d8bc6d
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 89 deletions.
5 changes: 4 additions & 1 deletion app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ private function getAppliedTaxes(Total $total, string $currency): array
*
* @param Total $total
* @param string $currency
* @return array
* @return array|null
*/
private function getDiscount(Total $total, string $currency)
{
if ($total->getDiscountAmount() === 0) {
return null;
}
return [
'label' => explode(', ', $total->getDiscountDescription()),
'amount' => ['value' => $total->getDiscountAmount(), 'currency' => $currency]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Customer;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test is getting cart discount for registered customer.
*/
class CartDiscountTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.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 testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-10, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testGetDiscountInformationWithTwoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-11, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders', 'Test Coupon for General'], $discountResponse['label']);
}

/**
* @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 testGetDiscountInformationWithNoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
self::assertEquals(null, $response['cart']['prices']['discount']);
}

/**
* Generates GraphQl query for retrieving cart discount
*
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
prices {
discount {
label
amount {
value
currency
}
}
}
}
}
QUERY;
}

/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,44 +153,6 @@ public function testGetTotalsFromAnotherCustomerCart()
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.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 testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-10, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testGetDiscountInformationWithTwoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-11, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders', 'Test Coupon for General'], $discountResponse['label']);
}

/**
* Generates GraphQl query for retrieving cart totals
*
Expand Down Expand Up @@ -226,13 +188,6 @@ private function getQuery(string $maskedQuoteId): string
currency
}
}
discount {
label
amount {
value
currency
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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 is getting cart discount for guest customer.
*/
class CartDiscountTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.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 testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-10, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testGetDiscountInformationWithTwoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);
$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-15, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders', '5$ fixed discount on whole cart'], $discountResponse['label']);
}

/**
* @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 testGetDiscountInformationWithNoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);
self::assertEquals(null, $response['cart']['prices']['discount']);
}

/**
* Generates GraphQl query for retrieving cart discount.
*
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
prices {
discount {
label
amount {
value
currency
}
}
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,42 +124,6 @@ public function testGetSelectedShippingMethodFromCustomerCart()
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.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 testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);

$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-10, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders'], $discountResponse['label']);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
*/
public function testGetDiscountInformationWithTwoRulesApplied()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);

$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-15, $discountResponse['amount']['value']);
self::assertEquals(['50% Off for all orders', '5$ fixed discount on whole cart'], $discountResponse['label']);
}

/**
* Generates GraphQl query for retrieving cart totals
*
Expand Down Expand Up @@ -195,13 +159,6 @@ private function getQuery(string $maskedQuoteId): string
currency
}
}
discount {
label
amount {
value
currency
}
}
}
}
}
Expand Down

0 comments on commit 2d8bc6d

Please sign in to comment.