|
| 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\SalesRule\Test\Unit\Model\Coupon\Usage; |
| 9 | + |
| 10 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 11 | +use Magento\SalesRule\Model\Coupon; |
| 12 | +use Magento\SalesRule\Model\Coupon\Usage\Processor; |
| 13 | +use Magento\SalesRule\Model\Coupon\Usage\UpdateInfo; |
| 14 | +use Magento\SalesRule\Model\ResourceModel\Coupon\Usage; |
| 15 | +use Magento\SalesRule\Model\Rule; |
| 16 | +use Magento\SalesRule\Model\Rule\Customer; |
| 17 | +use Magento\SalesRule\Model\Rule\CustomerFactory; |
| 18 | +use Magento\SalesRule\Model\RuleFactory; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class ProcessorTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var Processor |
| 25 | + */ |
| 26 | + private $processor; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var RuleFactory |
| 30 | + */ |
| 31 | + private $ruleFactoryMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var CustomerFactory |
| 35 | + */ |
| 36 | + private $ruleCustomerFactoryMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Coupon |
| 40 | + */ |
| 41 | + private $couponMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Usage |
| 45 | + */ |
| 46 | + private $couponUsageMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var UpdateInfo |
| 50 | + */ |
| 51 | + private $updateInfoMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritDoc |
| 55 | + */ |
| 56 | + protected function setUp(): void |
| 57 | + { |
| 58 | + $this->ruleFactoryMock = $this->getMockBuilder(RuleFactory::class) |
| 59 | + ->disableOriginalConstructor() |
| 60 | + ->getMock(); |
| 61 | + |
| 62 | + $this->ruleCustomerFactoryMock = $this->getMockBuilder(CustomerFactory::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + |
| 66 | + $this->couponMock = $this->getMockBuilder(Coupon::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->getMock(); |
| 69 | + |
| 70 | + $this->couponUsageMock = $this->getMockBuilder(Usage::class) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->getMock(); |
| 73 | + |
| 74 | + $this->updateInfoMock = $this->getMockBuilder(UpdateInfo::class) |
| 75 | + ->disableOriginalConstructor() |
| 76 | + ->getMock(); |
| 77 | + |
| 78 | + $this->processor = (new ObjectManager($this))->getObject( |
| 79 | + Processor::class, |
| 80 | + [ |
| 81 | + 'ruleFactory' => $this->ruleFactoryMock, |
| 82 | + 'ruleCustomerFactory' => $this->ruleCustomerFactoryMock, |
| 83 | + 'coupon' => $this->couponMock, |
| 84 | + 'couponUsage' => $this->couponUsageMock |
| 85 | + ] |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test to update coupon usage |
| 91 | + * |
| 92 | + * @param $isIncrement |
| 93 | + * @param $timesUsed |
| 94 | + * |
| 95 | + * @return void |
| 96 | + * @dataProvider dataProvider |
| 97 | + */ |
| 98 | + public function testProcess($isIncrement, $timesUsed): void |
| 99 | + { |
| 100 | + $ruleId = 1; |
| 101 | + $customerId = 256; |
| 102 | + $couponId = 1; |
| 103 | + $couponCode = 'DISCOUNT-10'; |
| 104 | + $setTimesUsed = $timesUsed + ($isIncrement ? 1 : -1); |
| 105 | + $ruleCustomerId = 13; |
| 106 | + |
| 107 | + $this->updateInfoMock->expects($this->exactly(2))->method('getAppliedRuleIds')->willReturn([$couponId]); |
| 108 | + $this->updateInfoMock->expects($this->exactly(2))->method('getCouponCode')->willReturn($couponCode); |
| 109 | + $this->updateInfoMock->expects($this->exactly(3))->method('isIncrement')->willReturn($isIncrement); |
| 110 | + |
| 111 | + $this->couponMock->expects($this->once())->method('load')->with($couponCode, 'code') |
| 112 | + ->willReturnSelf(); |
| 113 | + $this->couponMock->expects($this->exactly(2))->method('getId')->willReturn($couponId); |
| 114 | + $this->couponMock->expects($this->atLeastOnce())->method('getTimesUsed')->willReturn($timesUsed); |
| 115 | + $this->couponMock->expects($this->any())->method('setTimesUsed')->with($setTimesUsed)->willReturnSelf(); |
| 116 | + $this->couponMock->expects($this->any())->method('save')->willReturnSelf(); |
| 117 | + |
| 118 | + $this->updateInfoMock->expects($this->exactly(3))->method('getCustomerId')->willReturn($customerId); |
| 119 | + |
| 120 | + $this->couponUsageMock->expects($this->once()) |
| 121 | + ->method('updateCustomerCouponTimesUsed') |
| 122 | + ->with($customerId, $couponId, $isIncrement) |
| 123 | + ->willReturnSelf(); |
| 124 | + |
| 125 | + $customerRuleMock = $this->getMockBuilder(Customer::class) |
| 126 | + ->disableOriginalConstructor() |
| 127 | + ->onlyMethods(['loadByCustomerRule', 'getId', 'hasData', 'save']) |
| 128 | + ->addMethods(['getTimesUsed', 'setTimesUsed', 'setCustomerId', 'setRuleId']) |
| 129 | + ->getMock(); |
| 130 | + $customerRuleMock->expects($this->once())->method('loadByCustomerRule')->with($customerId, $ruleId) |
| 131 | + ->willReturnSelf(); |
| 132 | + $customerRuleMock->expects($this->once())->method('getId')->willReturn($ruleCustomerId); |
| 133 | + $customerRuleMock->expects($this->any())->method('getTimesUsed')->willReturn($timesUsed); |
| 134 | + $customerRuleMock->expects($this->any())->method('setTimesUsed')->willReturn($setTimesUsed); |
| 135 | + $customerRuleMock->expects($this->any())->method('setCustomerId')->willReturn($customerId); |
| 136 | + $customerRuleMock->expects($this->any())->method('setRuleId')->willReturn($ruleId); |
| 137 | + $customerRuleMock->expects($this->once())->method('hasData')->willReturn(true); |
| 138 | + $customerRuleMock->expects($this->once())->method('save')->willReturnSelf(); |
| 139 | + $this->ruleCustomerFactoryMock->expects($this->once())->method('create')->willReturn($customerRuleMock); |
| 140 | + |
| 141 | + $ruleMock = $this->getMockBuilder(Rule::class) |
| 142 | + ->onlyMethods(['load', 'getId', 'loadCouponCode', 'save']) |
| 143 | + ->addMethods(['getTimesUsed', 'setTimesUsed']) |
| 144 | + ->disableOriginalConstructor() |
| 145 | + ->getMock(); |
| 146 | + $ruleMock->expects($this->once())->method('load')->willReturnSelf(); |
| 147 | + $ruleMock->expects($this->once())->method('getId')->willReturn(true); |
| 148 | + $ruleMock->expects($this->once())->method('loadCouponCode')->willReturnSelf(); |
| 149 | + $ruleMock->expects($this->any())->method('getTimesUsed')->willReturn($timesUsed); |
| 150 | + $ruleMock->expects($this->any())->method('setTimesUsed')->willReturn($setTimesUsed); |
| 151 | + $this->ruleFactoryMock->expects($this->once())->method('create')->willReturn($ruleMock); |
| 152 | + |
| 153 | + $this->processor->process($this->updateInfoMock); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @return array |
| 158 | + */ |
| 159 | + public function dataProvider(): array |
| 160 | + { |
| 161 | + return [ |
| 162 | + [true, 1], |
| 163 | + [true, 0], |
| 164 | + [false, 1], |
| 165 | + [false, 0] |
| 166 | + ]; |
| 167 | + } |
| 168 | +} |
0 commit comments