Skip to content

Commit 86ad181

Browse files
committed
MQE-578: [Unit Test] ActionGroupObject.php
- add new ActionGroupObject test - add new builder util for Action Group object test
1 parent 7bf2b77 commit 86ad181

File tree

2 files changed

+301
-0
lines changed

2 files changed

+301
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace tests\unit\Magento\FunctionalTestFramework\Test\Objects;
8+
9+
use AspectMock\Test as AspectMock;
10+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
11+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
12+
use Magento\FunctionalTestingFramework\Page\Handlers\SectionObjectHandler;
13+
use Magento\FunctionalTestingFramework\Page\Objects\ElementObject;
14+
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
15+
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
16+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
17+
use PHPUnit\Framework\TestCase;
18+
use tests\unit\Util\ActionGroupObjectBuilder;
19+
use tests\unit\Util\EntityDataObjectBuilder;
20+
21+
class ActionGroupObjectTest extends TestCase
22+
{
23+
const ACTION_GROUP_MERGE_KEY = 'testKey';
24+
25+
/**
26+
* Tests a string literal in an action group
27+
*/
28+
public function testGetStepsWithDefaultCase()
29+
{
30+
$entity = (new EntityDataObjectBuilder())
31+
->withDataFields(['field1' => 'testValue'])
32+
->build();
33+
$this->setEntityObjectHandlerReturn($entity);
34+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())->build();
35+
$steps = $actionGroupUnderTest->getSteps(null, self::ACTION_GROUP_MERGE_KEY);
36+
$this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'literal']);
37+
}
38+
39+
/**
40+
* Tests a data reference in an action group, replaced by the user
41+
*/
42+
public function testGetStepsWithCustomArgs()
43+
{
44+
$this->setEntityObjectHandlerReturn(function ($entityName) {
45+
if ($entityName == "data2") {
46+
return (new EntityDataObjectBuilder())->withDataFields(['field2' => 'testValue2'])->build();
47+
}
48+
});
49+
50+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
51+
->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1.field2}}'])])
52+
->build();
53+
54+
$steps = $actionGroupUnderTest->getSteps(['arg1' => 'data2'], self::ACTION_GROUP_MERGE_KEY);
55+
$this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'testValue2']);
56+
}
57+
58+
/**
59+
* Tests a data reference in an action group replaced with a persisted reference.
60+
*/
61+
public function testGetStepsWithPersistedArgs()
62+
{
63+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
64+
->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1.field2}}'])])
65+
->build();
66+
67+
$steps = $actionGroupUnderTest->getSteps(['arg1' => '$data3$'], self::ACTION_GROUP_MERGE_KEY);
68+
$this->assertOnMergeKeyAndActionValue($steps, ['userInput' => '$data3.field2$']);
69+
}
70+
71+
/**
72+
* Tests a data reference in an action group replaced with a data.field reference.
73+
*/
74+
public function testGetStepsWithNoFieldArg()
75+
{
76+
$this->setEntityObjectHandlerReturn(function ($entityName) {
77+
if ($entityName == "data2") {
78+
return (new EntityDataObjectBuilder())->withDataFields(['field2' => 'testValue2'])->build();
79+
}
80+
});
81+
82+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
83+
->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1}}'])])
84+
->build();
85+
86+
$steps = $actionGroupUnderTest->getSteps(['arg1' => 'data2.field2'], self::ACTION_GROUP_MERGE_KEY);
87+
$this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'testValue2']);
88+
}
89+
90+
/**
91+
* Tests a data reference in an action group resolved with its default state.
92+
*/
93+
public function testGetStepsWithNoArgs()
94+
{
95+
$this->setEntityObjectHandlerReturn(function ($entityName) {
96+
if ($entityName == "data1") {
97+
return (new EntityDataObjectBuilder())->withDataFields(['field1' => 'testValue'])->build();
98+
}
99+
});
100+
101+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
102+
->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{data1.field1}}'])])
103+
->build();
104+
105+
$steps = $actionGroupUnderTest->getSteps(null, self::ACTION_GROUP_MERGE_KEY);
106+
$this->assertOnMergeKeyAndActionValue($steps, ['userInput' => 'testValue']);
107+
}
108+
109+
/**
110+
* Tests a parameterized section reference in an action group resolved with user args.
111+
*/
112+
public function testGetStepsWithParameterizedArg()
113+
{
114+
// mock the section object handler response
115+
$element = new ElementObject("element1", "textArea", ".selector {{var1}}", null, null, true);
116+
$section = new SectionObject("testSection", ["element1" => $element]);
117+
// bypass the private constructor
118+
$sectionInstance = AspectMock::double(SectionObjectHandler::class, ['getObject' => $section])->make();
119+
AspectMock::double(SectionObjectHandler::class, ['getInstance' => $sectionInstance]);
120+
121+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
122+
->withActionObjects(
123+
[new ActionObject('action1', 'testAction', ['selector' => '{{section1.element1(arg1.field1)}}'])]
124+
)
125+
->build();
126+
127+
$steps = $actionGroupUnderTest->getSteps(['arg1' => '$someData$'], self::ACTION_GROUP_MERGE_KEY);
128+
$this->assertOnMergeKeyAndActionValue($steps, ['selector' => '.selector $someData.field1$']);
129+
}
130+
131+
/**
132+
* Tests a data reference in an action group resolved with a persisted reference used in another function.
133+
*/
134+
public function testGetStepsWithOuterScopePersistence()
135+
{
136+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
137+
->withActionObjects([new ActionObject('action1', 'testAction', ['userInput' => '{{arg1.field1}}'])])
138+
->build();
139+
140+
$steps = $actionGroupUnderTest->getSteps(['arg1' => '$$someData$$'], self::ACTION_GROUP_MERGE_KEY);
141+
$this->assertOnMergeKeyAndActionValue($steps, ['userInput' => '$$someData.field1$$']);
142+
}
143+
144+
/**
145+
* Tests an action group with mismatching args.
146+
*/
147+
public function testExceptionOnMissingActions()
148+
{
149+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
150+
->withArguments(['arg1' => null])
151+
->build();
152+
153+
$this->expectException(TestReferenceException::class);
154+
$this->expectExceptionMessageRegExp('/Argument\(s\) missed .* for actionGroup/');
155+
$actionGroupUnderTest->getSteps(['arg2' => 'data1'], self::ACTION_GROUP_MERGE_KEY);
156+
}
157+
158+
/**
159+
* Tests an action group with missing args.
160+
*/
161+
public function testExceptionOnMissingArguments()
162+
{
163+
$actionGroupUnderTest = (new ActionGroupObjectBuilder())
164+
->withArguments(['arg1' => null])
165+
->build();
166+
167+
$this->expectException(TestReferenceException::class);
168+
$this->expectExceptionMessageRegExp('/Not enough arguments given for actionGroup .*/');
169+
$actionGroupUnderTest->getSteps(null, self::ACTION_GROUP_MERGE_KEY);
170+
}
171+
172+
/**
173+
* This function takes a desired return for the EntityObjectHandler mock and performs set up of the mock for the
174+
* duration of a single test case.
175+
*
176+
* @param mixed $return
177+
* @return void
178+
*/
179+
private function setEntityObjectHandlerReturn($return)
180+
{
181+
$instance = AspectMock::double(DataObjectHandler::class, ['getObject' => $return])
182+
->make(); // bypass the private constructor
183+
AspectMock::double(DataObjectHandler::class, ['getInstance' => $instance]);
184+
}
185+
186+
/**
187+
* Given a set of steps and an expected custom attribute value, this function performs a set of asserts to validate
188+
* information such as step key and step attribute value.
189+
*
190+
* @param array $actions
191+
* @param array $expectedValue
192+
* @param string $expectedMergeKey
193+
* @return void
194+
*/
195+
private function assertOnMergeKeyAndActionValue($actions, $expectedValue, $expectedMergeKey = null)
196+
{
197+
$expectedMergeKey = $expectedMergeKey ??
198+
ActionGroupObjectBuilder::DEFAULT_ACTION_OBJECT_NAME . self::ACTION_GROUP_MERGE_KEY;
199+
$this->assertArrayHasKey($expectedMergeKey, $actions);
200+
201+
$action = $actions[$expectedMergeKey];
202+
$this->assertEquals($expectedMergeKey, $action->getStepKey());
203+
$this->assertEquals($expectedValue, $action->getCustomActionAttributes());
204+
}
205+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace tests\unit\Util;
8+
9+
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
10+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
11+
12+
class ActionGroupObjectBuilder
13+
{
14+
const DEFAULT_ACTION_OBJECT_NAME = 'action1';
15+
16+
/**
17+
* Action Group Object Builder default name
18+
*
19+
* @var string
20+
*/
21+
private $name = "testActionGroupObject";
22+
23+
/**
24+
* Action Group Object Builder default action objects (set by constructor).
25+
*
26+
* @var array
27+
*/
28+
private $actionObjects = [];
29+
30+
/**
31+
* Action Group Object Builder default arguments.
32+
*
33+
* @var array
34+
*/
35+
private $arguments = ['arg1' => 'data1'];
36+
37+
/**
38+
* Setter for the Action Group Object name
39+
*
40+
* @param string $name
41+
* @return ActionGroupObjectBuilder
42+
*/
43+
public function withName($name)
44+
{
45+
$this->name = $name;
46+
return $this;
47+
}
48+
49+
/**
50+
* Setter for the Action Group Object arguments
51+
*
52+
* @param array $args
53+
* @return ActionGroupObjectBuilder
54+
*/
55+
public function withArguments($args)
56+
{
57+
$this->arguments = $args;
58+
return $this;
59+
}
60+
61+
/**
62+
* Setter for the Action Group Object action objects
63+
*
64+
* @param array $actionObjs
65+
* @return ActionGroupObjectBuilder
66+
*/
67+
public function withActionObjects($actionObjs)
68+
{
69+
$this->actionObjects = $actionObjs;
70+
return $this;
71+
}
72+
73+
/**
74+
* ActionGroupObjectBuilder constructor.
75+
*/
76+
public function __construct()
77+
{
78+
$this->actionObjects = [
79+
new ActionObject(self::DEFAULT_ACTION_OBJECT_NAME, 'testAction', ['userInput' => 'literal'])
80+
];
81+
}
82+
83+
/**
84+
* Function which takes builder parameters and returns a new ActionGroupObject.
85+
*
86+
* @return ActionGroupObject
87+
*/
88+
public function build()
89+
{
90+
return new ActionGroupObject(
91+
$this->name,
92+
$this->arguments,
93+
$this->actionObjects
94+
);
95+
}
96+
}

0 commit comments

Comments
 (0)