Skip to content

Commit 7c7be3a

Browse files
authored
Merge branch 'develop' into MQE-1078
2 parents 4cf1663 + 7003122 commit 7c7be3a

File tree

4 files changed

+181
-8
lines changed

4 files changed

+181
-8
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;
7+
8+
use AspectMock\Proxy\Verifier;
9+
use AspectMock\Test as AspectMock;
10+
use Magento\FunctionalTestingFramework\Test\Util\AnnotationExtractor;
11+
use Monolog\Handler\TestHandler;
12+
use Monolog\Logger;
13+
use PHPUnit\Framework\TestCase;
14+
use tests\unit\Util\TestLoggingUtil;
15+
16+
class AnnotationExtractorTest extends TestCase
17+
{
18+
/**
19+
* Before test functionality
20+
* @return void
21+
*/
22+
public function setUp()
23+
{
24+
TestLoggingUtil::getInstance()->setMockLoggingUtil();
25+
}
26+
27+
/**
28+
* Annotation extractor takes in raw array and condenses it to expected format
29+
*
30+
* @throws \Exception
31+
*/
32+
public function testExtractAnnotations()
33+
{
34+
// Test Data
35+
$testAnnotations = [
36+
"nodeName" => "annotations",
37+
"features" => [
38+
[
39+
"nodeName" => "features",
40+
"value" => "TestFeatures"
41+
]
42+
],
43+
"stories" => [
44+
[
45+
"nodeName" => "stories",
46+
"value" => "TestStories"
47+
]
48+
],
49+
"description" => [
50+
[
51+
"nodeName" => "description",
52+
"value" => "TestDescription"
53+
]
54+
],
55+
"severity" => [
56+
[
57+
"nodeName" => "severity",
58+
"value" => "CRITICAL"
59+
]
60+
],
61+
"group" => [
62+
[
63+
"nodeName" => "group",
64+
"value" => "TestGroup"
65+
]
66+
],
67+
];
68+
// Perform Test
69+
$extractor = new AnnotationExtractor();
70+
$returnedAnnotations = $extractor->extractAnnotations($testAnnotations, "testFileName");
71+
72+
// Asserts
73+
74+
$this->assertEquals("TestFeatures", $returnedAnnotations['features'][0]);
75+
$this->assertEquals("TestStories", $returnedAnnotations['stories'][0]);
76+
$this->assertEquals("TestDescription", $returnedAnnotations['description'][0]);
77+
$this->assertEquals("CRITICAL", $returnedAnnotations['severity'][0]);
78+
$this->assertEquals("TestGroup", $returnedAnnotations['group'][0]);
79+
}
80+
81+
/**
82+
* Annotation extractor should throw warning when required annotations are missing
83+
*
84+
* @throws \Exception
85+
*/
86+
public function testMissingAnnotations()
87+
{
88+
// Test Data, missing title, description, and severity
89+
$testAnnotations = [
90+
"nodeName" => "annotations",
91+
"features" => [
92+
[
93+
"nodeName" => "features",
94+
"value" => "TestFeatures"
95+
]
96+
],
97+
"stories" => [
98+
[
99+
"nodeName" => "stories",
100+
"value" => "TestStories"
101+
]
102+
],
103+
"group" => [
104+
[
105+
"nodeName" => "group",
106+
"value" => "TestGroup"
107+
]
108+
],
109+
];
110+
// Perform Test
111+
$extractor = new AnnotationExtractor();
112+
$returnedAnnotations = $extractor->extractAnnotations($testAnnotations, "testFileName");
113+
114+
// Asserts
115+
TestLoggingUtil::getInstance()->validateMockLogStatement(
116+
'warning',
117+
'Test testFileName is missing required annotations.',
118+
[
119+
'testName' => 'testFileName',
120+
'missingAnnotations' => "title, description, severity"
121+
]
122+
);
123+
}
124+
125+
/**
126+
* After class functionality
127+
* @return void
128+
*/
129+
public static function tearDownAfterClass()
130+
{
131+
TestLoggingUtil::getInstance()->clearMockLoggingUtil();
132+
}
133+
}

src/Magento/FunctionalTestingFramework/Test/Handlers/TestObjectHandler.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\FunctionalTestingFramework\Test\Handlers;
77

8+
use Magento\FunctionalTestingFramework\Exceptions\Collector\ExceptionCollector;
89
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
910
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
1011
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
@@ -136,13 +137,19 @@ private function initTestData()
136137
return;
137138
}
138139

140+
$exceptionCollector = new ExceptionCollector();
139141
foreach ($parsedTestArray[TestObjectHandler::XML_ROOT] as $testName => $testData) {
140142
if (!is_array($testData)) {
141143
continue;
142144
}
143-
144-
$this->tests[$testName] = $testObjectExtractor->extractTestData($testData);
145+
try {
146+
$this->tests[$testName] = $testObjectExtractor->extractTestData($testData);
147+
} catch (XmlException $exception) {
148+
$exceptionCollector->addError(self::class, $exception->getMessage());
149+
}
145150
}
151+
$exceptionCollector->throwException();
152+
146153
$testObjectExtractor->getAnnotationExtractor()->validateStoryTitleUniqueness();
147154
}
148155

src/Magento/FunctionalTestingFramework/Test/Util/AnnotationExtractor.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\FunctionalTestingFramework\Test\Util;
88

99
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
10+
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
1011

1112
/**
1213
* Class AnnotationExtractor
@@ -28,6 +29,12 @@ class AnnotationExtractor extends BaseObjectExtractor
2829
"AVERAGE" => "MINOR",
2930
"MINOR" => "TRIVIAL"
3031
];
32+
const REQUIRED_ANNOTATIONS = [
33+
"stories",
34+
"title",
35+
"description",
36+
"severity"
37+
];
3138

3239
/**
3340
* AnnotationExtractor constructor.
@@ -67,7 +74,11 @@ public function extractAnnotations($testAnnotations, $filename)
6774
}
6875
$annotationObjects[$annotationKey] = $annotationValues;
6976
}
77+
78+
$this->validateMissingAnnotations($annotationObjects, $filename);
79+
7080
$this->addStoryTitleToMap($annotationObjects, $filename);
81+
7182
return $annotationObjects;
7283
}
7384

@@ -86,6 +97,30 @@ public function addStoryTitleToMap($annotations, $filename)
8697
}
8798
}
8899

100+
/**
101+
* Validates given annotations against list of required annotations.
102+
* @param array $annotationObjects
103+
* @return void
104+
*/
105+
private function validateMissingAnnotations($annotationObjects, $filename)
106+
{
107+
$missingAnnotations = [];
108+
109+
foreach (self::REQUIRED_ANNOTATIONS as $REQUIRED_ANNOTATION) {
110+
if (!array_key_exists($REQUIRED_ANNOTATION, $annotationObjects)) {
111+
$missingAnnotations[] = $REQUIRED_ANNOTATION;
112+
}
113+
}
114+
115+
if (!empty($missingAnnotations)) {
116+
$message = "Test {$filename} is missing required annotations.";
117+
LoggingUtil::getInstance()->getLogger(ActionObject::class)->warning(
118+
$message,
119+
["testName" => $filename, "missingAnnotations" => implode(", ", $missingAnnotations)]
120+
);
121+
}
122+
}
123+
89124
/**
90125
* Validates that all Story/Title combinations are unique, builds list of violators if found.
91126
* @throws XmlException

src/Magento/FunctionalTestingFramework/Test/Util/TestObjectExtractor.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,10 @@ public function extractTestData($testData)
110110
'extends'
111111
);
112112

113-
if (array_key_exists(self::TEST_ANNOTATIONS, $testData)) {
114-
$testAnnotations = $this->annotationExtractor->extractAnnotations(
115-
$testData[self::TEST_ANNOTATIONS],
116-
$testData[self::NAME]
117-
);
118-
}
113+
$testAnnotations = $this->annotationExtractor->extractAnnotations(
114+
$testData[self::TEST_ANNOTATIONS] ?? [],
115+
$testData[self::NAME]
116+
);
119117

120118
//Override features with module name if present, populates it otherwise
121119
$testAnnotations["features"] = [$module];

0 commit comments

Comments
 (0)