Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LoadOptionsTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Widget\Helper\Conditions as ConditionsHelper;
17 
21 class LoadOptionsTest extends \PHPUnit\Framework\TestCase
22 {
26  private $objectManagerHelper;
27 
31  private $contextMock;
32 
36  private $viewMock;
37 
41  private $conditionsHelperMock;
42 
46  private $responseMock;
47 
51  private $objectManagerMock;
52 
56  private $requestMock;
57 
61  private $loadOptions;
62 
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 
108  public function dtestExecuteWithException()
109  {
110  $jsonResult = '{"error":true,"message":"Some error"}';
111  $errorMessage = 'Some error';
112 
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 
140  public function testExecute()
141  {
142  $widgetType = 'Magento\SomeWidget';
143  $conditionsEncoded = 'encoded conditions';
144  $conditionsDecoded = [
145  'value' => 1,
146  'operator' => '==',
147  'attribute' => 'id',
148  ];
149  $widgetJsonParams = '{"widget_type":"Magento\\Widget","values":{"title":"&quot;Test&quot;", "":}}';
150  $widgetArrayParams = [
151  'widget_type' => $widgetType,
152  'values' => [
153  'title' => '&quot;Test&quot;',
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 
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 
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 
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 }
__()
Definition: __.php:13