|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Test\Unit\Controller\Category; |
| 7 | + |
| 8 | +use Magento\Catalog\Controller\Adminhtml\Category\Move; |
| 9 | +use Magento\Framework\Message\ManagerInterface; |
| 10 | +use Magento\Framework\ObjectManagerInterface; |
| 11 | +use Magento\Framework\Registry; |
| 12 | + |
| 13 | +class MoveTest extends \PHPUnit_Framework_TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var \Magento\Framework\Controller\Result\JsonFactory | \PHPUnit_Framework_MockObject_MockObject |
| 17 | + */ |
| 18 | + private $resultJsonFactoryMock; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\Framework\View\LayoutFactory | \PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + private $layoutFactoryMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Psr\Log\LoggerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $loggerMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $context; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $request; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Catalog\Controller\Adminhtml\Category\Move |
| 42 | + */ |
| 43 | + private $moveController; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + private $objectManager; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ManagerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 52 | + */ |
| 53 | + private $messageManager; |
| 54 | + |
| 55 | + public function setUp() |
| 56 | + { |
| 57 | + $this->resultJsonFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class) |
| 58 | + ->setMethods(['create']) |
| 59 | + ->disableOriginalConstructor() |
| 60 | + ->getMock(); |
| 61 | + $this->layoutFactoryMock = $this->getMockBuilder(\Magento\Framework\View\LayoutFactory::class) |
| 62 | + ->setMethods(['create']) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + $this->context = $this->getMock(\Magento\Backend\App\Action\Context::class, [], [], '', false); |
| 66 | + $this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); |
| 67 | + $this->fillContext(); |
| 68 | + |
| 69 | + $this->moveController = new Move( |
| 70 | + $this->context, |
| 71 | + $this->resultJsonFactoryMock, |
| 72 | + $this->layoutFactoryMock, |
| 73 | + $this->loggerMock |
| 74 | + ); |
| 75 | + $this->initObjectManager(); |
| 76 | + } |
| 77 | + |
| 78 | + private function fillContext() |
| 79 | + { |
| 80 | + $this->request = $this |
| 81 | + ->getMockBuilder(\Magento\Framework\App\RequestInterface::class) |
| 82 | + ->setMethods(['getPost']) |
| 83 | + ->disableOriginalConstructor() |
| 84 | + ->getMockForAbstractClass(); |
| 85 | + $this->context->expects($this->once())->method('getRequest')->will($this->returnValue($this->request)); |
| 86 | + $this->messageManager = $this->getMock(ManagerInterface::class); |
| 87 | + $this->context->expects($this->once())->method('getMessageManager')->willReturn($this->messageManager); |
| 88 | + } |
| 89 | + |
| 90 | + private function initObjectManager() |
| 91 | + { |
| 92 | + $this->objectManager = $this->getMock(ObjectManagerInterface::class); |
| 93 | + $moveController = new \ReflectionClass($this->moveController); |
| 94 | + $objectManagerProp = $moveController->getProperty('_objectManager'); |
| 95 | + $objectManagerProp->setAccessible(true); |
| 96 | + $objectManagerProp->setValue($this->moveController, $this->objectManager); |
| 97 | + } |
| 98 | + |
| 99 | + public function testExecuteWithGenericException() |
| 100 | + { |
| 101 | + $messagesCollection = $this->getMockBuilder(\Magento\Framework\Message\Collection::class) |
| 102 | + ->disableOriginalConstructor() |
| 103 | + ->getMock(); |
| 104 | + $messageBlock = $this->getMockBuilder(\Magento\Framework\View\Element\Messages::class) |
| 105 | + ->disableOriginalConstructor() |
| 106 | + ->getMock(); |
| 107 | + $layoutMock = $this->getMock(\Magento\Framework\View\LayoutInterface::class); |
| 108 | + $this->layoutFactoryMock->expects($this->once()) |
| 109 | + ->method('create') |
| 110 | + ->willReturn($layoutMock); |
| 111 | + $layoutMock->expects($this->once()) |
| 112 | + ->method('getMessagesBlock') |
| 113 | + ->willReturn($messageBlock); |
| 114 | + $wysiwigConfig = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class) |
| 115 | + ->disableOriginalConstructor() |
| 116 | + ->getMock(); |
| 117 | + $registry = $this->getMockBuilder(Registry::class) |
| 118 | + ->disableOriginalConstructor() |
| 119 | + ->getMock(); |
| 120 | + $categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class) |
| 121 | + ->disableOriginalConstructor() |
| 122 | + ->getMock(); |
| 123 | + $this->request->expects($this->exactly(2)) |
| 124 | + ->method('getPost') |
| 125 | + ->withConsecutive(['pid', false], ['aid', false]) |
| 126 | + ->willReturnMap([['pid', false, 2], ['aid', false, 1]]); |
| 127 | + $this->objectManager->expects($this->once()) |
| 128 | + ->method('create') |
| 129 | + ->with(\Magento\Catalog\Model\Category::class) |
| 130 | + ->willReturn($categoryMock); |
| 131 | + $this->objectManager->expects($this->any()) |
| 132 | + ->method('get') |
| 133 | + ->withConsecutive([Registry::class], [Registry::class], [\Magento\Cms\Model\Wysiwyg\Config::class]) |
| 134 | + ->willReturnMap([[Registry::class, $registry], [\Magento\Cms\Model\Wysiwyg\Config::class, $wysiwigConfig]]); |
| 135 | + $categoryMock->expects($this->once()) |
| 136 | + ->method('move') |
| 137 | + ->willThrowException(new \Exception( |
| 138 | + __('Some exception') |
| 139 | + )); |
| 140 | + $this->messageManager->expects($this->once()) |
| 141 | + ->method('addError') |
| 142 | + ->with(__('There was a category move error.')); |
| 143 | + $this->messageManager->expects($this->once()) |
| 144 | + ->method('getMessages') |
| 145 | + ->with(true) |
| 146 | + ->willReturn($messagesCollection); |
| 147 | + $messageBlock->expects($this->once()) |
| 148 | + ->method('setMessages') |
| 149 | + ->with($messagesCollection); |
| 150 | + $resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class) |
| 151 | + ->disableOriginalConstructor() |
| 152 | + ->getMock(); |
| 153 | + $messageBlock->expects($this->once()) |
| 154 | + ->method('getGroupedHtml') |
| 155 | + ->willReturn('<body></body>'); |
| 156 | + $resultJsonMock->expects($this->once()) |
| 157 | + ->method('setData') |
| 158 | + ->with( |
| 159 | + [ |
| 160 | + 'messages' => '<body></body>', |
| 161 | + 'error' => true |
| 162 | + ] |
| 163 | + ) |
| 164 | + ->willReturn(true); |
| 165 | + $this->resultJsonFactoryMock |
| 166 | + ->expects($this->once()) |
| 167 | + ->method('create') |
| 168 | + ->willReturn($resultJsonMock); |
| 169 | + $this->assertTrue($this->moveController->execute()); |
| 170 | + } |
| 171 | + |
| 172 | + public function testExecuteWithLocaliedException() |
| 173 | + { |
| 174 | + $exceptionMessage = 'Sorry, but we can\'t find the new category you selected.'; |
| 175 | + $messagesCollection = $this->getMockBuilder(\Magento\Framework\Message\Collection::class) |
| 176 | + ->disableOriginalConstructor() |
| 177 | + ->getMock(); |
| 178 | + $messageBlock = $this->getMockBuilder(\Magento\Framework\View\Element\Messages::class) |
| 179 | + ->disableOriginalConstructor() |
| 180 | + ->getMock(); |
| 181 | + $layoutMock = $this->getMock(\Magento\Framework\View\LayoutInterface::class); |
| 182 | + $this->layoutFactoryMock->expects($this->once()) |
| 183 | + ->method('create') |
| 184 | + ->willReturn($layoutMock); |
| 185 | + $layoutMock->expects($this->once()) |
| 186 | + ->method('getMessagesBlock') |
| 187 | + ->willReturn($messageBlock); |
| 188 | + $wysiwigConfig = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class) |
| 189 | + ->disableOriginalConstructor() |
| 190 | + ->getMock(); |
| 191 | + $registry = $this->getMockBuilder(Registry::class) |
| 192 | + ->disableOriginalConstructor() |
| 193 | + ->getMock(); |
| 194 | + $categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class) |
| 195 | + ->disableOriginalConstructor() |
| 196 | + ->getMock(); |
| 197 | + $this->request->expects($this->exactly(2)) |
| 198 | + ->method('getPost') |
| 199 | + ->withConsecutive(['pid', false], ['aid', false]) |
| 200 | + ->willReturnMap([['pid', false, 2], ['aid', false, 1]]); |
| 201 | + $this->objectManager->expects($this->once()) |
| 202 | + ->method('create') |
| 203 | + ->with(\Magento\Catalog\Model\Category::class) |
| 204 | + ->willReturn($categoryMock); |
| 205 | + $this->objectManager->expects($this->any()) |
| 206 | + ->method('get') |
| 207 | + ->withConsecutive([Registry::class], [Registry::class], [\Magento\Cms\Model\Wysiwyg\Config::class]) |
| 208 | + ->willReturnMap([[Registry::class, $registry], [\Magento\Cms\Model\Wysiwyg\Config::class, $wysiwigConfig]]); |
| 209 | + $this->messageManager->expects($this->once()) |
| 210 | + ->method('addError') |
| 211 | + ->with($exceptionMessage); |
| 212 | + $this->messageManager->expects($this->once()) |
| 213 | + ->method('getMessages') |
| 214 | + ->with(true) |
| 215 | + ->willReturn($messagesCollection); |
| 216 | + $messageBlock->expects($this->once()) |
| 217 | + ->method('setMessages') |
| 218 | + ->with($messagesCollection); |
| 219 | + $resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class) |
| 220 | + ->disableOriginalConstructor() |
| 221 | + ->getMock(); |
| 222 | + $messageBlock->expects($this->once()) |
| 223 | + ->method('getGroupedHtml') |
| 224 | + ->willReturn('<body></body>'); |
| 225 | + $resultJsonMock->expects($this->once()) |
| 226 | + ->method('setData') |
| 227 | + ->with( |
| 228 | + [ |
| 229 | + 'messages' => '<body></body>', |
| 230 | + 'error' => true |
| 231 | + ] |
| 232 | + ) |
| 233 | + ->willReturn(true); |
| 234 | + $categoryMock->expects($this->once()) |
| 235 | + ->method('move') |
| 236 | + ->willThrowException(new \Magento\Framework\Exception\LocalizedException( |
| 237 | + __($exceptionMessage) |
| 238 | + )); |
| 239 | + $this->resultJsonFactoryMock |
| 240 | + ->expects($this->once()) |
| 241 | + ->method('create') |
| 242 | + ->willReturn($resultJsonMock); |
| 243 | + $this->assertTrue($this->moveController->execute()); |
| 244 | + } |
| 245 | + |
| 246 | + public function testSuccessfullCategorySave() |
| 247 | + { |
| 248 | + $exceptionMessage = 'Sorry, but we can\'t find the new category you selected.'; |
| 249 | + $messagesCollection = $this->getMockBuilder(\Magento\Framework\Message\Collection::class) |
| 250 | + ->disableOriginalConstructor() |
| 251 | + ->getMock(); |
| 252 | + $messageBlock = $this->getMockBuilder(\Magento\Framework\View\Element\Messages::class) |
| 253 | + ->disableOriginalConstructor() |
| 254 | + ->getMock(); |
| 255 | + $layoutMock = $this->getMock(\Magento\Framework\View\LayoutInterface::class); |
| 256 | + $this->layoutFactoryMock->expects($this->once()) |
| 257 | + ->method('create') |
| 258 | + ->willReturn($layoutMock); |
| 259 | + $layoutMock->expects($this->once()) |
| 260 | + ->method('getMessagesBlock') |
| 261 | + ->willReturn($messageBlock); |
| 262 | + $wysiwigConfig = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class) |
| 263 | + ->disableOriginalConstructor() |
| 264 | + ->getMock(); |
| 265 | + $registry = $this->getMockBuilder(Registry::class) |
| 266 | + ->disableOriginalConstructor() |
| 267 | + ->getMock(); |
| 268 | + $categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class) |
| 269 | + ->disableOriginalConstructor() |
| 270 | + ->getMock(); |
| 271 | + $this->request->expects($this->exactly(2)) |
| 272 | + ->method('getPost') |
| 273 | + ->withConsecutive(['pid', false], ['aid', false]) |
| 274 | + ->willReturnMap([['pid', false, 2], ['aid', false, 1]]); |
| 275 | + $this->objectManager->expects($this->once()) |
| 276 | + ->method('create') |
| 277 | + ->with(\Magento\Catalog\Model\Category::class) |
| 278 | + ->willReturn($categoryMock); |
| 279 | + $this->objectManager->expects($this->any()) |
| 280 | + ->method('get') |
| 281 | + ->withConsecutive([Registry::class], [Registry::class], [\Magento\Cms\Model\Wysiwyg\Config::class]) |
| 282 | + ->willReturnMap([[Registry::class, $registry], [\Magento\Cms\Model\Wysiwyg\Config::class, $wysiwigConfig]]); |
| 283 | + $this->messageManager->expects($this->once()) |
| 284 | + ->method('getMessages') |
| 285 | + ->with(true) |
| 286 | + ->willReturn($messagesCollection); |
| 287 | + $messageBlock->expects($this->once()) |
| 288 | + ->method('setMessages') |
| 289 | + ->with($messagesCollection); |
| 290 | + $resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class) |
| 291 | + ->disableOriginalConstructor() |
| 292 | + ->getMock(); |
| 293 | + $messageBlock->expects($this->once()) |
| 294 | + ->method('getGroupedHtml') |
| 295 | + ->willReturn('<body></body>'); |
| 296 | + $resultJsonMock->expects($this->once()) |
| 297 | + ->method('setData') |
| 298 | + ->with( |
| 299 | + [ |
| 300 | + 'messages' => '<body></body>', |
| 301 | + 'error' => false |
| 302 | + ] |
| 303 | + ) |
| 304 | + ->willReturn(true); |
| 305 | + $this->messageManager->expects($this->once()) |
| 306 | + ->method('addSuccess') |
| 307 | + ->with(__('You moved the category.')); |
| 308 | + $categoryMock->expects($this->once()) |
| 309 | + ->method('move') |
| 310 | + ->with(2,1); |
| 311 | + $this->resultJsonFactoryMock |
| 312 | + ->expects($this->once()) |
| 313 | + ->method('create') |
| 314 | + ->willReturn($resultJsonMock); |
| 315 | + $this->assertTrue($this->moveController->execute()); |
| 316 | + } |
| 317 | +} |
0 commit comments