Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessDataTest.php
Go to the documentation of this file.
1 <?php
8 
10 
12 
19 class ProcessDataTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $processData;
25 
29  protected $objectManager;
30 
34  protected $request;
35 
39  protected $session;
40 
44  protected $eventManager;
45 
49  protected $messageManager;
50 
54  protected $escaper;
55 
59  protected $resultForward;
60 
65 
69  protected function setUp()
70  {
71  $objectManagerHelper = new ObjectManagerHelper($this);
72  $context = $this->createMock(\Magento\Backend\App\Action\Context::class);
73 
74  $this->request = $this->getMockForAbstractClass(
75  \Magento\Framework\App\RequestInterface::class,
76  [],
77  '',
78  false,
79  true,
80  true,
81  [
82  'getParam',
83  'getPost',
84  'getPostValue',
85  'get',
86  'has',
87  'setModuleName',
88  'setActionName',
89  'initForward',
90  'setDispatched',
91  'getModuleName',
92  'getActionName',
93  'getCookie'
94  ]
95  );
96  $response = $this->getMockForAbstractClass(
97  \Magento\Framework\App\ResponseInterface::class,
98  [],
99  '',
100  false,
101  true,
102  true,
103  []
104  );
105  $context->expects($this->any())->method('getResponse')->willReturn($response);
106  $context->expects($this->any())->method('getRequest')->willReturn($this->request);
107 
108  $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
109  $context->expects($this->any())->method('getMessageManager')->willReturn($this->messageManager);
110 
111  $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
112  $context->expects($this->any())->method('getEventManager')->willReturn($this->eventManager);
113 
114  $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
115  $context->expects($this->any())->method('getObjectManager')->willReturn($this->objectManager);
116 
117  $this->session = $this->createMock(\Magento\Backend\Model\Session\Quote::class);
118  $context->expects($this->any())->method('getSession')->willReturn($this->session);
119  $this->escaper = $this->createPartialMock(\Magento\Framework\Escaper::class, ['escapeHtml']);
120 
121  $this->resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
122  ->disableOriginalConstructor()
123  ->getMock();
124  $this->resultForwardFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\ForwardFactory::class)
125  ->disableOriginalConstructor()
126  ->setMethods(['create'])
127  ->getMock();
128 
129  $this->resultForwardFactory->expects($this->once())
130  ->method('create')
131  ->willReturn($this->resultForward);
132 
133  $this->processData = $objectManagerHelper->getObject(
134  \Magento\Sales\Controller\Adminhtml\Order\Create\ProcessData::class,
135  [
136  'context' => $context,
137  'escaper' => $this->escaper,
138  'resultForwardFactory' => $this->resultForwardFactory,
139  ]
140  );
141  }
142 
150  public function testExecute($noDiscount, $couponCode, $errorMessage, $actualCouponCode)
151  {
152  $quote = $this->createPartialMock(
153  \Magento\Quote\Model\Quote::class,
154  ['getCouponCode', 'isVirtual', 'getAllItems']
155  );
156  $create = $this->createMock(\Magento\Sales\Model\AdminOrder\Create::class);
157 
158  $paramReturnMap = [
159  ['customer_id', null, null],
160  ['store_id', null, null],
161  ['currency_id', null, null]
162  ];
163  $this->request->expects($this->atLeastOnce())->method('getParam')->willReturnMap($paramReturnMap);
164 
165  $objectManagerParamMap = [
166  [\Magento\Sales\Model\AdminOrder\Create::class, $create],
167  [\Magento\Backend\Model\Session\Quote::class, $this->session]
168  ];
169  $this->objectManager->expects($this->atLeastOnce())->method('get')->willReturnMap($objectManagerParamMap);
170 
171  $this->eventManager->expects($this->any())->method('dispatch');
172 
173  $data = ['coupon' => ['code' => $couponCode]];
174  $postReturnMap = [
175  ['order', $data],
176  ['reset_shipping', false],
177  ['collect_shipping_rates', false],
178  ['sidebar', false],
179  ['add_product', false],
180  ['', false],
181  ['update_items', false],
182  ['remove_item', 1],
183  ['from', 2],
184  ['move_item', 1],
185  ['to', 2],
186  ['qty', 3],
187  ['payment', false],
188  [null, 'request'],
189  ['payment', false],
190  ['giftmessage', false],
191  ['add_products', false],
192  ['update_items', false],
193 
194  ];
195  $this->request->expects($this->atLeastOnce())->method('getPost')->willReturnMap($postReturnMap);
196 
197  $create->expects($this->once())->method('importPostData')->willReturnSelf();
198  $create->expects($this->once())->method('initRuleData')->willReturnSelf();
199  $create->expects($this->any())->method('getQuote')->willReturn($quote);
200 
201  $address = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
202  $create->expects($this->once())->method('getBillingAddress')->willReturn($address);
203 
204  $quote->expects($this->any())->method('isVirtual')->willReturn(true);
205 
206  $this->request->expects($this->once())->method('has')->with('item')->willReturn(false);
207 
208  $create->expects($this->once())->method('saveQuote')->willReturnSelf();
209 
210  $this->session->expects($this->any())->method('getQuote')->willReturn($quote);
211  $item = $this->getMockForAbstractClass(
212  \Magento\Eav\Model\Entity\Collection\AbstractCollection::class,
213  [],
214  '',
215  false,
216  true,
217  true,
218  ['getNoDiscount']
219  );
220  $quote->expects($this->any())->method('getAllItems')->willReturn([$item]);
221  $item->expects($this->any())->method('getNoDiscount')->willReturn($noDiscount);
222  if (!$noDiscount) {
223  $quote->expects($this->once())->method('getCouponCode')->willReturn($actualCouponCode);
224  }
225 
226  $errorMessageManager = __(
227  $errorMessage,
229  );
230  $this->escaper->expects($this->once())->method('escapeHtml')->with($couponCode)->willReturn($couponCode);
231 
232  $this->messageManager
233  ->expects($this->once())
234  ->method('addErrorMessage')
235  ->with($errorMessageManager)
236  ->willReturnSelf();
237 
238  $this->resultForward->expects($this->once())
239  ->method('forward')
240  ->with('index')
241  ->willReturnSelf();
242  $this->assertInstanceOf(\Magento\Backend\Model\View\Result\Forward::class, $this->processData->execute());
243  }
244 
248  public function isApplyDiscountDataProvider()
249  {
250  return [
251  [true, '123', '"%1" coupon code was not applied. Do not apply discount is selected for item(s)', null],
252  [false, '123', 'The "%1" coupon code isn\'t valid. Verify the code and try again.', '132'],
253  ];
254  }
255 }
$response
Definition: 404.php:11
$quote
__()
Definition: __.php:13
$address
Definition: customer.php:38
testExecute($noDiscount, $couponCode, $errorMessage, $actualCouponCode)