4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
- declare (strict_types = 1 );
7
+ declare (strict_types= 1 );
8
8
9
9
namespace Magento \FunctionalTestingFramework \Console ;
10
10
19
19
20
20
class GenerateDevUrnCommand extends Command
21
21
{
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
+
22
30
/**
23
31
* Configures the current command.
24
32
*
@@ -27,8 +35,12 @@ class GenerateDevUrnCommand extends Command
27
35
protected function configure ()
28
36
{
29
37
$ 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
+ )
32
44
->addOption (
33
45
"force " ,
34
46
'f ' ,
@@ -47,7 +59,7 @@ protected function configure()
47
59
*/
48
60
protected function execute (InputInterface $ input , OutputInterface $ output )
49
61
{
50
- $ miscXmlFilePath = $ input ->getArgument (' path ' ) . DIRECTORY_SEPARATOR . " misc.xml " ;
62
+ $ miscXmlFilePath = $ input ->getArgument (self :: IDE_FILE_PATH_ARGUMENT ) ;
51
63
$ miscXmlFile = realpath ($ miscXmlFilePath );
52
64
$ force = $ input ->getOption ('force ' );
53
65
@@ -71,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
71
83
72
84
//Locate ProjectResources node, create one if none are found.
73
85
$ nodeForWork = null ;
74
- foreach ($ dom ->getElementsByTagName ('component ' ) as $ child ) {
86
+ foreach ($ dom ->getElementsByTagName ('component ' ) as $ child ) {
75
87
if ($ child ->getAttribute ('name ' ) === 'ProjectResources ' ) {
76
88
$ nodeForWork = $ child ;
77
89
}
@@ -109,35 +121,74 @@ protected function execute(InputInterface $input, OutputInterface $output)
109
121
110
122
/**
111
123
* Generates urn => location array for all MFTF schema.
124
+ *
112
125
* @return array
113
- * @throws TestFrameworkException
114
126
*/
115
127
private function generateResourcesArray ()
116
128
{
117
129
$ resourcesArray = [
118
130
'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 ' ),
121
132
'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 ' ),
124
134
'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 ' ),
127
136
'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 ' ),
130
138
'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 ' ),
133
140
'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 ' ),
136
142
'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 ' )
139
144
];
140
145
return $ resourcesArray ;
141
146
}
142
147
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
+ }
143
194
}
0 commit comments