Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ActionObjectTest.php
Go to the documentation of this file.
1 <?php
8 
9 use AspectMock\Test as AspectMock;
21 
26 {
31  public function setUp()
32  {
33  TestLoggingUtil::getInstance()->setMockLoggingUtil();
34  }
35 
39  public function testConstructOrderBefore()
40  {
41  $actionObject = new ActionObject('stepKey', 'type', [], null, 'before');
42  $this->assertEquals(0, $actionObject->getOrderOffset());
43  }
44 
48  public function testConstructOrderAfter()
49  {
50  $actionObject = new ActionObject('stepKey', 'type', [], null, 'after');
51  $this->assertEquals(1, $actionObject->getOrderOffset());
52  }
53 
57  public function testResolveElementInSelector()
58  {
59  // Set up mocks
60  $actionObject = new ActionObject('merge123', 'fillField', [
61  'selector' => '{{SectionObject.elementObject}}',
62  'userInput' => 'Hello world'
63  ]);
64  $elementObject = new ElementObject('elementObject', 'button', '#replacementSelector', null, '42', false);
65  $this->mockSectionHandlerWithElement($elementObject);
66 
67  // Call the method under test
68  $actionObject->resolveReferences();
69 
70  // Verify
71  $expected = [
72  'selector' => '#replacementSelector',
73  'userInput' => 'Hello world'
74  ];
75  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
76  }
77 
82  {
83  $actionObject = new ActionObject('key123', 'fillField', [
84  'selector' => "{{SectionObject.elementObject('stringliteral')}}",
85  'userInput' => 'Input'
86  ]);
87  $elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
88  $this->mockSectionHandlerWithElement($elementObject);
89 
90  // Call the method under test
91  $actionObject->resolveReferences();
92 
93  // Verify
94  $expected = [
95  'selector' => '#stringliteral',
96  'userInput' => 'Input'
97  ];
98  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
99  }
100 
105  {
106  $actionObject = new ActionObject('key123', 'fillField', [
107  'selector' => "{{SectionObject.elementObject(dataObject.key)}}",
108  'userInput' => 'Input'
109  ]);
110 
111  // Mock SectionHandler
112  $elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
113  $this->mockSectionHandlerWithElement($elementObject);
114 
115  // Mock DataHandler
116  $dataObject = new EntityDataObject('dataObject', 'dataType', ["key" => 'myValue'], null, null, null);
117  $this->mockDataHandlerWithData($dataObject);
118 
119  // Call the method under test
120  $actionObject->resolveReferences();
121 
122  // Verify
123  $expected = [
124  'selector' => '#myValue',
125  'userInput' => 'Input'
126  ];
127  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
128  }
129 
134  {
135  $actionObject = new ActionObject('key123', 'fillField', [
136  'selector' => '{{SectionObject.elementObject($data.key$)}}',
137  'userInput' => 'Input'
138  ]);
139 
140  // Mock SectionHandler
141  $elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
142  $this->mockSectionHandlerWithElement($elementObject);
143 
144  // Call the method under test
145  $actionObject->resolveReferences();
146 
147  // Verify
148  $expected = [
149  'selector' => '#$data.key$',
150  'userInput' => 'Input'
151  ];
152  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
153  }
154 
159  {
160  $actionObject = new ActionObject('key123', 'fillField', [
161  'selector' => "{{SectionObject.elementObject('stringLiteral', data.key, \$data.key\$)}}",
162  'userInput' => 'Input'
163  ]);
164 
165  // Mock SectionHandler
166  $elementObject = new ElementObject('elementObject', 'button', '#{{var1}}[{{var2}},{{var3}}]', null, '42', true);
167  $this->mockSectionHandlerWithElement($elementObject);
168 
169  // Mock DataHandler
170  $dataObject = new EntityDataObject('dataObject', 'dataType', ["key" => 'myValue'], null, null, null);
171  $this->mockDataHandlerWithData($dataObject);
172 
173  // Call the method under test
174  $actionObject->resolveReferences();
175 
176  // Verify
177  $expected = [
178  'selector' => '#stringLiteral[myValue,$data.key$]',
179  'userInput' => 'Input'
180  ];
181  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
182  }
183 
187  public function testTimeoutFromElement()
188  {
189  // Set up mocks
190  $actionObject = new ActionObject('merge123', 'click', [
191  'selector' => '{{SectionObject.elementObject}}'
192  ]);
193  $elementObject = new ElementObject('elementObject', 'button', '#replacementSelector', null, '42', false);
194  $this->mockSectionHandlerWithElement($elementObject);
195 
196  // Call the method under test
197  $actionObject->resolveReferences();
198 
199  // Verify
200  $this->assertEquals(42, $actionObject->getTimeout());
201  }
202 
208  public function testResolveUrl()
209  {
210  // Set up mocks
211  $actionObject = new ActionObject('merge123', 'amOnPage', [
212  'url' => '{{PageObject.url}}'
213  ]);
214  $pageObject = new PageObject('PageObject', '/replacement/url.html', 'Test', [], false, "test");
215  $instance = AspectMock::double(PageObjectHandler::class, ['getObject' => $pageObject])
216  ->make(); // bypass the private constructor
217  AspectMock::double(PageObjectHandler::class, ['getInstance' => $instance]);
218 
219  // Call the method under test
220  $actionObject->resolveReferences();
221 
222  // Verify
223  $expected = [
224  'url' => '/replacement/url.html'
225  ];
226  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
227  }
228 
235  {
236  // Set up mocks
237  $actionObject = new ActionObject('merge123', 'amOnPage', [
238  'url' => '{{PageObject}}'
239  ]);
240  $pageObject = new PageObject('PageObject', '/replacement/url.html', 'Test', [], false, "test");
241  $pageObjectList = ["PageObject" => $pageObject];
242  $instance = AspectMock::double(
243  PageObjectHandler::class,
244  ['getObject' => $pageObject, 'getAllObjects' => $pageObjectList]
245  )->make(); // bypass the private constructor
246  AspectMock::double(PageObjectHandler::class, ['getInstance' => $instance]);
247 
248  // Call the method under test
249  $actionObject->resolveReferences();
250 
251  // Expect this warning to get generated
252  TestLoggingUtil::getInstance()->validateMockLogStatement(
253  "warning",
254  "page url attribute not found and is required",
255  ['action' => $actionObject->getType(), 'url' => '{{PageObject}}', 'stepKey' => $actionObject->getStepKey()]
256  );
257 
258  // Verify
259  $expected = [
260  'url' => '{{PageObject}}'
261  ];
262  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
263  }
264 
268  public function testResolveUrlWithOneParam()
269  {
270  $this->markTestIncomplete('TODO');
271  }
272 
277  {
278  $this->markTestIncomplete('TODO');
279  }
280 
284  public function testResolveDataInUserInput()
285  {
286  // Set up mocks
287  $actionObject = new ActionObject('merge123', 'fillField', [
288  'selector' => '#selector',
289  'userInput' => '{{EntityDataObject.key}}'
290  ]);
291  $entityDataObject = new EntityDataObject('EntityDataObject', 'test', [
292  'key' => 'replacementData'
293  ], [], '', '');
294  $this->mockDataHandlerWithData($entityDataObject);
295 
296  // Call the method under test
297  $actionObject->resolveReferences();
298 
299  // Verify
300  $expected = [
301  'selector' => '#selector',
302  'userInput' => 'replacementData'
303  ];
304  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
305  }
306 
310  public function testResolveArrayData()
311  {
312  // Set up mocks
313  $actionObject = new ActionObject('merge123', 'fillField', [
314  'selector' => '#selector',
315  'userInput' => '{{EntityDataObject.values}}'
316  ]);
317  $entityDataObject = new EntityDataObject('EntityDataObject', 'test', [
318  'values' => [
319  'value1',
320  'value2',
321  '"My" Value'
322  ]
323  ], [], '', '');
324  $this->mockDataHandlerWithData($entityDataObject);
325 
326  // Call the method under test
327  $actionObject->resolveReferences();
328 
329  // Verify
330  $expected = [
331  'selector' => '#selector',
332  'userInput' => '["value1","value2","\"My\" Value"]'
333  ];
334  $this->assertEquals($expected, $actionObject->getCustomActionAttributes());
335  }
336 
340  public function testTooFewArgumentException()
341  {
342  $this->expectException(TestReferenceException::class);
343 
344  $actionObject = new ActionObject('key123', 'fillField', [
345  'selector' => "{{SectionObject.elementObject('arg1')}}",
346  'userInput' => 'Input'
347  ]);
348  $elementObject = new ElementObject('elementObject', 'button', '#{{var1}} {{var2}}', null, '42', true);
349  $this->mockSectionHandlerWithElement($elementObject);
350 
351  // Call the method under test
352  $actionObject->resolveReferences();
353  }
354 
359  {
360  $this->expectException(TestReferenceException::class);
361 
362  $actionObject = new ActionObject('key123', 'fillField', [
363  'selector' => "{{SectionObject.elementObject('arg1', 'arg2', 'arg3')}}",
364  'userInput' => 'Input'
365  ]);
366  $elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
367  $this->mockSectionHandlerWithElement($elementObject);
368 
369  // Call the method under test
370  $actionObject->resolveReferences();
371  }
372 
377  {
378  $this->expectException(TestReferenceException::class);
379 
380  $actionObject = new ActionObject('key123', 'generateDate', [
381  'timezone' => "INVALID_TIMEZONE"
382  ]);
383 
384  // Call the method under test
385  $actionObject->resolveReferences();
386  }
387 
388  private function mockSectionHandlerWithElement($elementObject)
389  {
390  $sectionObject = new SectionObject('SectionObject', ['elementObject' => $elementObject]);
391  $instance = AspectMock::double(SectionObjectHandler::class, ['getObject' => $sectionObject])
392  ->make(); // bypass the private constructor
393  AspectMock::double(SectionObjectHandler::class, ['getInstance' => $instance]);
394  }
395 
396  private function mockDataHandlerWithData($dataObject)
397  {
398  $dataInstance = AspectMock::double(DataObjectHandler::class, ['getObject' => $dataObject])
399  ->make();
400  AspectMock::double(DataObjectHandler::class, ['getInstance' => $dataInstance]);
401  }
402 
407  public static function tearDownAfterClass()
408  {
409  TestLoggingUtil::getInstance()->clearMockLoggingUtil();
410  }
411 }