|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\SalesRuleGraphQl\Test\Unit\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 12 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 13 | +use Magento\Quote\Api\Data\CartInterface; |
| 14 | +use Magento\SalesRule\Api\Data\CouponInterface; |
| 15 | +use Magento\SalesRule\Api\Data\RuleDiscountInterface; |
| 16 | +use Magento\SalesRule\Model\GetCoupons; |
| 17 | +use Magento\Sales\Api\Data\OrderInterface; |
| 18 | +use Magento\SalesRule\Model\Quote\GetCouponCodes; |
| 19 | +use Magento\SalesRuleGraphQl\Model\Resolver\Coupon; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 23 | + |
| 24 | +class CouponTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var GetCouponCodes|MockObject |
| 28 | + */ |
| 29 | + private $getCouponCodesMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var GetCoupons|MockObject |
| 33 | + */ |
| 34 | + private $getCouponsMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Coupon|MockObject |
| 38 | + */ |
| 39 | + private $resolver; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ContextInterface|MockObject |
| 43 | + */ |
| 44 | + private $contextMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Field|MockObject |
| 48 | + */ |
| 49 | + private $fieldMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var ResolveInfo|MockObject |
| 53 | + */ |
| 54 | + private $resolveInfoMock; |
| 55 | + |
| 56 | + protected function setUp(): void |
| 57 | + { |
| 58 | + $this->getCouponCodesMock = $this->createMock(GetCouponCodes::class); |
| 59 | + $this->getCouponsMock = $this->createMock(GetCoupons::class); |
| 60 | + $this->contextMock = $this->createMock(ContextInterface::class); |
| 61 | + $this->fieldMock = $this->createMock(Field::class); |
| 62 | + $this->resolveInfoMock = $this->createMock(ResolveInfo::class); |
| 63 | + $this->resolver = new Coupon( |
| 64 | + $this->getCouponCodesMock, |
| 65 | + $this->getCouponsMock |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function testResolveWithOrderModel(): void |
| 70 | + { |
| 71 | + $orderModel = $this->createMock(OrderInterface::class); |
| 72 | + $orderModel->expects($this->once()) |
| 73 | + ->method('getCouponCode') |
| 74 | + ->willReturn('TEST1234'); |
| 75 | + $result = $this->resolver->resolve( |
| 76 | + $this->fieldMock, |
| 77 | + $this->contextMock, |
| 78 | + $this->resolveInfoMock, |
| 79 | + ['order_model' => $orderModel] |
| 80 | + ); |
| 81 | + |
| 82 | + $this->assertEquals(['code' => 'TEST1234'], $result); |
| 83 | + } |
| 84 | + |
| 85 | + public function testResolveWithoutDiscountModel(): void |
| 86 | + { |
| 87 | + $this->expectException(LocalizedException::class); |
| 88 | + $this->expectExceptionMessage('"discount_model" value should be specified'); |
| 89 | + $this->resolver->resolve( |
| 90 | + $this->fieldMock, |
| 91 | + $this->contextMock, |
| 92 | + $this->resolveInfoMock, |
| 93 | + [] |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + public function testResolveWithoutQuoteModel(): void |
| 98 | + { |
| 99 | + $this->expectException(LocalizedException::class); |
| 100 | + $this->expectExceptionMessage('"quote_model" value should be specified'); |
| 101 | + $this->resolver->resolve( |
| 102 | + $this->fieldMock, |
| 103 | + $this->contextMock, |
| 104 | + $this->resolveInfoMock, |
| 105 | + ['discount_model' => $this->createMock(RuleDiscountInterface::class)] |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + public function testResolveWithoutCoupon(): void |
| 110 | + { |
| 111 | + $quoteModel = $this->createMock(CartInterface::class); |
| 112 | + $discountModel = $this->createMock(RuleDiscountInterface::class); |
| 113 | + $this->getCouponCodesMock->method('execute')->willReturn([]); |
| 114 | + $this->getCouponsMock->method('execute')->willReturn([]); |
| 115 | + $result = $this->resolver->resolve( |
| 116 | + $this->fieldMock, |
| 117 | + $this->contextMock, |
| 118 | + $this->resolveInfoMock, |
| 119 | + ['discount_model' => $discountModel, 'quote_model' => $quoteModel] |
| 120 | + ); |
| 121 | + |
| 122 | + $this->assertNull($result); |
| 123 | + } |
| 124 | + |
| 125 | + public function testResolveWithMatchingRuleId(): void |
| 126 | + { |
| 127 | + $quoteModel = $this->createMock(CartInterface::class); |
| 128 | + $discountModel = $this->createMock(RuleDiscountInterface::class); |
| 129 | + $discountModel->method('getRuleID')->willReturn(123); |
| 130 | + $couponMock = $this->createMock(CouponInterface::class); |
| 131 | + $couponMock->method('getRuleId')->willReturn(123); |
| 132 | + $couponMock->method('getCode')->willReturn('TEST1234'); |
| 133 | + $this->getCouponCodesMock->method('execute')->willReturn(['test']); |
| 134 | + $this->getCouponsMock->method('execute')->willReturn([$couponMock]); |
| 135 | + $result = $this->resolver->resolve( |
| 136 | + $this->fieldMock, |
| 137 | + $this->contextMock, |
| 138 | + $this->resolveInfoMock, |
| 139 | + ['discount_model' => $discountModel, 'quote_model' => $quoteModel] |
| 140 | + ); |
| 141 | + |
| 142 | + $this->assertEquals(['code' => 'TEST1234'], $result); |
| 143 | + } |
| 144 | + |
| 145 | + public function testResolveNoMatchingRuleId(): void |
| 146 | + { |
| 147 | + $quoteModel = $this->createMock(CartInterface::class); |
| 148 | + $discountModel = $this->createMock(RuleDiscountInterface::class); |
| 149 | + $discountModel->method('getRuleID')->willReturn(123); |
| 150 | + $couponMock = $this->createMock(CouponInterface::class); |
| 151 | + $couponMock->method('getRuleId')->willReturn(321); |
| 152 | + $this->getCouponCodesMock->method('execute')->willReturn(['test']); |
| 153 | + $this->getCouponsMock->method('execute')->willReturn([$couponMock]); |
| 154 | + $result = $this->resolver->resolve( |
| 155 | + $this->fieldMock, |
| 156 | + $this->contextMock, |
| 157 | + $this->resolveInfoMock, |
| 158 | + ['discount_model' => $discountModel, 'quote_model' => $quoteModel] |
| 159 | + ); |
| 160 | + |
| 161 | + $this->assertNull($result); |
| 162 | + } |
| 163 | +} |
0 commit comments