Skip to content

Commit 4b70753

Browse files
MC-42288: Coupon codes for existing cart price rule are not generated
- Update unit test scenarios
1 parent 81164e3 commit 4b70753

File tree

1 file changed

+118
-2
lines changed
  • app/code/Magento/SalesRule/Test/Unit/Controller/Adminhtml/Promo/Quote

1 file changed

+118
-2
lines changed

app/code/Magento/SalesRule/Test/Unit/Controller/Adminhtml/Promo/Quote/GenerateTest.php

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ protected function setUp(): void
137137
}
138138

139139
/**
140-
* testExecute
140+
* @covers \Magento\SalesRule\Controller\Adminhtml\Promo\Quote::execute
141141
*/
142-
public function testExecute()
142+
public function testExecuteWithCouponTypeAuto()
143143
{
144144
$helperData = $this->getMockBuilder(Data::class)
145145
->disableOriginalConstructor()
@@ -207,4 +207,120 @@ public function testExecute()
207207
->willReturn(__('%1 coupon(s) have been generated.', 2));
208208
$this->model->execute();
209209
}
210+
211+
/**
212+
* @covers \Magento\SalesRule\Controller\Adminhtml\Promo\Quote::execute
213+
*/
214+
public function testExecuteWithAutoGenerationEnabled()
215+
{
216+
$helperData = $this->getMockBuilder(Data::class)
217+
->disableOriginalConstructor()
218+
->getMock();
219+
$this->objectManagerMock->expects($this->any())
220+
->method('get')
221+
->with(Data::class)
222+
->willReturn($helperData);
223+
$requestData = [
224+
'qty' => 2,
225+
'length' => 10,
226+
'rule_id' => 1
227+
];
228+
$this->requestMock->expects($this->once())
229+
->method('isAjax')
230+
->willReturn(true);
231+
$ruleMock = $this->getMockBuilder(Rule::class)
232+
->addMethods(['getUseAutoGeneration'])
233+
->onlyMethods(['getId'])
234+
->disableOriginalConstructor()
235+
->getMock();
236+
$this->registryMock->expects($this->once())
237+
->method('registry')
238+
->willReturn($ruleMock);
239+
$ruleMock->expects($this->once())
240+
->method('getId')
241+
->willReturn(1);
242+
$ruleMock->expects($this->once())
243+
->method('getUseAutoGeneration')
244+
->willReturn(1);
245+
$this->requestMock->expects($this->once())
246+
->method('getParams')
247+
->willReturn($requestData);
248+
$requestData['quantity'] = isset($requestData['qty']) ? $requestData['qty'] : null;
249+
$this->couponGenerationSpec->expects($this->once())
250+
->method('create')
251+
->with(['data' => $requestData])
252+
->willReturn(['some_data', 'some_data_2']);
253+
$this->messageManager->expects($this->once())
254+
->method('addSuccessMessage');
255+
$this->responseMock->expects($this->once())
256+
->method('representJson')
257+
->with();
258+
$helperData->expects($this->once())
259+
->method('jsonEncode')
260+
->with([
261+
'messages' => __('%1 coupon(s) have been generated.', 2)
262+
]);
263+
$layout = $this->getMockBuilder(Layout::class)
264+
->disableOriginalConstructor()
265+
->getMock();
266+
$this->view->expects($this->any())
267+
->method('getLayout')
268+
->willReturn($layout);
269+
$messageBlock = $this->getMockBuilder(Messages::class)
270+
->disableOriginalConstructor()
271+
->getMock();
272+
$layout->expects($this->once())
273+
->method('initMessages');
274+
$layout->expects($this->once())
275+
->method('getMessagesBlock')
276+
->willReturn($messageBlock);
277+
$messageBlock->expects($this->once())
278+
->method('getGroupedHtml')
279+
->willReturn(__('%1 coupon(s) have been generated.', 2));
280+
$this->model->execute();
281+
}
282+
283+
/**
284+
* @covers \Magento\SalesRule\Controller\Adminhtml\Promo\Quote::execute
285+
*/
286+
public function testExecuteWithCouponTypeNotAutoAndAutoGenerationNotEnabled()
287+
{
288+
$helperData = $this->getMockBuilder(Data::class)
289+
->disableOriginalConstructor()
290+
->getMock();
291+
$this->objectManagerMock->expects($this->any())
292+
->method('get')
293+
->with(Data::class)
294+
->willReturn($helperData);
295+
$this->requestMock->expects($this->once())
296+
->method('isAjax')
297+
->willReturn(true);
298+
$ruleMock = $this->getMockBuilder(Rule::class)
299+
->addMethods(['getUseAutoGeneration', 'getCouponType'])
300+
->onlyMethods(['getId'])
301+
->disableOriginalConstructor()
302+
->getMock();
303+
$this->registryMock->expects($this->once())
304+
->method('registry')
305+
->willReturn($ruleMock);
306+
$ruleMock->expects($this->once())
307+
->method('getId')
308+
->willReturn(1);
309+
$ruleMock->expects($this->once())
310+
->method('getCouponType')
311+
->willReturn(\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
312+
$ruleMock->expects($this->once())
313+
->method('getUseAutoGeneration')
314+
->willReturn(0);
315+
$this->responseMock->expects($this->once())
316+
->method('representJson')
317+
->with();
318+
$helperData->expects($this->once())
319+
->method('jsonEncode')
320+
->with([
321+
'error' =>
322+
__('Rule is not saved with auto generate option enabled. Please save the rule and try again.')
323+
]);
324+
$this->model->execute();
325+
}
210326
}

0 commit comments

Comments
 (0)