Skip to content

Commit 65bd46c

Browse files
authored
Merge pull request #570 from lbajsarowicz/bugfix/568-urn-catalog
#568 Project-based URN path generation + add consistency of misc.xml path
2 parents d82e6cf + a734abb commit 65bd46c

File tree

1 file changed

+71
-20
lines changed

1 file changed

+71
-20
lines changed

src/Magento/FunctionalTestingFramework/Console/GenerateDevUrnCommand.php

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7-
declare(strict_types = 1);
7+
declare(strict_types=1);
88

99
namespace Magento\FunctionalTestingFramework\Console;
1010

@@ -19,6 +19,14 @@
1919

2020
class GenerateDevUrnCommand extends Command
2121
{
22+
/**
23+
* Argument for the path to IDE config file
24+
*/
25+
const IDE_FILE_PATH_ARGUMENT = 'path';
26+
27+
const PROJECT_PATH_IDENTIFIER = '$PROJECT_DIR$';
28+
const MFTF_SRC_PATH = 'src/Magento/FunctionalTestingFramework/';
29+
2230
/**
2331
* Configures the current command.
2432
*
@@ -27,8 +35,12 @@ class GenerateDevUrnCommand extends Command
2735
protected function configure()
2836
{
2937
$this->setName('generate:urn-catalog')
30-
->setDescription('This command generates an URN catalog to enable PHPStorm to recognize and highlight URNs.')
31-
->addArgument('path', InputArgument::REQUIRED, 'path to PHPStorm misc.xml file (typically located in [ProjectRoot]/.idea/misc.xml)')
38+
->setDescription('Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml.')
39+
->addArgument(
40+
self::IDE_FILE_PATH_ARGUMENT,
41+
InputArgument::REQUIRED,
42+
'Path to file to output the catalog. For PhpStorm use .idea/misc.xml'
43+
)
3244
->addOption(
3345
"force",
3446
'f',
@@ -47,7 +59,7 @@ protected function configure()
4759
*/
4860
protected function execute(InputInterface $input, OutputInterface $output)
4961
{
50-
$miscXmlFilePath = $input->getArgument('path') . DIRECTORY_SEPARATOR . "misc.xml";
62+
$miscXmlFilePath = $input->getArgument(self::IDE_FILE_PATH_ARGUMENT);
5163
$miscXmlFile = realpath($miscXmlFilePath);
5264
$force = $input->getOption('force');
5365

@@ -71,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7183

7284
//Locate ProjectResources node, create one if none are found.
7385
$nodeForWork = null;
74-
foreach($dom->getElementsByTagName('component') as $child) {
86+
foreach ($dom->getElementsByTagName('component') as $child) {
7587
if ($child->getAttribute('name') === 'ProjectResources') {
7688
$nodeForWork = $child;
7789
}
@@ -109,35 +121,74 @@ protected function execute(InputInterface $input, OutputInterface $output)
109121

110122
/**
111123
* Generates urn => location array for all MFTF schema.
124+
*
112125
* @return array
113-
* @throws TestFrameworkException
114126
*/
115127
private function generateResourcesArray()
116128
{
117129
$resourcesArray = [
118130
'urn:magento:mftf:DataGenerator/etc/dataOperation.xsd' =>
119-
realpath(FilePathFormatter::format(FW_BP)
120-
. 'src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataOperation.xsd'),
131+
$this->getResourcePath('DataGenerator/etc/dataOperation.xsd'),
121132
'urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd' =>
122-
realpath(FilePathFormatter::format(FW_BP)
123-
. 'src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataProfileSchema.xsd'),
133+
$this->getResourcePath('DataGenerator/etc/dataProfileSchema.xsd'),
124134
'urn:magento:mftf:Page/etc/PageObject.xsd' =>
125-
realpath(FilePathFormatter::format(FW_BP)
126-
. 'src/Magento/FunctionalTestingFramework/Page/etc/PageObject.xsd'),
135+
$this->getResourcePath('Page/etc/PageObject.xsd'),
127136
'urn:magento:mftf:Page/etc/SectionObject.xsd' =>
128-
realpath(FilePathFormatter::format(FW_BP)
129-
. 'src/Magento/FunctionalTestingFramework/Page/etc/SectionObject.xsd'),
137+
$this->getResourcePath('Page/etc/SectionObject.xsd'),
130138
'urn:magento:mftf:Test/etc/actionGroupSchema.xsd' =>
131-
realpath(FilePathFormatter::format(FW_BP)
132-
. 'src/Magento/FunctionalTestingFramework/Test/etc/actionGroupSchema.xsd'),
139+
$this->getResourcePath('Test/etc/actionGroupSchema.xsd'),
133140
'urn:magento:mftf:Test/etc/testSchema.xsd' =>
134-
realpath(FilePathFormatter::format(FW_BP)
135-
. 'src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd'),
141+
$this->getResourcePath('Test/etc/testSchema.xsd'),
136142
'urn:magento:mftf:Suite/etc/suiteSchema.xsd' =>
137-
realpath(FilePathFormatter::format(FW_BP)
138-
. 'src/Magento/FunctionalTestingFramework/Suite/etc/suiteSchema.xsd')
143+
$this->getResourcePath('Suite/etc/suiteSchema.xsd')
139144
];
140145
return $resourcesArray;
141146
}
142147

148+
/**
149+
* Returns path (full or PhpStorm project-based) to XSD file
150+
*
151+
* @param $relativePath
152+
* @return string
153+
* @throws TestFrameworkException
154+
*/
155+
private function getResourcePath($relativePath)
156+
{
157+
$urnPath = realpath(FilePathFormatter::format(FW_BP) . self::MFTF_SRC_PATH . $relativePath);
158+
$projectRoot = $this->getProjectRootPath();
159+
160+
if ($projectRoot !== null) {
161+
return str_replace($projectRoot, self::PROJECT_PATH_IDENTIFIER, $urnPath);
162+
}
163+
164+
return $urnPath;
165+
}
166+
167+
/**
168+
* Returns Project root directory absolute path
169+
* @TODO Find out how to detect other types of installation
170+
*
171+
* @return string|null
172+
*/
173+
private function getProjectRootPath()
174+
{
175+
$frameworkRoot = realpath(__DIR__);
176+
177+
if ($this->isInstalledByComposer($frameworkRoot)) {
178+
return strstr($frameworkRoot, '/vendor/', true);
179+
}
180+
181+
return null;
182+
}
183+
184+
/**
185+
* Determines whether MFTF was installed using Composer
186+
*
187+
* @param string $frameworkRoot
188+
* @return bool
189+
*/
190+
private function isInstalledByComposer($frameworkRoot)
191+
{
192+
return false !== strpos($frameworkRoot, '/vendor/');
193+
}
143194
}

0 commit comments

Comments
 (0)