|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Widget\Test\Unit\Controller\Adminhtml\Widget; |
| 7 | + |
| 8 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 9 | +use Magento\Widget\Controller\Adminhtml\Widget\LoadOptions; |
| 10 | +use Magento\Backend\App\Action\Context; |
| 11 | +use Magento\Framework\App\ViewInterface; |
| 12 | +use Magento\Widget\Helper\Conditions as ConditionsHelper; |
| 13 | +use Magento\Framework\Exception\LocalizedException; |
| 14 | +use Magento\Framework\App\ResponseInterface; |
| 15 | +use Magento\Framework\ObjectManagerInterface; |
| 16 | +use Magento\Framework\App\RequestInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Test class for \Magento\Widget\Controller\Adminhtml\Widget\LoadOptions |
| 20 | + */ |
| 21 | +class LoadOptionsTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ObjectManagerHelper |
| 25 | + */ |
| 26 | + private $objectManagerHelper; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + private $contextMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ViewInterface|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + private $viewMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ConditionsHelper|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + private $conditionsHelperMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + private $responseMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 50 | + */ |
| 51 | + private $objectManagerMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 55 | + */ |
| 56 | + private $requestMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var LoadOptions |
| 60 | + */ |
| 61 | + private $loadOptions; |
| 62 | + |
| 63 | + /** |
| 64 | + * return void |
| 65 | + */ |
| 66 | + protected function setUp() |
| 67 | + { |
| 68 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 69 | + $this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class); |
| 70 | + $this->viewMock = $this->getMockForAbstractClass(ViewInterface::class); |
| 71 | + $this->requestMock = $this->getMockForAbstractClass(RequestInterface::class); |
| 72 | + $this->responseMock = $this->getMockBuilder(ResponseInterface::class) |
| 73 | + ->setMethods(['representJson']) |
| 74 | + ->getMockForAbstractClass(); |
| 75 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + $this->contextMock->expects($this->once()) |
| 79 | + ->method('getView') |
| 80 | + ->willReturn($this->viewMock); |
| 81 | + $this->contextMock->expects($this->once()) |
| 82 | + ->method('getRequest') |
| 83 | + ->willReturn($this->requestMock); |
| 84 | + $this->contextMock->expects($this->once()) |
| 85 | + ->method('getResponse') |
| 86 | + ->willReturn($this->responseMock); |
| 87 | + $this->contextMock->expects($this->once()) |
| 88 | + ->method('getObjectManager') |
| 89 | + ->willReturn($this->objectManagerMock); |
| 90 | + $this->conditionsHelperMock = $this->getMockBuilder(ConditionsHelper::class) |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->getMock(); |
| 93 | + |
| 94 | + $this->loadOptions = $this->objectManagerHelper->getObject( |
| 95 | + LoadOptions::class, |
| 96 | + ['context' => $this->contextMock] |
| 97 | + ); |
| 98 | + $this->objectManagerHelper->setBackwardCompatibleProperty( |
| 99 | + $this->loadOptions, |
| 100 | + 'conditionsHelper', |
| 101 | + $this->conditionsHelperMock |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return void |
| 107 | + */ |
| 108 | + public function dtestExecuteWithException() |
| 109 | + { |
| 110 | + $jsonResult = '{"error":true,"message":"Some error"}'; |
| 111 | + $errorMessage = 'Some error'; |
| 112 | + |
| 113 | + /** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject $jsonDataHelperMock */ |
| 114 | + $jsonDataHelperMock = $this->getMockBuilder(\Magento\Framework\Json\Helper\Data::class) |
| 115 | + ->disableOriginalConstructor() |
| 116 | + ->getMock(); |
| 117 | + $jsonDataHelperMock->expects($this->once()) |
| 118 | + ->method('jsonEncode') |
| 119 | + ->with(['error' => true, 'message' => $errorMessage]) |
| 120 | + ->willReturn($jsonResult); |
| 121 | + |
| 122 | + $this->viewMock->expects($this->once()) |
| 123 | + ->method('loadLayout') |
| 124 | + ->willThrowException(new LocalizedException(__($errorMessage))); |
| 125 | + $this->objectManagerMock->expects($this->once()) |
| 126 | + ->method('get') |
| 127 | + ->with(\Magento\Framework\Json\Helper\Data::class) |
| 128 | + ->willReturn($jsonDataHelperMock); |
| 129 | + $this->responseMock->expects($this->once()) |
| 130 | + ->method('representJson') |
| 131 | + ->with($jsonResult) |
| 132 | + ->willReturnArgument(0); |
| 133 | + |
| 134 | + $this->loadOptions->execute(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @return void |
| 139 | + */ |
| 140 | + public function testExecute() |
| 141 | + { |
| 142 | + $widgetType = 'Magento\SomeWidget'; |
| 143 | + $conditionsEncoded = 'a:3:{s:5:"value";i:1;s:8:"operator";s:2:"==";s:9:"attribute";s:2:"id";}'; |
| 144 | + $conditionsDecoded = [ |
| 145 | + 'value' => 1, |
| 146 | + 'operator' => '==', |
| 147 | + 'attribute' => 'id', |
| 148 | + ]; |
| 149 | + $widgetJsonParams = '{"widget_type":"Magento\\Widget","values":{"title":""Test"", "":}}'; |
| 150 | + $widgetArrayParams = [ |
| 151 | + 'widget_type' => $widgetType, |
| 152 | + 'values' => [ |
| 153 | + 'title' => '"Test"', |
| 154 | + 'conditions_encoded' => $conditionsEncoded, |
| 155 | + ], |
| 156 | + ]; |
| 157 | + $resultWidgetArrayParams = [ |
| 158 | + 'widget_type' => $widgetType, |
| 159 | + 'values' => [ |
| 160 | + 'title' => '"Test"', |
| 161 | + 'conditions_encoded' => $conditionsEncoded, |
| 162 | + 'conditions' => $conditionsDecoded, |
| 163 | + ], |
| 164 | + ]; |
| 165 | + |
| 166 | + /** @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject $jsonDataHelperMock */ |
| 167 | + $jsonDataHelperMock = $this->getMockBuilder(\Magento\Framework\Json\Helper\Data::class) |
| 168 | + ->disableOriginalConstructor() |
| 169 | + ->getMock(); |
| 170 | + $jsonDataHelperMock->expects($this->once()) |
| 171 | + ->method('jsonDecode') |
| 172 | + ->with($widgetJsonParams) |
| 173 | + ->willReturn($widgetArrayParams); |
| 174 | + |
| 175 | + $this->viewMock->expects($this->once()) |
| 176 | + ->method('loadLayout'); |
| 177 | + $this->requestMock->expects($this->once()) |
| 178 | + ->method('getParam') |
| 179 | + ->with('widget') |
| 180 | + ->willReturn($widgetJsonParams); |
| 181 | + $this->objectManagerMock->expects($this->once()) |
| 182 | + ->method('get') |
| 183 | + ->with(\Magento\Framework\Json\Helper\Data::class) |
| 184 | + ->willReturn($jsonDataHelperMock); |
| 185 | + |
| 186 | + /** @var \Magento\Framework\View\Element\BlockInterface|\PHPUnit_Framework_MockObject_MockObject $blockMock */ |
| 187 | + $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class) |
| 188 | + ->setMethods(['setWidgetType', 'setWidgetValues']) |
| 189 | + ->getMockForAbstractClass(); |
| 190 | + $blockMock->expects($this->once()) |
| 191 | + ->method('setWidgetType') |
| 192 | + ->with($widgetType) |
| 193 | + ->willReturnSelf(); |
| 194 | + $blockMock->expects($this->once()) |
| 195 | + ->method('setWidgetValues') |
| 196 | + ->with($resultWidgetArrayParams['values']) |
| 197 | + ->willReturnSelf(); |
| 198 | + |
| 199 | + /** @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject $layoutMock */ |
| 200 | + $layoutMock = $this->getMockForAbstractClass(\Magento\Framework\View\LayoutInterface::class); |
| 201 | + $layoutMock->expects($this->once()) |
| 202 | + ->method('getBlock') |
| 203 | + ->with('wysiwyg_widget.options') |
| 204 | + ->willReturn($blockMock); |
| 205 | + |
| 206 | + $this->conditionsHelperMock->expects($this->once()) |
| 207 | + ->method('decode') |
| 208 | + ->with($conditionsEncoded) |
| 209 | + ->willReturn($conditionsDecoded); |
| 210 | + $this->viewMock->expects($this->once()) |
| 211 | + ->method('getLayout') |
| 212 | + ->willReturn($layoutMock); |
| 213 | + $this->viewMock->expects($this->once()) |
| 214 | + ->method('renderLayout'); |
| 215 | + |
| 216 | + $this->loadOptions->execute(); |
| 217 | + } |
| 218 | +} |
0 commit comments