|
| 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( |
| 78 | + Quote::class, |
| 79 | + ['getCouponCode', 'setCouponCode', 'getId'] |
| 80 | + ); |
| 81 | + |
| 82 | + $this->couponCodeValidation = new CouponCodeValidation( |
| 83 | + $this->codeLimitManagerMock, |
| 84 | + $this->cartRepositoryMock, |
| 85 | + $this->searchCriteriaBuilderMock |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Testing the coupon code that haven't reached the request limit |
| 91 | + */ |
| 92 | + public function testCouponCodeNotReachedTheLimit() |
| 93 | + { |
| 94 | + $couponCode = 'AB123'; |
| 95 | + $this->observerMock->expects($this->once())->method('getData')->with('quote') |
| 96 | + ->willReturn($this->quoteMock); |
| 97 | + $this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode); |
| 98 | + $this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->willReturnSelf(); |
| 99 | + $this->searchCriteriaBuilderMock->expects($this->once())->method('create') |
| 100 | + ->willReturn($this->searchCriteriaMock); |
| 101 | + $this->quoteMock->expects($this->once())->method('getId')->willReturn(123); |
| 102 | + $this->cartRepositoryMock->expects($this->any())->method('getList')->willReturnSelf(); |
| 103 | + $this->cartRepositoryMock->expects($this->any())->method('getItems')->willReturn([]); |
| 104 | + $this->codeLimitManagerMock->expects($this->once())->method('checkRequest')->with($couponCode); |
| 105 | + $this->quoteMock->expects($this->never())->method('setCouponCode')->with(''); |
| 106 | + |
| 107 | + $this->couponCodeValidation->execute($this->observerMock); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Testing with the changed coupon code |
| 112 | + */ |
| 113 | + public function testCouponCodeNotReachedTheLimitWithNewCouponCode() |
| 114 | + { |
| 115 | + $couponCode = 'AB123'; |
| 116 | + $newCouponCode = 'AB234'; |
| 117 | + |
| 118 | + $this->observerMock->expects($this->once())->method('getData')->with('quote') |
| 119 | + ->willReturn($this->quoteMock); |
| 120 | + $this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode); |
| 121 | + $this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->willReturnSelf(); |
| 122 | + $this->searchCriteriaBuilderMock->expects($this->once())->method('create') |
| 123 | + ->willReturn($this->searchCriteriaMock); |
| 124 | + $this->quoteMock->expects($this->once())->method('getId')->willReturn(123); |
| 125 | + $this->cartRepositoryMock->expects($this->any())->method('getList')->willReturnSelf(); |
| 126 | + $this->cartRepositoryMock->expects($this->any())->method('getItems') |
| 127 | + ->willReturn([new DataObject(['coupon_code' => $newCouponCode])]); |
| 128 | + $this->codeLimitManagerMock->expects($this->once())->method('checkRequest')->with($couponCode); |
| 129 | + $this->quoteMock->expects($this->never())->method('setCouponCode')->with(''); |
| 130 | + |
| 131 | + $this->couponCodeValidation->execute($this->observerMock); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Testing the coupon code that reached the request limit |
| 136 | + * |
| 137 | + * @expectedException \Magento\SalesRule\Api\Exception\CodeRequestLimitException |
| 138 | + * @expectedExceptionMessage Too many coupon code requests, please try again later. |
| 139 | + */ |
| 140 | + public function testReachingLimitForCouponCode() |
| 141 | + { |
| 142 | + $couponCode = 'AB123'; |
| 143 | + $this->observerMock->expects($this->once())->method('getData')->with('quote') |
| 144 | + ->willReturn($this->quoteMock); |
| 145 | + $this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode); |
| 146 | + $this->searchCriteriaBuilderMock->expects($this->once())->method('addFilter')->willReturnSelf(); |
| 147 | + $this->searchCriteriaBuilderMock->expects($this->once())->method('create') |
| 148 | + ->willReturn($this->searchCriteriaMock); |
| 149 | + $this->quoteMock->expects($this->once())->method('getId')->willReturn(123); |
| 150 | + $this->cartRepositoryMock->expects($this->any())->method('getList')->willReturnSelf(); |
| 151 | + $this->cartRepositoryMock->expects($this->any())->method('getItems')->willReturn([]); |
| 152 | + $this->codeLimitManagerMock->expects($this->once())->method('checkRequest')->with($couponCode) |
| 153 | + ->willThrowException( |
| 154 | + new CodeRequestLimitException(__('Too many coupon code requests, please try again later.')) |
| 155 | + ); |
| 156 | + $this->quoteMock->expects($this->once())->method('setCouponCode')->with(''); |
| 157 | + |
| 158 | + $this->couponCodeValidation->execute($this->observerMock); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Testing the quote that doesn't have a coupon code set |
| 163 | + */ |
| 164 | + public function testQuoteWithNoCouponCode() |
| 165 | + { |
| 166 | + $couponCode = null; |
| 167 | + $this->observerMock->expects($this->once())->method('getData')->with('quote') |
| 168 | + ->willReturn($this->quoteMock); |
| 169 | + $this->quoteMock->expects($this->once())->method('getCouponCode')->willReturn($couponCode); |
| 170 | + $this->quoteMock->expects($this->never())->method('getId')->willReturn(123); |
| 171 | + $this->quoteMock->expects($this->never())->method('setCouponCode')->with(''); |
| 172 | + |
| 173 | + $this->couponCodeValidation->execute($this->observerMock); |
| 174 | + } |
| 175 | +} |
0 commit comments