|
| 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\Sidebar; |
| 7 | + |
| 8 | +use Magento\Framework\Exception\LocalizedException; |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 10 | + |
| 11 | +class RemoveItemTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + /** @var \Magento\Checkout\Controller\Sidebar\RemoveItem */ |
| 14 | + protected $removeItem; |
| 15 | + |
| 16 | + /** @var ObjectManagerHelper */ |
| 17 | + protected $objectManagerHelper; |
| 18 | + |
| 19 | + /** @var \Magento\Checkout\Model\Sidebar|\PHPUnit_Framework_MockObject_MockObject */ |
| 20 | + protected $sidebarMock; |
| 21 | + |
| 22 | + /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 23 | + protected $loggerMock; |
| 24 | + |
| 25 | + /** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */ |
| 26 | + protected $jsonHelperMock; |
| 27 | + |
| 28 | + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 29 | + protected $requestMock; |
| 30 | + |
| 31 | + /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + protected $responseMock; |
| 33 | + |
| 34 | + /** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 35 | + protected $resultPageFactoryMock; |
| 36 | + |
| 37 | + protected function setUp() |
| 38 | + { |
| 39 | + $this->sidebarMock = $this->getMock('Magento\Checkout\Model\Sidebar', [], [], '', false); |
| 40 | + $this->loggerMock = $this->getMock('Psr\Log\LoggerInterface'); |
| 41 | + $this->jsonHelperMock = $this->getMock('Magento\Framework\Json\Helper\Data', [], [], '', false); |
| 42 | + $this->requestMock = $this->getMock('Magento\Framework\App\RequestInterface'); |
| 43 | + $this->responseMock = $this->getMockForAbstractClass( |
| 44 | + 'Magento\Framework\App\ResponseInterface', |
| 45 | + [], |
| 46 | + '', |
| 47 | + false, |
| 48 | + true, |
| 49 | + true, |
| 50 | + ['representJson'] |
| 51 | + ); |
| 52 | + $this->resultPageFactoryMock = $this->getMock('Magento\Framework\View\Result\PageFactory', [], [], '', false); |
| 53 | + |
| 54 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 55 | + $this->removeItem = $this->objectManagerHelper->getObject( |
| 56 | + 'Magento\Checkout\Controller\Sidebar\RemoveItem', |
| 57 | + [ |
| 58 | + 'sidebar' => $this->sidebarMock, |
| 59 | + 'logger' => $this->loggerMock, |
| 60 | + 'jsonHelper' => $this->jsonHelperMock, |
| 61 | + 'request' => $this->requestMock, |
| 62 | + 'response' => $this->responseMock, |
| 63 | + 'resultPageFactory' => $this->resultPageFactoryMock, |
| 64 | + ] |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function testExecute() |
| 69 | + { |
| 70 | + $this->requestMock->expects($this->once()) |
| 71 | + ->method('getParam') |
| 72 | + ->with('item_id', null) |
| 73 | + ->willReturn('1'); |
| 74 | + |
| 75 | + $this->sidebarMock->expects($this->once()) |
| 76 | + ->method('checkQuoteItem') |
| 77 | + ->with(1) |
| 78 | + ->willReturnSelf(); |
| 79 | + $this->sidebarMock->expects($this->once()) |
| 80 | + ->method('removeQuoteItem') |
| 81 | + ->with(1) |
| 82 | + ->willReturnSelf(); |
| 83 | + $this->sidebarMock->expects($this->once()) |
| 84 | + ->method('getResponseData') |
| 85 | + ->with('') |
| 86 | + ->willReturn( |
| 87 | + [ |
| 88 | + 'cleanup' => true, |
| 89 | + 'data' => [ |
| 90 | + 'summary_qty' => 0, |
| 91 | + 'summary_text' => __(' items'), |
| 92 | + 'subtotal' => 0, |
| 93 | + ], |
| 94 | + ] |
| 95 | + ); |
| 96 | + |
| 97 | + $pageMock = $this->getMockBuilder('Magento\Framework\View\Result\Page') |
| 98 | + ->disableOriginalConstructor() |
| 99 | + ->getMock(); |
| 100 | + |
| 101 | + $this->resultPageFactoryMock->expects($this->once()) |
| 102 | + ->method('create') |
| 103 | + ->with(false, []) |
| 104 | + ->willReturn($pageMock); |
| 105 | + |
| 106 | + $layoutMock = $this->getMockBuilder('Magento\Framework\View\LayoutInterface') |
| 107 | + ->getMock(); |
| 108 | + |
| 109 | + $pageMock->expects($this->once()) |
| 110 | + ->method('getLayout') |
| 111 | + ->willReturn($layoutMock); |
| 112 | + |
| 113 | + $blockMock = $this->getMockBuilder('Magento\Framework\View\Element\BlockInterface') |
| 114 | + ->getMock(); |
| 115 | + |
| 116 | + $layoutMock->expects($this->once()) |
| 117 | + ->method('getBlock') |
| 118 | + ->with('minicart.content') |
| 119 | + ->willReturn($blockMock); |
| 120 | + |
| 121 | + $blockMock->expects($this->once()) |
| 122 | + ->method('toHtml') |
| 123 | + ->willReturn('block html'); |
| 124 | + |
| 125 | + $this->jsonHelperMock->expects($this->once()) |
| 126 | + ->method('jsonEncode') |
| 127 | + ->with( |
| 128 | + [ |
| 129 | + 'cleanup' => true, |
| 130 | + 'data' => [ |
| 131 | + 'summary_qty' => 0, |
| 132 | + 'summary_text' => __(' items'), |
| 133 | + 'subtotal' => 0, |
| 134 | + ], |
| 135 | + 'content' => 'block html', |
| 136 | + ] |
| 137 | + ) |
| 138 | + ->willReturn('json encoded'); |
| 139 | + |
| 140 | + $this->responseMock->expects($this->once()) |
| 141 | + ->method('representJson') |
| 142 | + ->with('json encoded') |
| 143 | + ->willReturn('json represented'); |
| 144 | + |
| 145 | + $this->assertEquals('json represented', $this->removeItem->execute()); |
| 146 | + } |
| 147 | + |
| 148 | + public function testExecuteWithLocalizedException() |
| 149 | + { |
| 150 | + $this->requestMock->expects($this->once()) |
| 151 | + ->method('getParam') |
| 152 | + ->with('item_id', null) |
| 153 | + ->willReturn('1'); |
| 154 | + |
| 155 | + $this->sidebarMock->expects($this->once()) |
| 156 | + ->method('checkQuoteItem') |
| 157 | + ->with(1) |
| 158 | + ->willThrowException(new LocalizedException(__('Error message!'))); |
| 159 | + |
| 160 | + $this->sidebarMock->expects($this->once()) |
| 161 | + ->method('getResponseData') |
| 162 | + ->with('Error message!') |
| 163 | + ->willReturn( |
| 164 | + [ |
| 165 | + 'success' => false, |
| 166 | + 'error_message' => 'Error message!', |
| 167 | + ] |
| 168 | + ); |
| 169 | + |
| 170 | + $this->jsonHelperMock->expects($this->once()) |
| 171 | + ->method('jsonEncode') |
| 172 | + ->with( |
| 173 | + [ |
| 174 | + 'success' => false, |
| 175 | + 'error_message' => 'Error message!', |
| 176 | + ] |
| 177 | + ) |
| 178 | + ->willReturn('json encoded'); |
| 179 | + |
| 180 | + $this->responseMock->expects($this->once()) |
| 181 | + ->method('representJson') |
| 182 | + ->with('json encoded') |
| 183 | + ->willReturn('json represented'); |
| 184 | + |
| 185 | + $this->assertEquals('json represented', $this->removeItem->execute()); |
| 186 | + } |
| 187 | + |
| 188 | + public function testExecuteWithException() |
| 189 | + { |
| 190 | + $this->requestMock->expects($this->once()) |
| 191 | + ->method('getParam') |
| 192 | + ->with('item_id', null) |
| 193 | + ->willReturn('1'); |
| 194 | + |
| 195 | + $exception = new \Exception('Error message!'); |
| 196 | + |
| 197 | + $this->sidebarMock->expects($this->once()) |
| 198 | + ->method('checkQuoteItem') |
| 199 | + ->with(1) |
| 200 | + ->willThrowException($exception); |
| 201 | + |
| 202 | + $this->loggerMock->expects($this->once()) |
| 203 | + ->method('critical') |
| 204 | + ->with($exception) |
| 205 | + ->willReturn(null); |
| 206 | + |
| 207 | + $this->sidebarMock->expects($this->once()) |
| 208 | + ->method('getResponseData') |
| 209 | + ->with('Error message!') |
| 210 | + ->willReturn( |
| 211 | + [ |
| 212 | + 'success' => false, |
| 213 | + 'error_message' => 'Error message!', |
| 214 | + ] |
| 215 | + ); |
| 216 | + |
| 217 | + $this->jsonHelperMock->expects($this->once()) |
| 218 | + ->method('jsonEncode') |
| 219 | + ->with( |
| 220 | + [ |
| 221 | + 'success' => false, |
| 222 | + 'error_message' => 'Error message!', |
| 223 | + ] |
| 224 | + ) |
| 225 | + ->willReturn('json encoded'); |
| 226 | + |
| 227 | + $this->responseMock->expects($this->once()) |
| 228 | + ->method('representJson') |
| 229 | + ->with('json encoded') |
| 230 | + ->willReturn('json represented'); |
| 231 | + |
| 232 | + $this->assertEquals('json represented', $this->removeItem->execute()); |
| 233 | + } |
| 234 | +} |
0 commit comments