Skip to content

Commit 586fd5c

Browse files
authored
Merge pull request #95 from magento/MQE-809
MQE-809: Throw a warning or error when step key referencing in merges is invalid or ambiguous
2 parents bd43c81 + 9d03330 commit 586fd5c

File tree

8 files changed

+348
-34
lines changed

8 files changed

+348
-34
lines changed

dev/tests/_bootstrap.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
'includePaths' => [PROJECT_ROOT . '/src']
1717
]);
1818

19-
// force php to generate
20-
$GLOBALS['FORCE_PHP_GENERATE'] = true;
19+
// set mftf appplication context
20+
\Magento\FunctionalTestingFramework\Config\MftfApplicationConfig::create(
21+
true,
22+
\Magento\FunctionalTestingFramework\Config\MftfApplicationConfig::GENERATION_PHASE,
23+
true
24+
);
2125

2226
// Load needed framework env params
2327
$TEST_ENVS = [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;
7+
8+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
9+
use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class ActionObjectExtractorTest extends TestCase
13+
{
14+
/** @var ActionObjectExtractor */
15+
private $testActionObjectExtractor;
16+
17+
/**
18+
* Setup method
19+
*/
20+
public function setUp()
21+
{
22+
$this->testActionObjectExtractor = new ActionObjectExtractor();
23+
}
24+
25+
/**
26+
* Tests basic action object extraction with a valid parser array.
27+
*/
28+
public function testBasicActionObjectExtration()
29+
{
30+
$actionObjects = $this->testActionObjectExtractor->extractActions($this->createBasicActionObjectArray());
31+
$this->assertCount(1, $actionObjects);
32+
33+
/** @var ActionObject $firstElement */
34+
$firstElement = array_values($actionObjects)[0];
35+
$this->assertEquals('testAction1', $firstElement->getStepKey());
36+
$this->assertCount(1, $firstElement->getCustomActionAttributes());
37+
}
38+
39+
/**
40+
* Tests an invalid merge order reference (i.e. a step referencing itself).
41+
*/
42+
public function testInvalidMergeOrderReference()
43+
{
44+
$invalidArray = $this->createBasicActionObjectArray('invalidTestAction1', 'invalidTestAction1');
45+
46+
$this->expectException('\Magento\FunctionalTestingFramework\Exceptions\TestReferenceException');
47+
$expectedExceptionMessage = "Invalid ordering configuration in test TestWithSelfReferencingStepKey with step" .
48+
" key(s):\n\tinvalidTestAction1\n";
49+
$this->expectExceptionMessage($expectedExceptionMessage);
50+
51+
$this->testActionObjectExtractor->extractActions($invalidArray, 'TestWithSelfReferencingStepKey');
52+
}
53+
54+
/**
55+
* Validates a warning is printed to the console when multiple actions reference the same actions for merging.
56+
*/
57+
public function testAmbiguousMergeOrderRefernece()
58+
{
59+
$ambiguousArray = $this->createBasicActionObjectArray('testAction1');
60+
$ambiguousArray = array_merge(
61+
$ambiguousArray,
62+
$this->createBasicActionObjectArray('testAction2', 'testAction1')
63+
);
64+
65+
$ambiguousArray = array_merge(
66+
$ambiguousArray,
67+
$this->createBasicActionObjectArray('testAction3', null, 'testAction1')
68+
);
69+
70+
$outputString = "multiple actions referencing step key testAction1 in test AmbiguousRefTest:\n" .
71+
"\ttestAction2\n" .
72+
"\ttestAction3\n";
73+
74+
$this->expectOutputString($outputString);
75+
$this->testActionObjectExtractor->extractActions($ambiguousArray, 'AmbiguousRefTest');
76+
}
77+
78+
/**
79+
* Utility function to return mock parser output for testing extraction into ActionObjects.
80+
*
81+
* @param string $stepKey
82+
* @param string $before
83+
* @param string $after
84+
* @return array
85+
*/
86+
private function createBasicActionObjectArray($stepKey = 'testAction1', $before = null, $after = null)
87+
{
88+
$baseArray = [
89+
$stepKey => [
90+
"nodeName" => "sampleAction",
91+
"stepKey" => $stepKey,
92+
"someAttribute" => "someAttributeValue"
93+
]
94+
];
95+
96+
if ($before) {
97+
$baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['before' => $before]);
98+
}
99+
100+
if ($after) {
101+
$baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['after' => $after]);
102+
}
103+
104+
return $baseArray;
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\FunctionalTestingFramework\Config;
7+
8+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
9+
10+
class MftfApplicationConfig
11+
{
12+
const GENERATION_PHASE = "generation";
13+
const EXECUTION_PHASE = "execution";
14+
const MFTF_PHASES = [self::GENERATION_PHASE, self::EXECUTION_PHASE];
15+
16+
/**
17+
* Determines whether the user has specified a force option for generation
18+
*
19+
* @var bool
20+
*/
21+
private $forceGenerate;
22+
23+
/**
24+
* String which identifies the current phase of mftf execution
25+
*
26+
* @var string
27+
*/
28+
private $phase;
29+
30+
/**
31+
* Determines whether the user would like to execute mftf in a verbose run.
32+
*
33+
* @var bool
34+
*/
35+
private $verboseEnabled;
36+
37+
/**
38+
* MftfApplicationConfig Singelton Instance
39+
*
40+
* @var MftfApplicationConfig
41+
*/
42+
private static $MFTF_APPLICATION_CONTEXT;
43+
44+
/**
45+
* MftfApplicationConfig constructor.
46+
*
47+
* @param bool $forceGenerate
48+
* @param string $phase
49+
* @param bool $verboseEnabled
50+
* @throws TestFrameworkException
51+
*/
52+
private function __construct($forceGenerate = false, $phase = self::EXECUTION_PHASE, $verboseEnabled = null)
53+
{
54+
$this->forceGenerate = $forceGenerate;
55+
56+
if (!in_array($phase, self::MFTF_PHASES)) {
57+
throw new TestFrameworkException("{$phase} is not an mftf phase");
58+
}
59+
60+
$this->phase = $phase;
61+
$this->verboseEnabled = $verboseEnabled;
62+
}
63+
64+
/**
65+
* Creates an instance of the configuration instance for reference once application has started. This function
66+
* returns void and is only run once during the lifetime of the application.
67+
*
68+
* @param bool $forceGenerate
69+
* @param string $phase
70+
* @param bool $verboseEnabled
71+
* @return void
72+
*/
73+
public static function create($forceGenerate, $phase, $verboseEnabled)
74+
{
75+
if (self::$MFTF_APPLICATION_CONTEXT == null) {
76+
self::$MFTF_APPLICATION_CONTEXT = new MftfApplicationConfig($forceGenerate, $phase, $verboseEnabled);
77+
}
78+
}
79+
80+
/**
81+
* This function returns an instance of the MftfApplicationConfig which is created once the application starts.
82+
*
83+
* @return MftfApplicationConfig
84+
*/
85+
public static function getConfig()
86+
{
87+
// TODO explicitly set this with AcceptanceTester or MagentoWebDriver
88+
// during execution we cannot guarantee the use of the robofile so we return the default application config,
89+
// we don't want to set the application context in case the user explicitly does so at a later time.
90+
if (self::$MFTF_APPLICATION_CONTEXT == null) {
91+
return new MftfApplicationConfig();
92+
}
93+
94+
return self::$MFTF_APPLICATION_CONTEXT;
95+
}
96+
97+
/**
98+
* Returns a booelan indiciating whether or not the user has indicated a forced generation.
99+
*
100+
* @return bool
101+
*/
102+
public function forceGenerateEnabled()
103+
{
104+
return $this->forceGenerate;
105+
}
106+
107+
/**
108+
* Returns a boolean indicating whether the user has indicated a verbose run, which will cause all applicable
109+
* text to print to the console.
110+
*
111+
* @return bool
112+
*/
113+
public function verboseEnabled()
114+
{
115+
return $this->verboseEnabled ?? getenv('MFTF_DEBUG');
116+
}
117+
118+
/**
119+
* Returns a string which indicates the phase of mftf execution.
120+
*
121+
* @return string
122+
*/
123+
public function getPhase()
124+
{
125+
return $this->phase;
126+
}
127+
}

src/Magento/FunctionalTestingFramework/Test/Objects/ActionObject.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\FunctionalTestingFramework\Test\Objects;
77

8+
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
89
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
910
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
1011
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
@@ -264,7 +265,8 @@ public function trimAssertionAttributes()
264265
$oldAttributes = array_intersect($actionAttributeKeys, ActionObject::OLD_ASSERTION_ATTRIBUTES);
265266
if (!empty($oldAttributes)) {
266267
// @codingStandardsIgnoreStart
267-
if ($GLOBALS['GENERATE_TESTS'] ?? false == true) {
268+
$appConfig = MftfApplicationConfig::getConfig();
269+
if ($appConfig->getPhase() == MftfApplicationConfig::GENERATION_PHASE && $appConfig->verboseEnabled()) {
268270
echo("WARNING: Use of one line Assertion actions will be deprecated in MFTF 3.0.0, please use nested syntax (Action: {$this->type} StepKey: {$this->stepKey})" . PHP_EOL);
269271
}
270272
// @codingStandardsIgnoreEnd

0 commit comments

Comments
 (0)