forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestObjectExtractor.php
213 lines (190 loc) · 7.12 KB
/
TestObjectExtractor.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Test\Util;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\Page\Objects\ElementObject;
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Magento\FunctionalTestingFramework\Util\ModulePathExtractor;
use Magento\FunctionalTestingFramework\Util\Validation\NameValidationUtil;
/**
* Class TestObjectExtractor
* @SuppressWarnings(PHPMD)
*/
class TestObjectExtractor extends BaseObjectExtractor
{
const TEST_ANNOTATIONS = 'annotations';
const TEST_BEFORE_HOOK = 'before';
const TEST_AFTER_HOOK = 'after';
const TEST_FAILED_HOOK = 'failed';
const TEST_BEFORE_ATTRIBUTE = 'before';
const TEST_AFTER_ATTRIBUTE = 'after';
const TEST_INSERT_BEFORE = 'insertBefore';
const TEST_INSERT_AFTER = 'insertAfter';
const TEST_FILENAME = 'filename';
/**
* Action Object Extractor object
*
* @var ActionObjectExtractor
*/
private $actionObjectExtractor;
/**
* Annotation Extractor object
*
* @var AnnotationExtractor
*/
private $annotationExtractor;
/**
* Test Hook Object extractor
*
* @var TestHookObjectExtractor
*/
private $testHookObjectExtractor;
/**
* Module Path extractor
*
* @var ModulePathExtractor
*/
private $modulePathExtractor;
/**
* TestObjectExtractor constructor.
*/
public function __construct()
{
$this->actionObjectExtractor = new ActionObjectExtractor();
$this->annotationExtractor = new AnnotationExtractor();
$this->testHookObjectExtractor = new TestHookObjectExtractor();
$this->modulePathExtractor = new ModulePathExtractor();
}
/**
* Getter for AnnotationExtractor
* @return AnnotationExtractor
*/
public function getAnnotationExtractor()
{
return $this->annotationExtractor;
}
/**
* This method takes and array of test data and strips away irrelevant tags. The data is converted into an array of
* TestObjects.
*
* @param array $testData
* @return TestObject
* @throws \Exception
*/
public function extractTestData($testData)
{
// validate the test name for blacklisted char (will cause allure report issues) MQE-483
NameValidationUtil::validateName($testData[self::NAME], "Test");
$testAnnotations = [];
$testHooks = [];
$filename = $testData['filename'] ?? null;
$fileNames = explode(",", $filename);
$baseFileName = $fileNames[0];
$module = $this->modulePathExtractor->extractModuleName($baseFileName);
$testReference = $testData['extends'] ?? null;
$deprecated = isset($testData[self::OBJ_DEPRECATED]) ? $testData[self::OBJ_DEPRECATED] : null;
$testActions = $this->stripDescriptorTags(
$testData,
self::NODE_NAME,
self::NAME,
self::TEST_ANNOTATIONS,
self::TEST_BEFORE_HOOK,
self::TEST_AFTER_HOOK,
self::TEST_FAILED_HOOK,
self::TEST_INSERT_BEFORE,
self::TEST_INSERT_AFTER,
self::TEST_FILENAME,
self::OBJ_DEPRECATED,
'extends'
);
$testAnnotations = $this->annotationExtractor->extractAnnotations(
$testData[self::TEST_ANNOTATIONS] ?? [],
$testData[self::NAME]
);
//Override features with module name if present, populates it otherwise
$testAnnotations["features"] = [$module];
// Always try to append test file names in description annotation, i.e. displaying test files title only
// when $fileNames is not available
if (!isset($testAnnotations["description"])) {
$testAnnotations["description"] = [];
} else {
$testAnnotations["description"]['main'] = $testAnnotations["description"][0];
unset($testAnnotations["description"][0]);
}
$testAnnotations["description"]['test_files'] = $this->appendFileNamesInDescriptionAnnotation($fileNames);
$testAnnotations["description"][self::OBJ_DEPRECATED] = [];
if ($deprecated !== null) {
$testAnnotations["description"][self::OBJ_DEPRECATED][] = $deprecated;
LoggingUtil::getInstance()->getLogger(TestObject::class)->deprecation(
$deprecated,
["testName" => $filename, "deprecatedTest" => $deprecated]
);
}
// extract before
if (array_key_exists(self::TEST_BEFORE_HOOK, $testData) && is_array($testData[self::TEST_BEFORE_HOOK])) {
$testHooks[self::TEST_BEFORE_HOOK] = $this->testHookObjectExtractor->extractHook(
$testData[self::NAME],
'before',
$testData[self::TEST_BEFORE_HOOK]
);
}
if (array_key_exists(self::TEST_AFTER_HOOK, $testData) && is_array($testData[self::TEST_AFTER_HOOK])) {
// extract after
$testHooks[self::TEST_AFTER_HOOK] = $this->testHookObjectExtractor->extractHook(
$testData[self::NAME],
'after',
$testData[self::TEST_AFTER_HOOK]
);
// extract failed
$testHooks[self::TEST_FAILED_HOOK] = $this->testHookObjectExtractor->createDefaultFailedHook(
$testData[self::NAME]
);
}
if (!empty($testData[self::OBJ_DEPRECATED])) {
$testAnnotations[self::OBJ_DEPRECATED] = $testData[self::OBJ_DEPRECATED];
}
// TODO extract filename info and store
try {
return new TestObject(
$testData[self::NAME],
$this->actionObjectExtractor->extractActions($testActions, $testData[self::NAME]),
$testAnnotations,
$testHooks,
$filename,
$testReference
);
} catch (XmlException $exception) {
throw new XmlException($exception->getMessage() . ' in Test ' . $filename);
}
}
/**
* Append names of test files, including merge files, in description annotation
*
* @param array $fileNames
*
* @return string
*/
private function appendFileNamesInDescriptionAnnotation(array $fileNames)
{
$filePaths = '<h3>Test files</h3>';
foreach ($fileNames as $fileName) {
if (!empty($fileName) && realpath($fileName) !== false) {
$fileName = realpath($fileName);
$relativeFileName = ltrim(
str_replace(MAGENTO_BP, '', $fileName),
DIRECTORY_SEPARATOR
);
if (!empty($relativeFileName)) {
$filePaths .= $relativeFileName . '<br>';
}
}
}
return $filePaths;
}
}