Skip to content

Commit 9a0bb13

Browse files
committed
MQE-809: Throw a warning or error when step key referencing in merges is invalid or ambiguous
- add validation to ambiguous action object ref during merge - add validtion to invalid action object ref during merge - add unit test around action object extraction
1 parent bd43c81 commit 9a0bb13

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: imeron
5+
* Date: 4/10/18
6+
* Time: 11:39 AM
7+
*/
8+
9+
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;
10+
11+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
12+
use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class ActionObjectExtractorTest extends TestCase
16+
{
17+
/** @var ActionObjectExtractor */
18+
private $testActionObjectExtractor;
19+
20+
/**
21+
* Setup method
22+
*/
23+
public function setUp()
24+
{
25+
$this->testActionObjectExtractor = new ActionObjectExtractor();
26+
}
27+
28+
/**
29+
* Tests basic action object extraction with a valid parser array.
30+
*/
31+
public function testBasicActionObjectExtration()
32+
{
33+
$actionObjects = $this->testActionObjectExtractor->extractActions($this->createBasicActionObjectArray());
34+
$this->assertCount(1, $actionObjects);
35+
36+
/** @var ActionObject $firstElement */
37+
$firstElement = array_values($actionObjects)[0];
38+
$this->assertEquals('testAction1', $firstElement->getStepKey());
39+
$this->assertCount(1, $firstElement->getCustomActionAttributes());
40+
}
41+
42+
/**
43+
* Tests an invalid merge order reference (i.e. a step referencing itself).
44+
*/
45+
public function testInvalidMergeOrderReference()
46+
{
47+
$invalidArray = $this->createBasicActionObjectArray('invalidTestAction1', 'invalidTestAction1');
48+
49+
$this->expectException('\Magento\FunctionalTestingFramework\Exceptions\TestReferenceException');
50+
$expectedExceptionMessage = "Invalid ordering configuration in test TestWithSelfReferencingStepKey with step" .
51+
" key(s):\n\tinvalidTestAction1\n";
52+
$this->expectExceptionMessage($expectedExceptionMessage);
53+
54+
$this->testActionObjectExtractor->extractActions($invalidArray, 'TestWithSelfReferencingStepKey');
55+
}
56+
57+
/**
58+
* Validates a warning is printed to the console when multiple actions reference the same actions for merging.
59+
*/
60+
public function testAmbiguousMergeOrderRefernece()
61+
{
62+
$ambiguousArray = $this->createBasicActionObjectArray('testAction1');
63+
$ambiguousArray = array_merge(
64+
$ambiguousArray,
65+
$this->createBasicActionObjectArray('testAction2', 'testAction1')
66+
);
67+
68+
$ambiguousArray = array_merge(
69+
$ambiguousArray,
70+
$this->createBasicActionObjectArray('testAction3', null, 'testAction1')
71+
);
72+
73+
$outputString = "multiple actions referencing step key testAction1 in test AmbiguousRefTest:\n" .
74+
"\ttestAction2\n" .
75+
"\ttestAction3\n";
76+
77+
$this->expectOutputString($outputString);
78+
$this->testActionObjectExtractor->extractActions($ambiguousArray, 'AmbiguousRefTest');
79+
}
80+
81+
/**
82+
* Utility function to return mock parser output for testing extraction into ActionObjects.
83+
*
84+
* @param string $stepKey
85+
* @param string $before
86+
* @param string $after
87+
* @return array
88+
*/
89+
private function createBasicActionObjectArray($stepKey = 'testAction1', $before = null, $after = null)
90+
{
91+
$baseArray = [
92+
$stepKey => [
93+
"nodeName" => "sampleAction",
94+
"stepKey" => $stepKey,
95+
"someAttribute" => "someAttributeValue"
96+
]
97+
];
98+
99+
if ($before) {
100+
$baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['before' => $before]);
101+
}
102+
103+
if ($after) {
104+
$baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['after' => $after]);
105+
}
106+
107+
return $baseArray;
108+
}
109+
}

src/Magento/FunctionalTestingFramework/Test/Util/ActionObjectExtractor.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\FunctionalTestingFramework\Test\Util;
88

9+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
10+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
911
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
1012
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1113

@@ -42,12 +44,14 @@ public function __construct()
4244
* irrelevant tags and returned as an array of ActionObjects.
4345
*
4446
* @param array $testActions
47+
* @param string $testName
4548
* @return array
4649
* @throws XmlException
4750
*/
48-
public function extractActions($testActions)
51+
public function extractActions($testActions, $testName = null)
4952
{
5053
$actions = [];
54+
$stepKeyRefs = [];
5155

5256
foreach ($testActions as $actionName => $actionData) {
5357
$stepKey = $actionData[self::TEST_STEP_MERGE_KEY];
@@ -89,6 +93,10 @@ public function extractActions($testActions)
8993
$actions = $this->extractFieldActions($actionData, $actions);
9094
$actionAttributes = $this->extractFieldReferences($actionData, $actionAttributes);
9195

96+
if ($linkedAction != null) {
97+
$stepKeyRefs[$linkedAction][] = $stepKey;
98+
}
99+
92100
// TODO this is to be implemented later. Currently the schema does not use or need return var.
93101
/*if (array_key_exists(ActionGroupObjectHandler::TEST_ACTION_RETURN_VARIABLE, $actionData)) {
94102
$returnVariable = $actionData[ActionGroupObjectHandler::TEST_ACTION_RETURN_VARIABLE];
@@ -103,6 +111,8 @@ public function extractActions($testActions)
103111
);
104112
}
105113

114+
$this->auditMergeSteps($stepKeyRefs, $testName);
115+
106116
return $actions;
107117
}
108118

@@ -194,4 +204,45 @@ private function extractFieldReferences($actionData, $actionAttributes)
194204

195205
return $attributes;
196206
}
207+
208+
/**
209+
* Function which validates stepKey references within mergeable actions
210+
*
211+
* @param array $stepKeyRefs
212+
* @param string $testName
213+
* @return void
214+
* @throws TestReferenceException
215+
*/
216+
private function auditMergeSteps($stepKeyRefs, $testName)
217+
{
218+
if (empty($stepKeyRefs)) {
219+
return;
220+
}
221+
222+
// check for step keys which are referencing themselves as before/after
223+
$invalidStepRef = array_filter($stepKeyRefs, function ($value, $key) {
224+
return in_array($key, $value);
225+
}, ARRAY_FILTER_USE_BOTH);
226+
227+
if (!empty($invalidStepRef)) {
228+
$errorMsg = "Invalid ordering configuration in test {$testName} with step key(s):\n";
229+
array_walk($invalidStepRef, function ($value, $key) use (&$errorMsg) {
230+
$errorMsg.="\t{$key}\n";
231+
});
232+
233+
throw new TestReferenceException($errorMsg);
234+
}
235+
236+
// check for ambiguous references to step keys (multiple refs across test merges).
237+
$atRiskStepRef = array_filter($stepKeyRefs, function ($value) {
238+
return count($value) > 1;
239+
});
240+
241+
foreach ($atRiskStepRef as $stepKey => $stepRefs) {
242+
print "multiple actions referencing step key {$stepKey} in test {$testName}:\n";
243+
array_walk($stepRefs, function ($value) {
244+
print "\t{$value}\n";
245+
});
246+
}
247+
}
197248
}

src/Magento/FunctionalTestingFramework/Test/Util/TestObjectExtractor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace Magento\FunctionalTestingFramework\Test\Util;
88

9+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
910
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
11+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1012
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
1113
use Magento\FunctionalTestingFramework\Util\Validation\NameValidationUtil;
1214

@@ -109,7 +111,7 @@ public function extractTestData($testData)
109111
try {
110112
return new TestObject(
111113
$testData[self::NAME],
112-
$this->actionObjectExtractor->extractActions($testActions),
114+
$this->actionObjectExtractor->extractActions($testActions, $testData[self::NAME]),
113115
$testAnnotations,
114116
$testHooks,
115117
$filename

0 commit comments

Comments
 (0)