-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathTestObjectHandler.php
290 lines (267 loc) · 10 KB
/
TestObjectHandler.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Test\Handlers;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Exceptions\FastFailException;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser;
use Magento\FunctionalTestingFramework\Test\Util\ObjectExtensionUtil;
use Magento\FunctionalTestingFramework\Test\Util\TestObjectExtractor;
use Magento\FunctionalTestingFramework\Util\GenerationErrorHandler;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Magento\FunctionalTestingFramework\Util\Validation\NameValidationUtil;
/**
* Class TestObjectHandler
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TestObjectHandler implements ObjectHandlerInterface
{
const XML_ROOT = 'tests';
const TEST_FILENAME_ATTRIBUTE = 'filename';
/**
* Test Object Handler
*
* @var TestObjectHandler
*/
private static $testObjectHandler;
/**
* Array contains all test objects indexed by name
*
* @var TestObject[] $tests
*/
private $tests = [];
/**
* Instance of ObjectExtensionUtil class
*
* @var ObjectExtensionUtil
*/
private $extendUtil;
/**
* Singleton method to return TestObjectHandler.
*
* @return TestObjectHandler
* @throws FastFailException
* @throws TestFrameworkException
*/
public static function getInstance($validateAnnotations = true)
{
if (!self::$testObjectHandler) {
self::$testObjectHandler = new TestObjectHandler();
self::$testObjectHandler->initTestData($validateAnnotations);
}
return self::$testObjectHandler;
}
/**
* TestObjectHandler constructor.
*/
private function __construct()
{
$this->extendUtil = new ObjectExtensionUtil();
}
/**
* Takes a test name and returns the corresponding test.
*
* @param string $testName
* @return TestObject
* @throws TestReferenceException
*/
public function getObject($testName)
{
if (!array_key_exists($testName, $this->tests)) {
throw new TestReferenceException("Test ${testName} not defined in xml.");
}
$testObject = $this->tests[$testName];
return $this->extendTest($testObject);
}
/**
* Returns all tests parsed from xml indexed by testName.
*
* @return array
* @throws FastFailException
* @throws TestFrameworkException
*/
public function getAllObjects()
{
$errCount = 0;
$testObjects = [];
foreach ($this->tests as $testName => $test) {
try {
$testObjects[$testName] = $this->extendTest($test);
} catch (FastFailException $exception) {
throw $exception;
} catch (\Exception $exception) {
$errCount++;
LoggingUtil::getInstance()->getLogger(self::class)->error(
"Unable to extend test " . $testName . "\n" . $exception->getMessage()
);
if (MftfApplicationConfig::getConfig()->getPhase() != MftfApplicationConfig::EXECUTION_PHASE) {
GenerationErrorHandler::getInstance()->addError(
'test',
$testName,
self::class . ': Unable to extend test ' . $exception->getMessage()
);
}
}
}
if ($errCount > 0
&& MftfApplicationConfig::getConfig()->getPhase() == MftfApplicationConfig::GENERATION_PHASE) {
print(
"ERROR: "
. strval($errCount)
. " Test(s) cannot to be extended in TestObjectHandler::getAllObjects(). See mftf.log for details."
);
}
return $testObjects;
}
/**
* Returns tests tagged with the group name passed to the method.
*
* @param string $groupName
* @return TestObject[]
* @throws FastFailException
* @throws TestFrameworkException
*/
public function getTestsByGroup($groupName)
{
$errCount = 0;
$relevantTests = [];
foreach ($this->tests as $test) {
try {
/** @var TestObject $test */
if (in_array($groupName, $test->getAnnotationByName('group'))) {
$relevantTests[$test->getName()] = $this->extendTest($test);
}
} catch (FastFailException $exception) {
throw $exception;
} catch (\Exception $exception) {
$errCount++;
$message = "Unable to reference test "
. $test->getName()
. " for group {$groupName}\n"
. $exception->getMessage();
LoggingUtil::getInstance()->getLogger(self::class)->error($message);
if (MftfApplicationConfig::getConfig()->getPhase() != MftfApplicationConfig::EXECUTION_PHASE) {
GenerationErrorHandler::getInstance()->addError(
'test',
$test->getName(),
self::class . ': ' . $message
);
}
}
}
if ($errCount > 0
&& MftfApplicationConfig::getConfig()->getPhase() == MftfApplicationConfig::GENERATION_PHASE) {
print(
"ERROR: "
. strval($errCount)
. " Test(s) cannot be referenced for group {$groupName} in TestObjectHandler::getTestsByGroup()."
. " See mftf.log for details."
);
}
return $relevantTests;
}
/**
* Sanitize test objects
*
* @param array $testsToRemove
* @return void
*/
public function sanitizeTests($testsToRemove)
{
foreach ($testsToRemove as $name) {
unset($this->tests[$name]);
LoggingUtil::getInstance()->getLogger(self::class)->error(
"Removed invalid test object {$name}"
);
}
}
/**
* This method reads all Test.xml files into objects and stores them in an array for future access.
*
* @param boolean $validateAnnotations
* @return void
* @throws FastFailException
* @throws TestFrameworkException
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function initTestData($validateAnnotations = true)
{
$parserErrorMessage = null;
try {
$testDataParser = ObjectManagerFactory::getObjectManager()->create(TestDataParser::class);
$parsedTestArray = $testDataParser->readTestData();
if (!$parsedTestArray) {
$parserErrorMessage = "Could not parse any test in xml.";
}
} catch (\Exception $e) {
$parserErrorMessage = $e->getMessage();
}
if ($parserErrorMessage) {
throw new FastFailException("Test Data Parser Error: " . $parserErrorMessage);
}
$testObjectExtractor = new TestObjectExtractor();
$testNameValidator = new NameValidationUtil();
foreach ($parsedTestArray as $testName => $testData) {
try {
$filename = $testData[TestObjectHandler::TEST_FILENAME_ATTRIBUTE];
$testNameValidator->validatePascalCase($testName, NameValidationUtil::TEST_NAME, $filename);
if (!is_array($testData)) {
continue;
}
$this->tests[$testName] = $testObjectExtractor->extractTestData($testData, $validateAnnotations);
} catch (FastFailException $exception) {
throw $exception;
} catch (\Exception $exception) {
LoggingUtil::getInstance()->getLogger(self::class)->error(
"Unable to parse test " . $testName . "\n" . $exception->getMessage()
);
if (MftfApplicationConfig::getConfig()->getPhase() == MftfApplicationConfig::GENERATION_PHASE) {
print("ERROR: Unable to parse test " . $testName . "\n");
}
if (MftfApplicationConfig::getConfig()->getPhase() != MftfApplicationConfig::EXECUTION_PHASE) {
GenerationErrorHandler::getInstance()->addError(
'test',
$testName,
self::class . ': Unable to parse test ' . $exception->getMessage()
);
}
}
}
$testNameValidator->summarize(NameValidationUtil::TEST_NAME);
if ($validateAnnotations) {
$testObjectExtractor->getAnnotationExtractor()->validateStoryTitleUniqueness();
$testObjectExtractor->getAnnotationExtractor()->validateTestCaseIdTitleUniqueness();
}
}
/**
* This method checks if the test is extended and creates a new test object accordingly
*
* @param TestObject $testObject
* @return TestObject
* @throws TestFrameworkException
* @throws XmlException
*/
private function extendTest($testObject)
{
if ($testObject->getParentName() !== null) {
if ($testObject->getParentName() == $testObject->getName()) {
throw new TestFrameworkException(
"Mftf Test can not extend from itself: " . $testObject->getName()
);
}
return $this->extendUtil->extendTest($testObject);
}
return $testObject;
}
}