|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Sales\Controller\Adminhtml\Order\Create; |
| 8 | + |
| 9 | +use Magento\TestFramework\Helper\ObjectManager as ObjectManagerHelper; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class ProcessDataTest |
| 13 | + * |
| 14 | + */ |
| 15 | +class ProcessDataTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var ProcessData |
| 19 | + */ |
| 20 | + protected $processData; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 24 | + */ |
| 25 | + protected $objectManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + protected $request; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var \Magento\Backend\Model\Session\Quote|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + protected $session; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + protected $eventManager; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + protected $actionFlag; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 49 | + */ |
| 50 | + protected $messageManager; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject |
| 54 | + */ |
| 55 | + protected $escaper; |
| 56 | + |
| 57 | + protected function setUp() |
| 58 | + { |
| 59 | + $objectManagerHelper = new ObjectManagerHelper($this); |
| 60 | + $context = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false); |
| 61 | + |
| 62 | + $this->request = $this->getMockForAbstractClass( |
| 63 | + 'Magento\Framework\App\RequestInterface', |
| 64 | + [], |
| 65 | + '', |
| 66 | + false, |
| 67 | + true, |
| 68 | + true, |
| 69 | + [ |
| 70 | + 'getParam', |
| 71 | + 'getPost', |
| 72 | + 'get', |
| 73 | + 'has', |
| 74 | + 'setModuleName', |
| 75 | + 'setActionName', |
| 76 | + 'initForward', |
| 77 | + 'setDispatched', |
| 78 | + 'getModuleName', |
| 79 | + 'getActionName', |
| 80 | + 'getCookie' |
| 81 | + ] |
| 82 | + ); |
| 83 | + $response = $this->getMockForAbstractClass( |
| 84 | + 'Magento\Framework\App\ResponseInterface', |
| 85 | + [], |
| 86 | + '', |
| 87 | + false, |
| 88 | + true, |
| 89 | + true, |
| 90 | + [] |
| 91 | + ); |
| 92 | + $context->expects($this->any())->method('getResponse')->willReturn($response); |
| 93 | + $context->expects($this->any())->method('getRequest')->willReturn($this->request); |
| 94 | + |
| 95 | + $this->actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', [], [], '', false); |
| 96 | + $context->expects($this->any())->method('getActionFlag')->willReturn($this->actionFlag); |
| 97 | + |
| 98 | + $this->messageManager = $this->getMock('Magento\Framework\Message\ManagerInterface', [], [], '', false); |
| 99 | + $context->expects($this->any())->method('getMessageManager')->willReturn($this->messageManager); |
| 100 | + |
| 101 | + $this->eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false); |
| 102 | + $context->expects($this->any())->method('getEventManager')->willReturn($this->eventManager); |
| 103 | + |
| 104 | + $this->objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface'); |
| 105 | + $context->expects($this->any())->method('getObjectManager')->willReturn($this->objectManager); |
| 106 | + |
| 107 | + $this->session = $this->getMock( |
| 108 | + 'Magento\Backend\Model\Session\Quote', |
| 109 | + ['setIsUrlNotice', 'getQuote'], |
| 110 | + [], |
| 111 | + '', |
| 112 | + false |
| 113 | + ); |
| 114 | + $context->expects($this->any())->method('getSession')->willReturn($this->session); |
| 115 | + $this->escaper = $this->getMock('Magento\Framework\Escaper', ['escapeHtml'], [], '', false); |
| 116 | + |
| 117 | + $this->processData = $objectManagerHelper->getObject( |
| 118 | + 'Magento\Sales\Controller\Adminhtml\Order\Create\ProcessData', |
| 119 | + [ |
| 120 | + 'context' => $context, |
| 121 | + 'escaper' => $this->escaper |
| 122 | + ] |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param bool $noDiscount |
| 128 | + * @param string $couponCode |
| 129 | + * @param string $errorMessage |
| 130 | + * @param string $actualCouponCode |
| 131 | + * @dataProvider isApplyDiscountDataProvider |
| 132 | + */ |
| 133 | + public function testExecute($noDiscount, $couponCode, $errorMessage, $actualCouponCode) |
| 134 | + { |
| 135 | + $quote = $this->getMock( |
| 136 | + 'Magento\Sales\Model\Quote', |
| 137 | + ['getCouponCode', 'isVirtual', 'getAllItems'], |
| 138 | + [], |
| 139 | + '', |
| 140 | + false |
| 141 | + ); |
| 142 | + $create = $this->getMock('Magento\Sales\Model\AdminOrder\Create', [], [], '', false); |
| 143 | + |
| 144 | + $paramReturnMap = [ |
| 145 | + ['customer_id', null, null], |
| 146 | + ['store_id', null, null], |
| 147 | + ['currency_id', null, null] |
| 148 | + ]; |
| 149 | + $this->request->expects($this->atLeastOnce())->method('getParam')->willReturnMap($paramReturnMap); |
| 150 | + |
| 151 | + $objectManagerParamMap = [ |
| 152 | + ['Magento\Sales\Model\AdminOrder\Create', $create], |
| 153 | + ['Magento\Backend\Model\Session\Quote', $this->session] |
| 154 | + ]; |
| 155 | + $this->objectManager->expects($this->atLeastOnce())->method('get')->willReturnMap($objectManagerParamMap); |
| 156 | + |
| 157 | + $this->eventManager->expects($this->any())->method('dispatch'); |
| 158 | + |
| 159 | + $data = ['coupon' => ['code' => $couponCode]]; |
| 160 | + $postReturnMap = [ |
| 161 | + ['order', $data], |
| 162 | + ['reset_shipping', false], |
| 163 | + ['collect_shipping_rates', false], |
| 164 | + ['sidebar', false], |
| 165 | + ['add_product', false], |
| 166 | + ['', false], |
| 167 | + ['update_items', false], |
| 168 | + ['remove_item', 1], |
| 169 | + ['from', 2], |
| 170 | + ['move_item', 1], |
| 171 | + ['to', 2], |
| 172 | + ['qty', 3], |
| 173 | + ['payment', false], |
| 174 | + [null, 'request'], |
| 175 | + ['payment', false], |
| 176 | + ['giftmessage', false], |
| 177 | + ['add_products', false], |
| 178 | + ['update_items', false], |
| 179 | + |
| 180 | + ]; |
| 181 | + $this->request->expects($this->atLeastOnce())->method('getPost')->willReturnMap($postReturnMap); |
| 182 | + |
| 183 | + $create->expects($this->once())->method('importPostData')->willReturnSelf(); |
| 184 | + $create->expects($this->once())->method('initRuleData')->willReturnSelf(); |
| 185 | + $create->expects($this->any())->method('getQuote')->willReturn($quote); |
| 186 | + |
| 187 | + $address = $this->getMock('Magento\Sales\Model\Quote\Address', [], [], '', false); |
| 188 | + $create->expects($this->once())->method('getBillingAddress')->willReturn($address); |
| 189 | + |
| 190 | + $quote->expects($this->any())->method('isVirtual')->willReturn(true); |
| 191 | + |
| 192 | + $this->request->expects($this->once())->method('has')->with('item')->willReturn(false); |
| 193 | + |
| 194 | + $create->expects($this->once())->method('saveQuote')->willReturnSelf(); |
| 195 | + |
| 196 | + $this->session->expects($this->any())->method('getQuote')->willReturn($quote); |
| 197 | + $item = $this->getMockForAbstractClass( |
| 198 | + 'Magento\Eav\Model\Entity\Collection\AbstractCollection', |
| 199 | + [], |
| 200 | + '', |
| 201 | + false, |
| 202 | + true, |
| 203 | + true, |
| 204 | + ['getNoDiscount'] |
| 205 | + ); |
| 206 | + $quote->expects($this->any())->method('getAllItems')->willReturn([$item]); |
| 207 | + $item->expects($this->any())->method('getNoDiscount')->willReturn($noDiscount); |
| 208 | + if (!$noDiscount) { |
| 209 | + $quote->expects($this->once())->method('getCouponCode')->willReturn($actualCouponCode); |
| 210 | + } |
| 211 | + |
| 212 | + $errorMessageManager = __( |
| 213 | + $errorMessage, |
| 214 | + $couponCode |
| 215 | + ); |
| 216 | + $this->escaper->expects($this->once())->method('escapeHtml')->with($couponCode)->willReturn($couponCode); |
| 217 | + |
| 218 | + $this->messageManager->expects($this->once())->method('addError')->with($errorMessageManager)->willReturnSelf(); |
| 219 | + |
| 220 | + $this->actionFlag->expects($this->once())->method('get')->willReturn(false); |
| 221 | + $this->session->expects($this->once())->method('setIsUrlNotice')->with(false)->willReturn(false); |
| 222 | + $this->request->expects($this->once())->method('initForward')->willReturnSelf(); |
| 223 | + $this->request->expects($this->once())->method('setActionName')->willReturnSelf(); |
| 224 | + $this->request->expects($this->once())->method('setDispatched')->willReturnSelf(); |
| 225 | + $this->assertNull($this->processData->execute()); |
| 226 | + } |
| 227 | + |
| 228 | + public function isApplyDiscountDataProvider() |
| 229 | + { |
| 230 | + return [ |
| 231 | + [true, '123', '"%1" coupon code was not applied. Do not apply discount is selected for item(s)', null], |
| 232 | + [false, '123', '"%1" coupon code is not valid.', '132'], |
| 233 | + ]; |
| 234 | + } |
| 235 | +} |
0 commit comments