-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathScriptUtil.php
341 lines (310 loc) · 11.9 KB
/
ScriptUtil.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util\Script;
use Exception;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\Page\Objects\ElementObject;
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
use Symfony\Component\Finder\Finder;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Util\ModuleResolver;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
use Magento\FunctionalTestingFramework\Page\Handlers\PageObjectHandler;
use Magento\FunctionalTestingFramework\Page\Handlers\SectionObjectHandler;
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
use Magento\FunctionalTestingFramework\Util\TestGenerator;
/**
* ScriptUtil class that contains helper functions for static and upgrade scripts
*
* @package Magento\FunctionalTestingFramework\Util\Script
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ScriptUtil
{
const ACTIONGROUP_ARGUMENT_REGEX_PATTERN = '/<argument[^\/>]*name="([^"\']*)/';
const ROOT_SUITE_DIR = 'tests/_suite';
const DEV_TESTS_DIR = 'dev/tests/acceptance/';
/**
* Return all installed Magento module paths
*
* @return array
* @throws TestFrameworkException
*/
public function getAllModulePaths(): array
{
MftfApplicationConfig::create(
true,
MftfApplicationConfig::UNIT_TEST_PHASE,
false,
MftfApplicationConfig::LEVEL_DEFAULT,
true
);
return ModuleResolver::getInstance()->getModulesPath();
}
/**
* Prints out given errors to file, and returns summary result string
* @param array $errors
* @param string $filePath
* @param string $message
* @return string
*/
public function printErrorsToFile(array $errors, string $filePath, string $message): string
{
if (empty($errors)) {
return $message . ": No errors found.";
}
$this->printTofile($errors, $filePath);
$errorCount = count($errors);
return $message . ": Errors found across {$errorCount} file(s). Error details output to {$filePath}";
}
/**
* Prints out given warnings to file, and returns summary result string
* @param array $warnings
* @param string $filePath
* @param string $message
* @return string
*/
public function printWarningsToFile(array $warnings, string $filePath, string $message): string
{
if (empty($warnings)) {
return $message . ": No warnings found.";
}
$this->printTofile($warnings, $filePath);
$errorCount = count($warnings);
return $message . ": Warnings found across {$errorCount} file(s). Warning details output to {$filePath}";
}
/**
* Writes contents to filePath
* @param array $contents
* @param string $filePath
* @return void
*/
private function printTofile(array $contents, string $filePath)
{
$dirname = dirname($filePath);
if (!file_exists($dirname)) {
mkdir($dirname, 0777, true);
}
$fileResource = fopen($filePath, 'w');
foreach ($contents as $test => $error) {
fwrite($fileResource, $error[0] . PHP_EOL);
}
fclose($fileResource);
}
/**
* Return all XML files for $scope in given module paths, empty array if no path is valid
*
* @param array $modulePaths
* @param string $scope
* @return Finder|array
*/
public function getModuleXmlFilesByScope(array $modulePaths, string $scope)
{
$found = false;
$scopePath = DIRECTORY_SEPARATOR . ucfirst($scope) . DIRECTORY_SEPARATOR;
$finder = new Finder();
foreach ($modulePaths as $modulePath) {
if (!realpath($modulePath . $scopePath)) {
continue;
}
$finder->files()->followLinks()->in($modulePath . $scopePath)->name("*.xml")->sortByName();
$found = true;
}
return $found ? $finder->files() : [];
}
/**
* Return suite XML files in TESTS_BP/ROOT_SUITE_DIR directory
*
* @return Finder|array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getRootSuiteXmlFiles()
{
$rootSuitePaths = [];
$defaultTestPath = null;
$devTestsPath = null;
try {
$defaultTestPath = FilePathFormatter::format(TESTS_BP);
} catch (TestFrameworkException $e) {
}
try {
$devTestsPath = FilePathFormatter::format(MAGENTO_BP) . self::DEV_TESTS_DIR;
} catch (TestFrameworkException $e) {
}
if ($defaultTestPath) {
$rootSuitePaths[] = $defaultTestPath . self::ROOT_SUITE_DIR;
}
if ($devTestsPath && realpath($devTestsPath) && $devTestsPath !== $defaultTestPath) {
$rootSuitePaths[] = $devTestsPath . self::ROOT_SUITE_DIR;
}
$found = false;
$finder = new Finder();
foreach ($rootSuitePaths as $rootSuitePath) {
if (!realpath($rootSuitePath)) {
continue;
}
$finder->files()->followLinks()->in($rootSuitePath)->name("*.xml");
$found = true;
}
return $found ? $finder->files() : [];
}
/**
* Resolve entity reference in {{entity.field}} or {{entity.field('param')}}
*
* @param array $braceReferences
* @param string $contents
* @param boolean $resolveSectionElement
* @return array
* @throws XmlException
*/
public function resolveEntityReferences($braceReferences, $contents, $resolveSectionElement = false)
{
$entities = [];
foreach ($braceReferences as $reference) {
// trim `{{data.field}}` to `data`
preg_match('/{{([^.]+)/', $reference, $entityName);
// Double check that {{data.field}} isn't an argument for an ActionGroup
$entity = $this->findEntity($entityName[1]);
preg_match_all(self::ACTIONGROUP_ARGUMENT_REGEX_PATTERN, $contents, $possibleArgument);
if (array_search($entityName[1], $possibleArgument[1]) !== false) {
continue;
}
if ($entity !== null) {
$entities[$entity->getName()] = $entity;
if ($resolveSectionElement) {
if (get_class($entity) === SectionObject::class) {
// trim `{{data.field}}` to `field`
preg_match('/.([^.]+)}}/', $reference, $elementName);
/** @var ElementObject $element */
$element = $entity->getElement($elementName[1]);
if ($element) {
$entities[$entity->getName() . '.' . $elementName[1]] = $element;
}
}
}
}
}
return $entities;
}
/**
* Drill down into params in {{ref.params('string', $data.key$, entity.reference)}} to resolve entity reference
*
* @param array $braceReferences
* @param string $contents
* @param boolean $resolveSectionElement
* @return array
* @throws XmlException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function resolveParametrizedReferences($braceReferences, $contents, $resolveSectionElement = false): array
{
$entities = [];
foreach ($braceReferences as $parameterizedReference) {
preg_match(
ActionObject::ACTION_ATTRIBUTE_VARIABLE_REGEX_PARAMETER,
$parameterizedReference,
$arguments
);
$splitArguments = explode(',', ltrim(rtrim($arguments[0], ")"), "("));
foreach ($splitArguments as $argument) {
// Do nothing for 'string' or $persisted.data$
if (preg_match(ActionObject::STRING_PARAMETER_REGEX, $argument)) {
continue;
} elseif (preg_match(TestGenerator::PERSISTED_OBJECT_NOTATION_REGEX, $argument)) {
continue;
}
// trim `data.field` to `data`
preg_match('/([^.]+)/', $argument, $entityName);
// Double check that {{data.field}} isn't an argument for an ActionGroup
$entity = $this->findEntity($entityName[1]);
preg_match_all(self::ACTIONGROUP_ARGUMENT_REGEX_PATTERN, $contents, $possibleArgument);
if (array_search($entityName[1], $possibleArgument[1]) !== false) {
continue;
}
if ($entity !== null) {
$entities[$entity->getName()] = $entity;
if ($resolveSectionElement) {
if (get_class($entity) === SectionObject::class) {
// trim `data.field` to `field`
preg_match('/.([^.]+)/', $argument, $elementName);
/** @var ElementObject $element */
$element = $entity->getElement($elementName[1]);
if ($element) {
$entities[$entity->getName() . '.' . $elementName[1]] = $element;
}
}
}
}
}
}
return $entities;
}
/**
* Resolve entity by names
*
* @param array $references
* @return array
* @throws XmlException
*/
public function resolveEntityByNames(array $references): array
{
$entities = [];
foreach ($references as $reference) {
$entity = $this->findEntity($reference);
if ($entity !== null) {
$entities[$entity->getName()] = $entity;
}
}
return $entities;
}
/**
* Attempts to find any MFTF entity by its name. Returns null if none are found
*
* @param string $name
* @return mixed
* @throws XmlException
* @throws Exception
*/
public function findEntity(string $name)
{
if ($name === '_ENV' || $name === '_CREDS') {
return null;
}
if (DataObjectHandler::getInstance()->getObject($name)) {
return DataObjectHandler::getInstance()->getObject($name);
} elseif (PageObjectHandler::getInstance()->getObject($name)) {
return PageObjectHandler::getInstance()->getObject($name);
} elseif (SectionObjectHandler::getInstance()->getObject($name)) {
return SectionObjectHandler::getInstance()->getObject($name);
} elseif (ActionGroupObjectHandler::getInstance()->getObject($name)) {
return ActionGroupObjectHandler::getInstance()->getObject($name);
}
try {
return TestObjectHandler::getInstance()->getObject($name);
} catch (TestReferenceException $e) {
}
return null;
}
/**
* Return all XML files in given test name, empty array if no path is valid
* @param array $testNames
* @return array|Finder
*/
public function getModuleXmlFilesByTestNames(array $testNames)
{
$finder = new Finder();
array_walk($testNames, function (&$value) {
$value = $value . ".xml";
});
$finder->files()->followLinks()->in(MAGENTO_BP)->name($testNames)->sortByName();
return $finder->files() ?? [];
}
}