Skip to content

Commit 216aeb0

Browse files
authored
MQE-550: [Unit Test] ActionObject.php
- Added unit testing for ActionObject, got coverage to 80%+
2 parents fcf5efa + 28f4597 commit 216aeb0

File tree

1 file changed

+139
-22
lines changed

1 file changed

+139
-22
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Test/Objects/ActionObjectTest.php

Lines changed: 139 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public function testResolveElementInSelector()
5151
'userInput' => 'Hello world'
5252
]);
5353
$elementObject = new ElementObject('elementObject', 'button', '#replacementSelector', null, '42', false);
54-
$sectionObject = new SectionObject('SectionObject', ['elementObject' => $elementObject]);
55-
$instance = AspectMock::double(SectionObjectHandler::class, ['getObject' => $sectionObject])
56-
->make(); // bypass the private constructor
57-
AspectMock::double(SectionObjectHandler::class, ['getInstance' => $instance]);
54+
$this->mockSectionHandlerWithElement($elementObject);
5855

5956
// Call the method under test
6057
$actionObject->resolveReferences();
@@ -68,19 +65,109 @@ public function testResolveElementInSelector()
6865
}
6966

7067
/**
71-
* {{Section.element(param)}} should be replaced
68+
* {{Section.element(param)}} should replace correctly with 'stringLiterals'
7269
*/
73-
public function testResolveSelectorWithOneParam()
70+
public function testResolveSelectorWithOneStringLiteral()
7471
{
75-
$this->markTestIncomplete('TODO');
72+
$actionObject = new ActionObject('key123', 'fillField', [
73+
'selector' => "{{SectionObject.elementObject('stringliteral')}}",
74+
'userInput' => 'Input'
75+
]);
76+
$elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
77+
$this->mockSectionHandlerWithElement($elementObject);
78+
79+
// Call the method under test
80+
$actionObject->resolveReferences();
81+
82+
// Verify
83+
$expected = [
84+
'selector' => '#stringliteral',
85+
'userInput' => 'Input'
86+
];
87+
$this->assertEquals($expected, $actionObject->getCustomActionAttributes());
88+
}
89+
90+
/**
91+
* {{Section.element(param)}} should replace correctly with {{data.key}} references
92+
*/
93+
public function testResolveSelectorWithOneDataReference()
94+
{
95+
$actionObject = new ActionObject('key123', 'fillField', [
96+
'selector' => "{{SectionObject.elementObject(dataObject.key)}}",
97+
'userInput' => 'Input'
98+
]);
99+
100+
// Mock SectionHandler
101+
$elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
102+
$this->mockSectionHandlerWithElement($elementObject);
103+
104+
// Mock DataHandler
105+
$dataObject = new EntityDataObject('dataObject', 'dataType', ["key" => 'myValue'], null, null, null);
106+
$this->mockDataHandlerWithData($dataObject);
107+
108+
// Call the method under test
109+
$actionObject->resolveReferences();
110+
111+
// Verify
112+
$expected = [
113+
'selector' => '#myValue',
114+
'userInput' => 'Input'
115+
];
116+
$this->assertEquals($expected, $actionObject->getCustomActionAttributes());
117+
}
118+
119+
/**
120+
* {{Section.element(param)}} should replace correctly with $data.key$ references
121+
*/
122+
public function testResolveSelectorWithOnePersistedReference()
123+
{
124+
$actionObject = new ActionObject('key123', 'fillField', [
125+
'selector' => '{{SectionObject.elementObject($data.key$)}}',
126+
'userInput' => 'Input'
127+
]);
128+
129+
// Mock SectionHandler
130+
$elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
131+
$this->mockSectionHandlerWithElement($elementObject);
132+
133+
// Call the method under test
134+
$actionObject->resolveReferences();
135+
136+
// Verify
137+
$expected = [
138+
'selector' => '#$data.key$',
139+
'userInput' => 'Input'
140+
];
141+
$this->assertEquals($expected, $actionObject->getCustomActionAttributes());
76142
}
77143

78144
/**
79-
* {{Section.element(param1,param2)}} should be replaced
145+
* {{Section.element(param1,param2,param3)}} should replace correctly with all 3 data types.
80146
*/
81147
public function testResolveSelectorWithManyParams()
82148
{
83-
$this->markTestIncomplete('TODO');
149+
$actionObject = new ActionObject('key123', 'fillField', [
150+
'selector' => "{{SectionObject.elementObject('stringLiteral', data.key, \$data.key\$)}}",
151+
'userInput' => 'Input'
152+
]);
153+
154+
// Mock SectionHandler
155+
$elementObject = new ElementObject('elementObject', 'button', '#{{var1}}[{{var2}},{{var3}}]', null, '42', true);
156+
$this->mockSectionHandlerWithElement($elementObject);
157+
158+
// Mock DataHandler
159+
$dataObject = new EntityDataObject('dataObject', 'dataType', ["key" => 'myValue'], null, null, null);
160+
$this->mockDataHandlerWithData($dataObject);
161+
162+
// Call the method under test
163+
$actionObject->resolveReferences();
164+
165+
// Verify
166+
$expected = [
167+
'selector' => '#stringLiteral[myValue,$data.key$]',
168+
'userInput' => 'Input'
169+
];
170+
$this->assertEquals($expected, $actionObject->getCustomActionAttributes());
84171
}
85172

86173
/**
@@ -93,10 +180,7 @@ public function testTimeoutFromElement()
93180
'selector' => '{{SectionObject.elementObject}}'
94181
]);
95182
$elementObject = new ElementObject('elementObject', 'button', '#replacementSelector', null, '42', false);
96-
$sectionObject = new SectionObject('SectionObject', ['elementObject' => $elementObject]);
97-
$instance = AspectMock::double(SectionObjectHandler::class, ['getObject' => $sectionObject])
98-
->make(); // bypass the private constructor
99-
AspectMock::double(SectionObjectHandler::class, ['getInstance' => $instance]);
183+
$this->mockSectionHandlerWithElement($elementObject);
100184

101185
// Call the method under test
102186
$actionObject->resolveReferences();
@@ -158,9 +242,7 @@ public function testResolveDataInUserInput()
158242
$entityDataObject = new EntityDataObject('EntityDataObject', 'test', [
159243
'key' => 'replacementData'
160244
], [], '', '');
161-
$instance = AspectMock::double(DataObjectHandler::class, ['getObject' => $entityDataObject])
162-
->make(); // bypass the private constructor
163-
AspectMock::double(DataObjectHandler::class, ['getInstance' => $instance]);
245+
$this->mockDataHandlerWithData($entityDataObject);
164246

165247
// Call the method under test
166248
$actionObject->resolveReferences();
@@ -174,18 +256,53 @@ public function testResolveDataInUserInput()
174256
}
175257

176258
/**
177-
* $testScopeData$ (single dollar sign) should be replaced
259+
* Action object should throw an exception if a reference to a parameterized selector has too few given args.
178260
*/
179-
public function testTestScopeDataResolution()
261+
public function testTooFewArgumentException()
180262
{
181-
$this->markTestIncomplete('TODO');
263+
$this->expectException('Magento\FunctionalTestingFramework\Exceptions\TestReferenceException');
264+
265+
$actionObject = new ActionObject('key123', 'fillField', [
266+
'selector' => "{{SectionObject.elementObject('arg1')}}",
267+
'userInput' => 'Input'
268+
]);
269+
$elementObject = new ElementObject('elementObject', 'button', '#{{var1}} {{var2}}', null, '42', true);
270+
$this->mockSectionHandlerWithElement($elementObject);
271+
272+
// Call the method under test
273+
$actionObject->resolveReferences();
182274
}
183275

184276
/**
185-
* $$cestScopeData$$ (double dollar sign) should be replaced
277+
* Action object should throw an exception if a reference to a parameterized selector has too many given args.
186278
*/
187-
public function testCestScopeDataResolution()
279+
public function testTooManyArgumentException()
188280
{
189-
$this->markTestIncomplete('TODO');
281+
$this->expectException('Magento\FunctionalTestingFramework\Exceptions\TestReferenceException');
282+
283+
$actionObject = new ActionObject('key123', 'fillField', [
284+
'selector' => "{{SectionObject.elementObject('arg1', 'arg2', 'arg3')}}",
285+
'userInput' => 'Input'
286+
]);
287+
$elementObject = new ElementObject('elementObject', 'button', '#{{var1}}', null, '42', true);
288+
$this->mockSectionHandlerWithElement($elementObject);
289+
290+
// Call the method under test
291+
$actionObject->resolveReferences();
292+
}
293+
294+
private function mockSectionHandlerWithElement($elementObject)
295+
{
296+
$sectionObject = new SectionObject('SectionObject', ['elementObject' => $elementObject]);
297+
$instance = AspectMock::double(SectionObjectHandler::class, ['getObject' => $sectionObject])
298+
->make(); // bypass the private constructor
299+
AspectMock::double(SectionObjectHandler::class, ['getInstance' => $instance]);
300+
}
301+
302+
private function mockDataHandlerWithData($dataObject)
303+
{
304+
$dataInstance = AspectMock::double(DataObjectHandler::class, ['getObject' => $dataObject])
305+
->make();
306+
AspectMock::double(DataObjectHandler::class, ['getInstance' => $dataInstance]);
190307
}
191308
}

0 commit comments

Comments
 (0)