-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathTestObjectExtractor.php
160 lines (144 loc) · 5.22 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
<?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\Test\Objects\ActionObject;
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
use Magento\FunctionalTestingFramework\Util\Validation\NameValidationUtil;
/**
* Class TestObjectExtractor
*/
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;
/**
* TestObjectExtractor constructor.
*/
public function __construct()
{
$this->actionObjectExtractor = new ActionObjectExtractor();
$this->annotationExtractor = new AnnotationExtractor();
$this->testHookObjectExtractor = new TestHookObjectExtractor();
}
/**
* 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->extractModuleName($baseFileName);
$testReference = $testData['extends'] ?? 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,
'extends'
);
if (array_key_exists(self::TEST_ANNOTATIONS, $testData)) {
$testAnnotations = $this->annotationExtractor->extractAnnotations($testData[self::TEST_ANNOTATIONS]);
}
//Override features with module name if present, populates it otherwise
$testAnnotations["features"] = [$module];
// 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]
);
}
// 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);
}
}
/**
* Extracts module name from the path given
* @param string $path
* @return string
*/
private function extractModuleName($path)
{
if (empty($path)) {
return "NO MODULE DETECTED";
}
$paths = explode(DIRECTORY_SEPARATOR, $path);
if (count($paths) < 3) {
return "NO MODULE DETECTED";
} elseif ($paths[count($paths)-3] == "Mftf") {
// app/code/Magento/[Analytics]/Test/Mftf/Test/SomeText.xml
return $paths[count($paths)-5];
}
// dev/tests/acceptance/tests/functional/Magento/FunctionalTest/[Analytics]/Test/SomeText.xml
return $paths[count($paths)-3];
}
}