Skip to content

Commit 665b114

Browse files
Manjusha.SManjusha.S
Manjusha.S
authored and
Manjusha.S
committed
fix
1 parent a628e57 commit 665b114

File tree

2 files changed

+217
-2
lines changed

2 files changed

+217
-2
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\StaticCheck;
8+
9+
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
10+
use InvalidArgumentException;
11+
use Exception;
12+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
13+
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
14+
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
15+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
16+
use Magento\FunctionalTestingFramework\Page\Objects\ElementObject;
17+
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
18+
use Magento\FunctionalTestingFramework\Page\Objects\PageObject;
19+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Finder\Finder;
22+
use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil;
23+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler;
24+
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationDefinitionObject;
25+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
26+
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
27+
use Symfony\Component\Finder\SplFileInfo;
28+
use DOMNodeList;
29+
use DOMElement;
30+
31+
/**
32+
* Class CreatedDataFromOutsideActionGroupCheck
33+
* @package Magento\FunctionalTestingFramework\StaticCheck
34+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
35+
*/
36+
class DuplicateStepKeysCheck implements StaticCheckInterface
37+
{
38+
const ERROR_LOG_FILENAME = 'duplicate-step-keys';
39+
const ERROR_LOG_MESSAGE = 'Duplicate Step Key Check';
40+
const STEP_KEY_REGEX_PATTERN = '/stepKey=["\']([^\'"]*)/';
41+
/**
42+
* Array containing all errors found after running the execute() function
43+
*
44+
* @var array
45+
*/
46+
private $errors = [];
47+
48+
/**
49+
* String representing the output summary found after running the execute() function
50+
*
51+
* @var string
52+
*/
53+
private $output;
54+
55+
/**
56+
* ScriptUtil instance
57+
*
58+
* @var ScriptUtil
59+
*/
60+
private $scriptUtil;
61+
62+
/**
63+
* Checks test dependencies, determined by references in tests versus the
64+
* dependencies listed in the Magento module
65+
*
66+
* @param InputInterface $input
67+
* @return void
68+
* @throws Exception
69+
*/
70+
public function execute(InputInterface $input)
71+
{
72+
$this->scriptUtil = new ScriptUtil();
73+
$this->loadAllXmlFiles($input);
74+
$this->errors += $this->findReferenceErrorsInFiles($this->actionGroupXmlFile);
75+
// hold on to the output and print any errors to a file
76+
$this->output = $this->scriptUtil->printErrorsToFile(
77+
$this->errors,
78+
StaticChecksList::getErrorFilesPath() . DIRECTORY_SEPARATOR . self::ERROR_LOG_FILENAME . '.txt',
79+
self::ERROR_LOG_MESSAGE
80+
);
81+
}
82+
83+
/**
84+
* Return array containing all errors found after running the execute() function
85+
*
86+
* @return array
87+
*/
88+
public function getErrors()
89+
{
90+
return $this->errors;
91+
}
92+
93+
/**
94+
* Return string of a short human readable result of the check. For example: "No Dependency errors found."
95+
*
96+
* @return string
97+
*/
98+
public function getOutput()
99+
{
100+
return $this->output;
101+
}
102+
103+
/**
104+
* Read all XML files for scanning
105+
*
106+
* @param InputInterface $input
107+
* @return void
108+
* @throws Exception
109+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
110+
*/
111+
private function loadAllXmlFiles($input)
112+
{
113+
$modulePaths = [];
114+
$path = $input->getOption('path');
115+
if ($path) {
116+
if (!realpath($path)) {
117+
throw new InvalidArgumentException('Invalid --path option: ' . $path);
118+
}
119+
MftfApplicationConfig::create(
120+
true,
121+
MftfApplicationConfig::UNIT_TEST_PHASE,
122+
false,
123+
MftfApplicationConfig::LEVEL_DEFAULT,
124+
true
125+
);
126+
$modulePaths[] = realpath($path);
127+
} else {
128+
$modulePaths = $this->scriptUtil->getAllModulePaths();
129+
}
130+
131+
// These files can contain references to other entities
132+
$this->actionGroupXmlFile = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'ActionGroup');
133+
$this->testXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Test');
134+
$this->suiteXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Suite');
135+
136+
if (empty($this->actionGroupXmlFile)) {
137+
if ($path) {
138+
throw new InvalidArgumentException(
139+
'Invalid --path option: '
140+
. $path
141+
. PHP_EOL
142+
. 'Please make sure --path points to a valid MFTF Test Module.'
143+
);
144+
} elseif (empty($this->rootSuiteXmlFiles)) {
145+
throw new TestFrameworkException('No xml file to scan.');
146+
}
147+
}
148+
}
149+
150+
/**
151+
* Find reference errors in set of action files
152+
*
153+
* @param Finder $files
154+
* @return array
155+
* @throws XmlException
156+
*/
157+
private function findReferenceErrorsInFiles($files)
158+
{
159+
$testErrors = [];
160+
/** @var SplFileInfo $filePath */
161+
foreach ($files as $filePath) {
162+
$actionGroupReferencesDataArray = [];
163+
$contents = file_get_contents($filePath);
164+
preg_match_all(self::STEP_KEY_REGEX_PATTERN, $contents, $actionGroupReferences);
165+
foreach ($actionGroupReferences[0] as $actionGroupReferencesData) {
166+
$actionGroupReferencesDataArray[] = trim(
167+
str_replace(
168+
['stepKey', '='],
169+
[""],
170+
$actionGroupReferencesData
171+
)
172+
).'"';
173+
}
174+
$duplicateStepKeys = array_unique(
175+
array_diff_assoc(
176+
$actionGroupReferencesDataArray,
177+
array_unique(
178+
$actionGroupReferencesDataArray
179+
)
180+
)
181+
);
182+
unset($actionGroupReferencesDataArray);
183+
if (isset($duplicateStepKeys) && count($duplicateStepKeys) > 0) {
184+
$testErrors = array_merge($testErrors, $this->setErrorOutput($duplicateStepKeys, $filePath));
185+
}
186+
}
187+
return $testErrors;
188+
}
189+
190+
/**
191+
* Build and return error output for violating references
192+
*
193+
* @param array $violatingReferences
194+
* @param SplFileInfo $path
195+
* @return mixed
196+
*/
197+
private function setErrorOutput($violatingReferences, $path)
198+
{
199+
$testErrors = [];
200+
201+
$filePath = StaticChecksList::getFilePath($path->getRealPath());
202+
203+
if (!empty($violatingReferences)) {
204+
// Build error output
205+
$errorOutput = "\nFile \"{$filePath}\"";
206+
foreach ($violatingReferences as $stepKey) {
207+
$errorOutput .= "\n\t has duplicate stepKey(s): ". $stepKey;
208+
}
209+
$testErrors[$filePath][] = $errorOutput;
210+
}
211+
return $testErrors;
212+
}
213+
}

src/Magento/FunctionalTestingFramework/StaticCheck/StaticChecksList.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class StaticChecksList implements StaticCheckListInterface
2020
const PAUSE_ACTION_USAGE_CHECK_NAME = 'pauseActionUsage';
2121
const CREATED_DATA_FROM_OUTSIDE_ACTIONGROUP = 'createdDataFromOutsideActionGroup';
2222
const UNUSED_ENTITY_CHECK = 'unusedEntityCheck';
23+
const DUPLICATE_STEP_KEYS = 'duplicateStepKeys';
2324
const STATIC_RESULTS = 'tests' . DIRECTORY_SEPARATOR .'_output' . DIRECTORY_SEPARATOR . 'static-results';
2425

2526
/**
@@ -51,8 +52,9 @@ public function __construct(array $checks = [])
5152
'annotations' => new AnnotationsCheck(),
5253
self::PAUSE_ACTION_USAGE_CHECK_NAME => new PauseActionUsageCheck(),
5354
self::UNUSED_ENTITY_CHECK => new UnusedEntityCheck(),
54-
self::CREATED_DATA_FROM_OUTSIDE_ACTIONGROUP => new CreatedDataFromOutsideActionGroupCheck()
55-
] + $checks;
55+
self::CREATED_DATA_FROM_OUTSIDE_ACTIONGROUP => new CreatedDataFromOutsideActionGroupCheck(),
56+
self::DUPLICATE_STEP_KEYS => new DuplicateStepKeysCheck()
57+
] + $checks;
5658

5759
// Static checks error files directory
5860
if (null === self::$errorFilesPath) {

0 commit comments

Comments
 (0)