|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Checkout\Test\Unit\Controller\Onepage; |
| 7 | + |
| 8 | +use Magento\Checkout\Controller\Onepage\SaveOrder; |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class SaveOrderTest |
| 13 | + */ |
| 14 | +class SaveOrderTest extends \PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var SaveOrder |
| 18 | + */ |
| 19 | + protected $controller; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Framework\Data\Form\FormKey\Validator|\PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + protected $formKeyValidatorMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + protected $contextMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + protected $requestMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + protected $responseMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + protected $objectManagerMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + protected $resultRedirectFactoryMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + protected $resultRawFactoryMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + protected $resultJsonFactoryMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 63 | + */ |
| 64 | + protected $eventManagerMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * Set up |
| 68 | + * |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + protected function setUp() |
| 72 | + { |
| 73 | + $helper = new ObjectManager($this); |
| 74 | + |
| 75 | + $contextMock = $this->getMock( |
| 76 | + 'Magento\Framework\App\Action\Context', |
| 77 | + [], |
| 78 | + [], |
| 79 | + '', |
| 80 | + false |
| 81 | + ); |
| 82 | + |
| 83 | + $this->requestMock = $this->getMockForAbstractClass( |
| 84 | + 'Magento\Framework\App\RequestInterface', |
| 85 | + [], |
| 86 | + '', |
| 87 | + false, |
| 88 | + true, |
| 89 | + true, |
| 90 | + ['getPost'] |
| 91 | + ); |
| 92 | + $this->responseMock = $this->getMockForAbstractClass( |
| 93 | + 'Magento\Framework\App\ResponseInterface', |
| 94 | + [], |
| 95 | + '', |
| 96 | + false, |
| 97 | + true, |
| 98 | + true, |
| 99 | + [] |
| 100 | + ); |
| 101 | + $this->objectManagerMock = $this->getMockForAbstractClass( |
| 102 | + 'Magento\Framework\ObjectManagerInterface', |
| 103 | + [], |
| 104 | + '', |
| 105 | + false, |
| 106 | + true, |
| 107 | + true, |
| 108 | + [] |
| 109 | + ); |
| 110 | + $this->formKeyValidatorMock = $this->getMock( |
| 111 | + 'Magento\Framework\Data\Form\FormKey\Validator', |
| 112 | + [], |
| 113 | + [], |
| 114 | + '', |
| 115 | + false |
| 116 | + ); |
| 117 | + $this->resultRedirectFactoryMock = $this->getMock( |
| 118 | + 'Magento\Framework\Controller\Result\RedirectFactory', |
| 119 | + [], |
| 120 | + [], |
| 121 | + '', |
| 122 | + false |
| 123 | + ); |
| 124 | + $this->resultRawFactoryMock = $this->getMock( |
| 125 | + 'Magento\Framework\Controller\Result\RawFactory', |
| 126 | + [], |
| 127 | + [], |
| 128 | + '', |
| 129 | + false |
| 130 | + ); |
| 131 | + $this->resultJsonFactoryMock = $this->getMock( |
| 132 | + 'Magento\Framework\Controller\Result\JsonFactory', |
| 133 | + [], |
| 134 | + [], |
| 135 | + '', |
| 136 | + false |
| 137 | + ); |
| 138 | + $this->eventManagerMock = $this->getMockForAbstractClass( |
| 139 | + 'Magento\Framework\Event\ManagerInterface', |
| 140 | + [], |
| 141 | + '', |
| 142 | + false, |
| 143 | + true, |
| 144 | + true, |
| 145 | + [] |
| 146 | + ); |
| 147 | + |
| 148 | + $contextMock->expects($this->once()) |
| 149 | + ->method('getRequest') |
| 150 | + ->willReturn($this->requestMock); |
| 151 | + $contextMock->expects($this->once()) |
| 152 | + ->method('getResponse') |
| 153 | + ->willReturn($this->responseMock); |
| 154 | + $contextMock->expects($this->once()) |
| 155 | + ->method('getObjectManager') |
| 156 | + ->willReturn($this->objectManagerMock); |
| 157 | + $contextMock->expects($this->once()) |
| 158 | + ->method('getResultRedirectFactory') |
| 159 | + ->willReturn($this->resultRedirectFactoryMock); |
| 160 | + $contextMock->expects($this->once()) |
| 161 | + ->method('getEventManager') |
| 162 | + ->willReturn($this->eventManagerMock); |
| 163 | + |
| 164 | + $this->controller = $helper->getObject( |
| 165 | + 'Magento\Checkout\Controller\Onepage\SaveOrder', |
| 166 | + [ |
| 167 | + 'context' => $contextMock, |
| 168 | + 'formKeyValidator' => $this->formKeyValidatorMock, |
| 169 | + 'resultRawFactory' => $this->resultRawFactoryMock, |
| 170 | + 'resultJsonFactory' => $this->resultJsonFactoryMock, |
| 171 | + ] |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Test for execute method |
| 177 | + * |
| 178 | + * @return void |
| 179 | + */ |
| 180 | + public function testExecute() |
| 181 | + { |
| 182 | + $data = [ |
| 183 | + 'payment-key-1' => 'payment-value-1', |
| 184 | + 'checks' => [ |
| 185 | + \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT, |
| 186 | + \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY, |
| 187 | + \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY, |
| 188 | + \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX, |
| 189 | + \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL, |
| 190 | + ] |
| 191 | + ]; |
| 192 | + |
| 193 | + $onepageMock = $this->getMock( |
| 194 | + 'Magento\Checkout\Model\Type\Onepage', |
| 195 | + [], |
| 196 | + [], |
| 197 | + '', |
| 198 | + false |
| 199 | + ); |
| 200 | + $redirectMock = $this->getMock( |
| 201 | + 'Magento\Framework\Controller\Result\Redirect', |
| 202 | + [], |
| 203 | + [], |
| 204 | + '', |
| 205 | + false |
| 206 | + ); |
| 207 | + $quoteMock = $this->getMock( |
| 208 | + 'Magento\Quote\Model\Quote', |
| 209 | + [], |
| 210 | + [], |
| 211 | + '', |
| 212 | + false |
| 213 | + ); |
| 214 | + $agreementsValidatorMock = $this->getMock( |
| 215 | + 'Magento\Checkout\Model\Agreements\AgreementsValidator', |
| 216 | + [], |
| 217 | + [], |
| 218 | + '', |
| 219 | + false |
| 220 | + ); |
| 221 | + $paymentMock = $this->getMock( |
| 222 | + 'Magento\Quote\Model\Quote\Payment', |
| 223 | + [], |
| 224 | + [], |
| 225 | + '', |
| 226 | + false |
| 227 | + ); |
| 228 | + $checkoutMock = $this->getMock( |
| 229 | + 'Magento\Checkout\Model\Session', |
| 230 | + ['getRedirectUrl'], |
| 231 | + [], |
| 232 | + '', |
| 233 | + false |
| 234 | + ); |
| 235 | + $resultJsonMock = $this->getMock( |
| 236 | + 'Magento\Framework\Controller\Result\Json', |
| 237 | + [], |
| 238 | + [], |
| 239 | + '', |
| 240 | + false |
| 241 | + ); |
| 242 | + |
| 243 | + $redirectMock->expects($this->never()) |
| 244 | + ->method('setPath') |
| 245 | + ->with('*/*/') |
| 246 | + ->willReturn('redirect'); |
| 247 | + |
| 248 | + $this->formKeyValidatorMock->expects($this->once()) |
| 249 | + ->method('validate') |
| 250 | + ->with($this->requestMock) |
| 251 | + ->willReturn(true); |
| 252 | + |
| 253 | + $this->resultRedirectFactoryMock->expects($this->never()) |
| 254 | + ->method('create') |
| 255 | + ->willReturn($redirectMock); |
| 256 | + |
| 257 | + $this->objectManagerMock->expects($this->atLeastOnce()) |
| 258 | + ->method('get') |
| 259 | + ->willReturnMap( |
| 260 | + [ |
| 261 | + ['Magento\Checkout\Model\Type\Onepage', $onepageMock], |
| 262 | + ['Magento\Checkout\Model\Agreements\AgreementsValidator', $agreementsValidatorMock], |
| 263 | + ] |
| 264 | + ); |
| 265 | + // call _expireAjax method |
| 266 | + $onepageMock->expects($this->atLeastOnce()) |
| 267 | + ->method('getQuote') |
| 268 | + ->willReturn($quoteMock); |
| 269 | + |
| 270 | + $quoteMock->expects($this->once()) |
| 271 | + ->method('hasItems') |
| 272 | + ->willReturn(false); |
| 273 | + $quoteMock->expects($this->never()) |
| 274 | + ->method('getHasError') |
| 275 | + ->willReturn(true); |
| 276 | + $quoteMock->expects($this->never()) |
| 277 | + ->method('validateMinimumAmount') |
| 278 | + ->willReturn(false); |
| 279 | + |
| 280 | + $this->requestMock->expects($this->never()) |
| 281 | + ->method('getActionName'); |
| 282 | + // -- end |
| 283 | + |
| 284 | + $this->requestMock->expects($this->atLeastOnce()) |
| 285 | + ->method('getPost') |
| 286 | + ->willReturnMap( |
| 287 | + [ |
| 288 | + [ |
| 289 | + 'agreement', |
| 290 | + [], |
| 291 | + [ |
| 292 | + 'test-key-1' => 'test-value-1' |
| 293 | + ] |
| 294 | + ], |
| 295 | + [ |
| 296 | + 'payment', |
| 297 | + [], |
| 298 | + $data |
| 299 | + ], |
| 300 | + ] |
| 301 | + ); |
| 302 | + |
| 303 | + $agreementsValidatorMock->expects($this->once()) |
| 304 | + ->method('isValid') |
| 305 | + ->with(['test-key-1']) |
| 306 | + ->willReturn(true); |
| 307 | + |
| 308 | + $quoteMock->expects($this->atLeastOnce()) |
| 309 | + ->method('getPayment') |
| 310 | + ->willReturn($paymentMock); |
| 311 | + |
| 312 | + $paymentMock->expects($this->once()) |
| 313 | + ->method('setQuote') |
| 314 | + ->with($quoteMock); |
| 315 | + $paymentMock->expects($this->once()) |
| 316 | + ->method('importData') |
| 317 | + ->with($data); |
| 318 | + |
| 319 | + $onepageMock->expects($this->once()) |
| 320 | + ->method('saveOrder'); |
| 321 | + $onepageMock->expects($this->once()) |
| 322 | + ->method('getCheckout') |
| 323 | + ->willReturn($checkoutMock); |
| 324 | + |
| 325 | + $checkoutMock->expects($this->once()) |
| 326 | + ->method('getRedirectUrl') |
| 327 | + ->willReturn(null); |
| 328 | + |
| 329 | + $this->eventManagerMock->expects($this->once()) |
| 330 | + ->method('dispatch') |
| 331 | + ->withConsecutive( |
| 332 | + $this->equalTo('checkout_controller_onepage_saveOrder'), |
| 333 | + $this->countOf(2) |
| 334 | + ); |
| 335 | + |
| 336 | + $this->resultJsonFactoryMock->expects($this->once()) |
| 337 | + ->method('create') |
| 338 | + ->willReturn($resultJsonMock); |
| 339 | + |
| 340 | + $resultJsonMock->expects($this->once()) |
| 341 | + ->method('setData') |
| 342 | + ->with(['success' => 1, 'error' => false]) |
| 343 | + ->willReturnSelf(); |
| 344 | + |
| 345 | + $this->assertEquals($resultJsonMock, $this->controller->execute()); |
| 346 | + } |
| 347 | +} |
0 commit comments