Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponManagementTest.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class CouponManagementTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $couponManagement;
18 
23 
27  protected $quoteMock;
28 
32  protected $storeMock;
33 
37  protected $quoteAddressMock;
38 
39  protected function setUp()
40  {
41  $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
42  $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
43  $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
44  'getItemsCount',
45  'setCouponCode',
46  'collectTotals',
47  'save',
48  'getShippingAddress',
49  'getCouponCode',
50  'getStoreId',
51  '__wakeup'
52  ]);
53  $this->quoteAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
54  'setCollectShippingRates',
55  '__wakeup'
56  ]);
57  $this->couponManagement = new CouponManagement(
58  $this->quoteRepositoryMock
59  );
60  }
61 
62  public function testGetCoupon()
63  {
64  $cartId = 11;
65  $couponCode = 'test_coupon_code';
66 
67  $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getCouponCode', '__wakeup']);
68  $quoteMock->expects($this->any())->method('getCouponCode')->will($this->returnValue($couponCode));
69 
70  $this->quoteRepositoryMock->expects($this->once())
71  ->method('getActive')
72  ->with($cartId)
73  ->will($this->returnValue($quoteMock));
74 
75  $this->assertEquals($couponCode, $this->couponManagement->get($cartId));
76  }
77 
83  {
84  $cartId = 33;
85 
86  $this->quoteRepositoryMock->expects($this->once())
87  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
88  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(0));
89 
90  $this->couponManagement->set($cartId, 'coupon_code');
91  }
92 
98  {
99  $cartId = 33;
100  $couponCode = '153a-ABC';
101 
102  $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
103  $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
104 
105  $this->quoteRepositoryMock->expects($this->once())
106  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
107  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
108  $this->quoteMock->expects($this->once())
109  ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
110  $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
111  $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
112  $exceptionMessage = "The coupon code couldn't be applied. Verify the coupon code and try again.";
113  $exception = new \Magento\Framework\Exception\CouldNotDeleteException(__($exceptionMessage));
114  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
115  $this->quoteRepositoryMock->expects($this->once())
116  ->method('save')
117  ->with($this->quoteMock)
118  ->willThrowException($exception);
119 
120  $this->couponManagement->set($cartId, $couponCode);
121  }
122 
128  {
129  $cartId = 33;
130  $couponCode = '153a-ABC';
131 
132  $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
133  $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
134 
135  $this->quoteRepositoryMock->expects($this->once())
136  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
137  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
138  $this->quoteMock->expects($this->once())
139  ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
140  $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
141  $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
142  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
143  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
144  $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue('invalidCoupon'));
145 
146  $this->couponManagement->set($cartId, $couponCode);
147  }
148 
149  public function testSet()
150  {
151  $cartId = 33;
152  $couponCode = '153a-ABC';
153 
154  $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
155  $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
156 
157  $this->quoteRepositoryMock->expects($this->once())
158  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
159  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
160  $this->quoteMock->expects($this->once())
161  ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
162  $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
163  $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
164  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
165  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
166  $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue($couponCode));
167 
168  $this->assertTrue($this->couponManagement->set($cartId, $couponCode));
169  }
170 
176  {
177  $cartId = 65;
178 
179  $this->quoteRepositoryMock->expects($this->once())
180  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
181  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(0));
182  $this->quoteMock->expects($this->never())->method('getShippingAddress');
183 
184  $this->couponManagement->remove($cartId);
185  }
186 
192  {
193  $cartId = 65;
194 
195  $this->quoteRepositoryMock->expects($this->once())
196  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
197  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
198  $this->quoteMock->expects($this->once())
199  ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
200  $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
201  $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
202  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
203  $exceptionMessage = "The coupon code couldn't be deleted. Verify the coupon code and try again.";
204  $exception = new \Magento\Framework\Exception\CouldNotSaveException(__($exceptionMessage));
205  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
206  $this->quoteRepositoryMock->expects($this->once())
207  ->method('save')
208  ->with($this->quoteMock)
209  ->willThrowException($exception);
210 
211  $this->couponManagement->remove($cartId);
212  }
213 
219  {
220  $cartId = 65;
221 
222  $this->quoteRepositoryMock->expects($this->once())
223  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
224  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
225  $this->quoteMock->expects($this->once())
226  ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
227  $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
228  $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
229  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
230  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
231  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
232  $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue('123_ABC'));
233 
234  $this->couponManagement->remove($cartId);
235  }
236 
237  public function testDelete()
238  {
239  $cartId = 65;
240 
241  $this->quoteRepositoryMock->expects($this->once())
242  ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
243  $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
244  $this->quoteMock->expects($this->once())
245  ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
246  $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
247  $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
248  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
249  $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
250  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
251  $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue(''));
252 
253  $this->assertTrue($this->couponManagement->remove($cartId));
254  }
255 }
__()
Definition: __.php:13
$cartId
Definition: quote.php:22