Skip to content

Commit d3e9fd4

Browse files
committed
Covering the CouponCodeValidation class by Unit Tests
1 parent 54244f7 commit d3e9fd4

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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\Observer;
9+
10+
use Magento\Framework\Api\SearchCriteria;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Framework\DataObject;
13+
use Magento\Framework\Event\Observer;
14+
use Magento\Quote\Api\CartRepositoryInterface;
15+
use Magento\Quote\Model\Quote;
16+
use Magento\SalesRule\Api\Exception\CodeRequestLimitException;
17+
use Magento\SalesRule\Model\Spi\CodeLimitManagerInterface;
18+
use Magento\SalesRule\Observer\CouponCodeValidation;
19+
use PHPUnit\Framework\TestCase;
20+
use PHPUnit_Framework_MockObject_MockObject;
21+
22+
/**
23+
* Class CouponCodeValidationTest
24+
*/
25+
class CouponCodeValidationTest extends TestCase
26+
{
27+
/**
28+
* @var CouponCodeValidation
29+
*/
30+
private $couponCodeValidation;
31+
32+
/**
33+
* @var PHPUnit_Framework_MockObject_MockObject|CodeLimitManagerInterface
34+
*/
35+
private $codeLimitManagerMock;
36+
37+
/**
38+
* @var PHPUnit_Framework_MockObject_MockObject|CartRepositoryInterface
39+
*/
40+
private $cartRepositoryMock;
41+
42+
/**
43+
* @var PHPUnit_Framework_MockObject_MockObject|SearchCriteriaBuilder
44+
*/
45+
private $searchCriteriaBuilderMock;
46+
47+
/**
48+
* @var PHPUnit_Framework_MockObject_MockObject|Observer
49+
*/
50+
private $observerMock;
51+
52+
/**
53+
* @var PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
private $searchCriteriaMock;
56+
57+
/**
58+
* @var PHPUnit_Framework_MockObject_MockObject
59+
*/
60+
private $quoteMock;
61+
62+
/**
63+
* Set Up
64+
*/
65+
protected function setUp()
66+
{
67+
$this->codeLimitManagerMock = $this->createMock(CodeLimitManagerInterface::class);
68+
$this->observerMock = $this->createMock(Observer::class);
69+
$this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class)
70+
->disableOriginalConstructor()->getMockForAbstractClass();
71+
$this->cartRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
72+
->setMethods(['getItems'])
73+
->disableOriginalConstructor()->getMockForAbstractClass();
74+
$this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)
75+
->setMethods(['addFilter', 'create'])
76+
->disableOriginalConstructor()->getMockForAbstractClass();
77+
$this->quoteMock = $this->createPartialMock(Quote::class, [
78+
'getCouponCode', 'setCouponCode', 'getId'
79+
]);
80+
81+
$this->couponCodeValidation = new CouponCodeValidation(
82+
$this->codeLimitManagerMock,
83+
$this->cartRepositoryMock,
84+
$this->searchCriteriaBuilderMock
85+
);
86+
}
87+
88+
/**
89+
* Testing the coupon code that haven't reached the request limit
90+
*/
91+
public function testCouponCodeNotReachedTheLimit()
92+
{
93+
$couponCode = 'AB123';
94+
$this->observerMock->expects($this->once())->method('getData')->with('quote')
95+
->willReturn($this->quoteMock);
96+
$this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode);
97+
$this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->willReturnSelf();
98+
$this->searchCriteriaBuilderMock->expects($this->once())->method('create')
99+
->willReturn($this->searchCriteriaMock);
100+
$this->quoteMock->expects($this->once())->method('getId')->willReturn(123);
101+
$this->cartRepositoryMock->expects($this->any())->method('getList')->willReturnSelf();
102+
$this->cartRepositoryMock->expects($this->any())->method('getItems')->willReturn([]);
103+
$this->codeLimitManagerMock->expects($this->once())->method('checkRequest')->with($couponCode);
104+
$this->quoteMock->expects($this->never())->method('setCouponCode')->with('');
105+
106+
$this->couponCodeValidation->execute($this->observerMock);
107+
}
108+
109+
/**
110+
* Testing with the changed coupon code
111+
*/
112+
public function testCouponCodeNotReachedTheLimitWithNewCouponCode()
113+
{
114+
$couponCode = 'AB123';
115+
$newCouponCode = 'AB234';
116+
117+
$this->observerMock->expects($this->once())->method('getData')->with('quote')
118+
->willReturn($this->quoteMock);
119+
$this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode);
120+
$this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->willReturnSelf();
121+
$this->searchCriteriaBuilderMock->expects($this->once())->method('create')
122+
->willReturn($this->searchCriteriaMock);
123+
$this->quoteMock->expects($this->once())->method('getId')->willReturn(123);
124+
$this->cartRepositoryMock->expects($this->any())->method('getList')->willReturnSelf();
125+
$this->cartRepositoryMock->expects($this->any())->method('getItems')
126+
->willReturn([new DataObject(['coupon_code' => $newCouponCode])]);
127+
$this->codeLimitManagerMock->expects($this->once())->method('checkRequest')->with($couponCode);
128+
$this->quoteMock->expects($this->never())->method('setCouponCode')->with('');
129+
130+
$this->couponCodeValidation->execute($this->observerMock);
131+
}
132+
133+
/**
134+
* Testing the coupon code that reached the request limit
135+
*
136+
* @expectedException \Magento\SalesRule\Api\Exception\CodeRequestLimitException
137+
* @expectedExceptionMessage Too many coupon code requests, please try again later.
138+
*/
139+
public function testReachingLimitForCouponCode()
140+
{
141+
$couponCode = 'AB123';
142+
$this->observerMock->expects($this->once())->method('getData')->with('quote')
143+
->willReturn($this->quoteMock);
144+
$this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode);
145+
$this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->willReturnSelf();
146+
$this->searchCriteriaBuilderMock->expects($this->once())->method('create')
147+
->willReturn($this->searchCriteriaMock);
148+
$this->quoteMock->expects($this->once())->method('getId')->willReturn(123);
149+
$this->cartRepositoryMock->expects($this->any())->method('getList')->willReturnSelf();
150+
$this->cartRepositoryMock->expects($this->any())->method('getItems')->willReturn([]);
151+
$this->codeLimitManagerMock->expects($this->once())->method('checkRequest')->with($couponCode)
152+
->willThrowException(
153+
new CodeRequestLimitException(__('Too many coupon code requests, please try again later.'))
154+
);
155+
$this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
156+
157+
$this->couponCodeValidation->execute($this->observerMock);
158+
}
159+
160+
/**
161+
* Testing the quote that doesn't have a coupon code set
162+
*/
163+
public function testQuoteWithNoCouponCode()
164+
{
165+
$couponCode = null;
166+
$this->observerMock->expects($this->once())->method('getData')->with('quote')
167+
->willReturn($this->quoteMock);
168+
$this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode);
169+
$this->quoteMock->expects($this->never())->method('getId')->willReturn(123);
170+
$this->quoteMock->expects($this->never())->method('setCouponCode')->with('');
171+
172+
$this->couponCodeValidation->execute($this->observerMock);
173+
}
174+
}

0 commit comments

Comments
 (0)