|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Checkout\Controller\Cart; |
| 7 | + |
| 8 | +/** |
| 9 | + * Shopping cart edit tests |
| 10 | + */ |
| 11 | +class ConfigureTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 16 | + */ |
| 17 | + protected $objectManagerMock; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var \Magento\Framework\View\Result\PageFactory | \PHPUnit_Framework_MockObject_MockObject |
| 21 | + */ |
| 22 | + protected $resultPageFactoryMock; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Magento\Framework\App\ResponseInterface | \PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + protected $responseMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \Magento\Framework\App\RequestInterface | \PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + protected $requestMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \Magento\Framework\Message\ManagerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + protected $messageManagerMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var \Magento\Framework\App\Response\RedirectInterface | \PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + protected $redirectMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var \Magento\Checkout\Controller\Cart\Configure | \PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + protected $configureController; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var \Magento\Framework\App\Action\Context | \PHPUnit_Framework_MockObject_MockObject |
| 51 | + */ |
| 52 | + protected $contextMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var \Magento\Checkout\Model\Cart | \PHPUnit_Framework_MockObject_MockObject |
| 56 | + */ |
| 57 | + protected $cartMock; |
| 58 | + |
| 59 | + public function setUp() |
| 60 | + { |
| 61 | + $eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface') |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->setMethods([]) |
| 64 | + ->getMockForAbstractClass(); |
| 65 | + $urlMock = $this->getMockBuilder('Magento\Framework\UrlInterface') |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->setMethods([]) |
| 68 | + ->getMockForAbstractClass(); |
| 69 | + $actionFlagMock = $this->getMockBuilder('Magento\Framework\App\ActionFlag') |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->setMethods([]) |
| 72 | + ->getMockForAbstractClass(); |
| 73 | + $viewMock = $this->getMockBuilder('Magento\Framework\App\ViewInterface') |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->setMethods([]) |
| 76 | + ->getMockForAbstractClass(); |
| 77 | + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->setMethods([]) |
| 80 | + ->getMockForAbstractClass(); |
| 81 | + $this->responseMock = $this->getMockBuilder('Magento\Framework\App\ResponseInterface') |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->setMethods([]) |
| 84 | + ->getMockForAbstractClass(); |
| 85 | + $this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface') |
| 86 | + ->disableOriginalConstructor() |
| 87 | + ->setMethods(['getParam']) |
| 88 | + ->getMockForAbstractClass(); |
| 89 | + $this->messageManagerMock = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface') |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->setMethods([]) |
| 92 | + ->getMockForAbstractClass(); |
| 93 | + $this->redirectMock = $this->getMockBuilder('Magento\Framework\App\Response\RedirectInterface') |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->setMethods([]) |
| 96 | + ->getMock(); |
| 97 | + |
| 98 | + $this->contextMock = $this->getMockBuilder('Magento\Framework\App\Action\Context') |
| 99 | + ->setConstructorArgs( |
| 100 | + [ |
| 101 | + $this->requestMock, |
| 102 | + $this->responseMock, |
| 103 | + $this->objectManagerMock, |
| 104 | + $eventManagerMock, |
| 105 | + $urlMock, |
| 106 | + $this->redirectMock, |
| 107 | + $actionFlagMock, |
| 108 | + $viewMock, |
| 109 | + $this->messageManagerMock |
| 110 | + ] |
| 111 | + ) |
| 112 | + ->setMethods([]) |
| 113 | + ->getMock(); |
| 114 | + $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock); |
| 115 | + $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock); |
| 116 | + $this->contextMock->expects($this->any())->method('getResponse')->willReturn($this->responseMock); |
| 117 | + $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock); |
| 118 | + $this->contextMock->expects($this->any())->method('getRedirect')->willReturn($this->redirectMock); |
| 119 | + $scopeConfig = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface') |
| 120 | + ->disableOriginalConstructor() |
| 121 | + ->getMock(); |
| 122 | + $session = $this->getMockBuilder('Magento\Checkout\Model\Session') |
| 123 | + ->disableOriginalConstructor() |
| 124 | + ->getMock(); |
| 125 | + $storeManager = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') |
| 126 | + ->disableOriginalConstructor() |
| 127 | + ->getMockForAbstractClass(); |
| 128 | + $formKeyValidator = $this->getMockBuilder('Magento\Core\App\Action\FormKeyValidator') |
| 129 | + ->disableOriginalConstructor() |
| 130 | + ->getMockForAbstractClass(); |
| 131 | + $this->cartMock = $this->getMockBuilder('Magento\Checkout\Model\Cart') |
| 132 | + ->disableOriginalConstructor() |
| 133 | + ->getMock(); |
| 134 | + $this->resultPageFactoryMock = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory') |
| 135 | + ->disableOriginalConstructor() |
| 136 | + ->getMock(); |
| 137 | + |
| 138 | + $this->configureController = new \Magento\Checkout\Controller\Cart\Configure( |
| 139 | + $this->contextMock, |
| 140 | + $scopeConfig, |
| 141 | + $session, |
| 142 | + $storeManager, |
| 143 | + $formKeyValidator, |
| 144 | + $this->cartMock, |
| 145 | + $this->resultPageFactoryMock |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Test checks controller call product view and send parameter to it |
| 151 | + * |
| 152 | + * @return void |
| 153 | + */ |
| 154 | + public function testPrepareAndRenderCall() |
| 155 | + { |
| 156 | + $quoteId = 1; |
| 157 | + $actualProductId = 1; |
| 158 | + $quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote') |
| 159 | + ->disableOriginalConstructor() |
| 160 | + ->getMock(); |
| 161 | + $quoteItemMock = $this->getMockBuilder('Magento\Quote\Model\Quote\Item') |
| 162 | + ->disableOriginalConstructor() |
| 163 | + ->getMock(); |
| 164 | + $productMock = $this->getMockBuilder('Magento\Catalog\Model\Product') |
| 165 | + ->disableOriginalConstructor() |
| 166 | + ->getMock(); |
| 167 | + $viewMock = $this->getMockBuilder('Magento\Catalog\Helper\Product\View') |
| 168 | + ->disableOriginalConstructor() |
| 169 | + ->getMock(); |
| 170 | + $pageMock = $this->getMockBuilder('Magento\Framework\View\Result\Page') |
| 171 | + ->disableOriginalConstructor() |
| 172 | + ->getMock(); |
| 173 | + $buyRequestMock = $this->getMockBuilder('Magento\Framework\Object') |
| 174 | + ->disableOriginalConstructor() |
| 175 | + ->getMock(); |
| 176 | + //expects |
| 177 | + $this->requestMock->expects($this->at(0)) |
| 178 | + ->method('getParam') |
| 179 | + ->with('id') |
| 180 | + ->willReturn($quoteId); |
| 181 | + $this->requestMock->expects($this->at(1)) |
| 182 | + ->method('getParam') |
| 183 | + ->with('product_id') |
| 184 | + ->willReturn($actualProductId); |
| 185 | + $this->cartMock->expects($this->any())->method('getQuote')->willReturn($quoteMock); |
| 186 | + |
| 187 | + $quoteItemMock->expects($this->exactly(1))->method('getBuyRequest')->willReturn($buyRequestMock); |
| 188 | + |
| 189 | + $this->resultPageFactoryMock->expects($this->once())->method('create')->willReturn($pageMock); |
| 190 | + $this->objectManagerMock->expects($this->at(0)) |
| 191 | + ->method('get') |
| 192 | + ->with('Magento\Catalog\Helper\Product\View') |
| 193 | + ->willReturn($viewMock); |
| 194 | + |
| 195 | + $viewMock->expects($this->once())->method('prepareAndRender')->with( |
| 196 | + $pageMock, |
| 197 | + $actualProductId, |
| 198 | + $this->configureController, |
| 199 | + $this->callback( |
| 200 | + function ($subject) use ($buyRequestMock) { |
| 201 | + return $subject->getBuyRequest() === $buyRequestMock; |
| 202 | + } |
| 203 | + ) |
| 204 | + )->willReturn($pageMock); |
| 205 | + |
| 206 | + $quoteMock->expects($this->once())->method('getItemById')->willReturn($quoteItemMock); |
| 207 | + $quoteItemMock->expects($this->exactly(2))->method('getProduct')->willReturn($productMock); |
| 208 | + |
| 209 | + $productMock->expects($this->exactly(2))->method('getId')->willReturn($actualProductId); |
| 210 | + |
| 211 | + $this->assertSame($pageMock, $this->configureController->execute()); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Test checks controller redirect user to cart |
| 216 | + * if user request product id in cart edit page is not same as quota product id |
| 217 | + * |
| 218 | + * @return void |
| 219 | + */ |
| 220 | + public function testRedirectWithWrongProductId() |
| 221 | + { |
| 222 | + $quotaId = 1; |
| 223 | + $productIdInQuota = 1; |
| 224 | + $productIdInRequest = null; |
| 225 | + $quoteItemMock = $this->getMockBuilder('Magento\Quote\Model\Quote\Item') |
| 226 | + ->disableOriginalConstructor() |
| 227 | + ->getMock(); |
| 228 | + $quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote') |
| 229 | + ->disableOriginalConstructor() |
| 230 | + ->getMock(); |
| 231 | + $productMock = $this->getMockBuilder('Magento\Catalog\Model\Product') |
| 232 | + ->disableOriginalConstructor() |
| 233 | + ->getMock(); |
| 234 | + $this->requestMock->expects($this->at(0)) |
| 235 | + ->method('getParam') |
| 236 | + ->with('id') |
| 237 | + ->willReturn($quotaId); |
| 238 | + $this->requestMock->expects($this->at(1)) |
| 239 | + ->method('getParam') |
| 240 | + ->with('product_id') |
| 241 | + ->willReturn($productIdInRequest); |
| 242 | + $this->cartMock->expects($this->any())->method('getQuote')->willReturn($quoteMock); |
| 243 | + $quoteMock->expects($this->once())->method('getItemById')->willReturn($quoteItemMock); |
| 244 | + $quoteItemMock->expects($this->exactly(1))->method('getProduct')->willReturn($productMock); |
| 245 | + $productMock->expects($this->exactly(1))->method('getId')->willReturn($productIdInQuota); |
| 246 | + $this->messageManagerMock->expects($this->once())->method('addError'); |
| 247 | + $this->redirectMock->expects($this->once())->method('redirect')->with($this->responseMock, 'checkout/cart', []); |
| 248 | + $this->configureController->execute(); |
| 249 | + } |
| 250 | +} |
0 commit comments