|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Framework\App; |
| 8 | + |
| 9 | +/** |
| 10 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 11 | + */ |
| 12 | +class AreaTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + const SCOPE_ID = '1'; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var \Magento\TestFramework\Helper\ObjectManager |
| 18 | + */ |
| 19 | + protected $objectManager; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Framework\Event\ManagerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + protected $eventManagerMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + protected $objectManagerMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Framework\App\ObjectManager\ConfigLoader | \PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + protected $diConfigLoaderMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\Framework\TranslateInterface | \PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + protected $translatorMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Psr\Log\LoggerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + protected $loggerMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\Framework\App\DesignInterface | \PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + protected $designMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Magento\Framework\App\ScopeResolverInterface | \PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + protected $scopeResolverMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \Magento\Framework\View\DesignExceptions | \PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + protected $designExceptionsMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + protected $areaCode; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var Area |
| 68 | + */ |
| 69 | + protected $object; |
| 70 | + |
| 71 | + /** @var \Magento\Framework\Phrase\RendererInterface */ |
| 72 | + private $defaultRenderer; |
| 73 | + |
| 74 | + public function setUp() |
| 75 | + { |
| 76 | + $this->defaultRenderer = \Magento\Framework\Phrase::getRenderer(); |
| 77 | + $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); |
| 78 | + $this->loggerMock = $this->getMockBuilder('Psr\Log\LoggerInterface') |
| 79 | + ->disableOriginalConstructor() |
| 80 | + ->getMock(); |
| 81 | + $this->eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface') |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->getMock(); |
| 84 | + $this->translatorMock = $this->getMockBuilder('Magento\Framework\TranslateInterface') |
| 85 | + ->disableOriginalConstructor() |
| 86 | + ->getMock(); |
| 87 | + $this->diConfigLoaderMock = $this->getMockBuilder('Magento\Framework\App\ObjectManager\ConfigLoader') |
| 88 | + ->disableOriginalConstructor() |
| 89 | + ->getMock(); |
| 90 | + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->getMock(); |
| 93 | + $this->designMock = $this->getMockBuilder('Magento\Framework\App\DesignInterface') |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + $this->scopeResolverMock = $this->getMockBuilder('Magento\Framework\App\ScopeResolverInterface') |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMock(); |
| 99 | + $scopeMock = $this->getMockBuilder('Magento\Framework\App\ScopeInterface') |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->getMock(); |
| 102 | + $scopeMock->expects($this->any()) |
| 103 | + ->method('getId') |
| 104 | + ->will($this->returnValue(self::SCOPE_ID)); |
| 105 | + $this->scopeResolverMock->expects($this->any()) |
| 106 | + ->method('getScope') |
| 107 | + ->will($this->returnValue($scopeMock)); |
| 108 | + $this->designExceptionsMock = $this->getMockBuilder('Magento\Framework\View\DesignExceptions') |
| 109 | + ->disableOriginalConstructor() |
| 110 | + ->getMock(); |
| 111 | + $this->areaCode = Area::AREA_FRONTEND; |
| 112 | + |
| 113 | + $this->object = $this->objectManager->getObject( |
| 114 | + 'Magento\Framework\App\Area', |
| 115 | + [ |
| 116 | + 'logger' => $this->loggerMock, |
| 117 | + 'objectManager' => $this->objectManagerMock, |
| 118 | + 'eventManager' => $this->eventManagerMock, |
| 119 | + 'translator' => $this->translatorMock, |
| 120 | + 'diConfigLoader' => $this->diConfigLoaderMock, |
| 121 | + 'design' => $this->designMock, |
| 122 | + 'scopeResolver' => $this->scopeResolverMock, |
| 123 | + 'designExceptions' => $this->designExceptionsMock, |
| 124 | + 'areaCode' => $this->areaCode, |
| 125 | + ] |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + public function tearDown() |
| 130 | + { |
| 131 | + \Magento\Framework\Phrase::setRenderer($this->defaultRenderer); |
| 132 | + } |
| 133 | + |
| 134 | + public function testLoadConfig() |
| 135 | + { |
| 136 | + $this->verifyLoadConfig(); |
| 137 | + $this->object->load(Area::PART_CONFIG); |
| 138 | + } |
| 139 | + |
| 140 | + public function testLoadTranslate() |
| 141 | + { |
| 142 | + $this->translatorMock->expects($this->once()) |
| 143 | + ->method('loadData'); |
| 144 | + $renderMock = $this->getMockBuilder('Magento\Framework\Phrase\RendererInterface') |
| 145 | + ->disableOriginalConstructor() |
| 146 | + ->getMock(); |
| 147 | + $this->objectManagerMock->expects($this->once()) |
| 148 | + ->method('get') |
| 149 | + ->with('Magento\Framework\Phrase\RendererInterface') |
| 150 | + ->will($this->returnValue($renderMock)); |
| 151 | + $this->object->load(Area::PART_TRANSLATE); |
| 152 | + } |
| 153 | + |
| 154 | + public function testLoadDesign() |
| 155 | + { |
| 156 | + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') |
| 157 | + ->disableOriginalConstructor() |
| 158 | + ->getMock(); |
| 159 | + $this->objectManagerMock->expects($this->once()) |
| 160 | + ->method('get') |
| 161 | + ->with('Magento\Framework\View\DesignInterface') |
| 162 | + ->will($this->returnValue($designMock)); |
| 163 | + $designMock->expects($this->once()) |
| 164 | + ->method('setArea') |
| 165 | + ->with($this->areaCode) |
| 166 | + ->willReturnSelf(); |
| 167 | + $designMock->expects($this->once()) |
| 168 | + ->method('setDefaultDesignTheme'); |
| 169 | + $this->object->load(Area::PART_DESIGN); |
| 170 | + } |
| 171 | + |
| 172 | + public function testLoadUnknownPart() |
| 173 | + { |
| 174 | + $this->objectManagerMock->expects($this->never()) |
| 175 | + ->method('configure'); |
| 176 | + $this->objectManagerMock->expects($this->never()) |
| 177 | + ->method('get'); |
| 178 | + $this->object->load('unknown part'); |
| 179 | + } |
| 180 | + |
| 181 | + public function testLoad() |
| 182 | + { |
| 183 | + $this->verifyLoadConfig(); |
| 184 | + $this->translatorMock->expects($this->once()) |
| 185 | + ->method('loadData'); |
| 186 | + $renderMock = $this->getMockBuilder('Magento\Framework\Phrase\RendererInterface') |
| 187 | + ->disableOriginalConstructor() |
| 188 | + ->getMock(); |
| 189 | + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') |
| 190 | + ->disableOriginalConstructor() |
| 191 | + ->getMock(); |
| 192 | + $designMock->expects($this->once()) |
| 193 | + ->method('setArea') |
| 194 | + ->with($this->areaCode) |
| 195 | + ->willReturnSelf(); |
| 196 | + $designMock->expects($this->once()) |
| 197 | + ->method('setDefaultDesignTheme'); |
| 198 | + $this->objectManagerMock->expects($this->exactly(2)) |
| 199 | + ->method('get') |
| 200 | + ->will($this->returnValueMap( |
| 201 | + [ |
| 202 | + ['Magento\Framework\Phrase\RendererInterface', $renderMock], |
| 203 | + ['Magento\Framework\View\DesignInterface', $designMock], |
| 204 | + ] |
| 205 | + )); |
| 206 | + $this->object->load(); |
| 207 | + } |
| 208 | + |
| 209 | + private function verifyLoadConfig() |
| 210 | + { |
| 211 | + $configs = ['dummy configs']; |
| 212 | + $this->diConfigLoaderMock->expects($this->once()) |
| 213 | + ->method('load') |
| 214 | + ->with($this->areaCode) |
| 215 | + ->will($this->returnValue($configs)); |
| 216 | + $this->objectManagerMock->expects($this->once()) |
| 217 | + ->method('configure') |
| 218 | + ->with($configs); |
| 219 | + } |
| 220 | + |
| 221 | + public function testDetectDesign() |
| 222 | + { |
| 223 | + $this->designExceptionsMock->expects($this->never()) |
| 224 | + ->method('getThemeByRequest'); |
| 225 | + $this->designMock->expects($this->once()) |
| 226 | + ->method('loadChange') |
| 227 | + ->with(self::SCOPE_ID) |
| 228 | + ->willReturnSelf(); |
| 229 | + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') |
| 230 | + ->disableOriginalConstructor() |
| 231 | + ->getMock(); |
| 232 | + $this->objectManagerMock->expects($this->once()) |
| 233 | + ->method('get') |
| 234 | + ->with('Magento\Framework\View\DesignInterface') |
| 235 | + ->will($this->returnValue($designMock)); |
| 236 | + $this->designMock->expects($this->once()) |
| 237 | + ->method('changeDesign') |
| 238 | + ->with($designMock) |
| 239 | + ->willReturnSelf(); |
| 240 | + $this->object->detectDesign(); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * @param string|bool $value |
| 245 | + * @param int $callNum |
| 246 | + * @param int $callNum2 |
| 247 | + * @dataProvider detectDesignByRequestDataProvider |
| 248 | + */ |
| 249 | + public function testDetectDesignByRequest($value, $callNum, $callNum2) |
| 250 | + { |
| 251 | + $this->designExceptionsMock->expects($this->once()) |
| 252 | + ->method('getThemeByRequest') |
| 253 | + ->will($this->returnValue($value)); |
| 254 | + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') |
| 255 | + ->disableOriginalConstructor() |
| 256 | + ->getMock(); |
| 257 | + $designMock->expects($this->exactly($callNum)) |
| 258 | + ->method('setDesignTheme'); |
| 259 | + $this->objectManagerMock->expects($this->once()) |
| 260 | + ->method('get') |
| 261 | + ->with('Magento\Framework\View\DesignInterface') |
| 262 | + ->will($this->returnValue($designMock)); |
| 263 | + $this->designMock->expects($this->exactly($callNum2)) |
| 264 | + ->method('loadChange') |
| 265 | + ->with(self::SCOPE_ID) |
| 266 | + ->willReturnSelf(); |
| 267 | + $this->designMock->expects($this->exactly($callNum2)) |
| 268 | + ->method('changeDesign') |
| 269 | + ->with($designMock) |
| 270 | + ->willReturnSelf(); |
| 271 | + $requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http') |
| 272 | + ->disableOriginalConstructor() |
| 273 | + ->getMock(); |
| 274 | + $this->object->detectDesign($requestMock); |
| 275 | + } |
| 276 | + |
| 277 | + public function detectDesignByRequestDataProvider() |
| 278 | + { |
| 279 | + return [ |
| 280 | + [false, 0, 1], |
| 281 | + ['theme', 1, 0], |
| 282 | + ]; |
| 283 | + } |
| 284 | + |
| 285 | + public function testDetectDesignByRequestWithException() |
| 286 | + { |
| 287 | + $exception = new \Exception('exception'); |
| 288 | + $this->designExceptionsMock->expects($this->once()) |
| 289 | + ->method('getThemeByRequest') |
| 290 | + ->will($this->throwException($exception)); |
| 291 | + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') |
| 292 | + ->disableOriginalConstructor() |
| 293 | + ->getMock(); |
| 294 | + $designMock->expects($this->never()) |
| 295 | + ->method('setDesignTheme'); |
| 296 | + $this->objectManagerMock->expects($this->once()) |
| 297 | + ->method('get') |
| 298 | + ->with('Magento\Framework\View\DesignInterface') |
| 299 | + ->will($this->returnValue($designMock)); |
| 300 | + $this->designMock->expects($this->once()) |
| 301 | + ->method('loadChange') |
| 302 | + ->with(self::SCOPE_ID) |
| 303 | + ->willReturnSelf(); |
| 304 | + $this->designMock->expects($this->once()) |
| 305 | + ->method('changeDesign') |
| 306 | + ->with($designMock) |
| 307 | + ->willReturnSelf(); |
| 308 | + $requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http') |
| 309 | + ->disableOriginalConstructor() |
| 310 | + ->getMock(); |
| 311 | + $this->loggerMock->expects($this->once()) |
| 312 | + ->method('critical') |
| 313 | + ->with($exception); |
| 314 | + $this->object->detectDesign($requestMock); |
| 315 | + } |
| 316 | +} |
0 commit comments