Skip to content

Commit 4ebea96

Browse files
committed
GraphQL-481: [Test Coverage] 'RemoveCouponFromCart' functionality
1 parent 84314af commit 4ebea96

File tree

7 files changed

+170
-473
lines changed

7 files changed

+170
-473
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Quote\Api\CouponManagementInterface;
1415

1516
/**
1617
* @inheritdoc
1718
*/
1819
class AppliedCoupon implements ResolverInterface
1920
{
21+
/**
22+
* @var CouponManagementInterface
23+
*/
24+
private $couponManagement;
25+
26+
/**
27+
* @param CouponManagementInterface $couponManagement
28+
*/
29+
public function __construct(
30+
CouponManagementInterface $couponManagement
31+
) {
32+
$this->couponManagement = $couponManagement;
33+
}
34+
2035
/**
2136
* @inheritdoc
2237
*/
@@ -26,9 +41,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
2641
throw new LocalizedException(__('"model" value should be specified'));
2742
}
2843
$cart = $value['model'];
44+
$cartId = $cart->getId();
2945

30-
$appliedCoupon = $cart->getCouponCode();
31-
46+
$appliedCoupon = $this->couponManagement->get($cartId);
3247
return $appliedCoupon ? ['code' => $appliedCoupon] : null;
3348
}
3449
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6262
try {
6363
$this->couponManagement->remove($cartId);
6464
} catch (NoSuchEntityException $e) {
65-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
65+
$message = $e->getMessage();
66+
if (preg_match('/The "\d+" Cart doesn\'t contain products/', $message)) {
67+
$message = 'Cart does not contain products';
68+
}
69+
throw new GraphQlNoSuchEntityException(__($message), $e);
6670
} catch (CouldNotDeleteException $e) {
6771
throw new LocalizedException(__($e->getMessage()), $e);
6872
}

0 commit comments

Comments
 (0)