Skip to content

Commit 081692d

Browse files
committed
MQE-2016: Add warning for test materials that violate naming convention
1 parent 32e7600 commit 081692d

File tree

6 files changed

+277
-7
lines changed

6 files changed

+277
-7
lines changed

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/DataObjectHandler.php

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ class DataObjectHandler implements ObjectHandlerInterface
5858
*/
5959
private $extendUtil;
6060

61+
/**
62+
* Keeps track of data.key's that are not camelCase.
63+
*
64+
* @var array
65+
*/
66+
private $entityKeyViolations = [];
67+
68+
/**
69+
* Keeps track of data entity names that are not PascalCase.
70+
*
71+
* @var array
72+
*/
73+
private $nameViolations = [];
74+
6175
/**
6276
* Constructor
6377
*/
@@ -121,6 +135,7 @@ public function getAllObjects()
121135
* @return EntityDataObject[]
122136
* @throws XmlException
123137
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
138+
* @SuppressWarnings(PHPMD.NPathComplexity)
124139
*/
125140
private function processParserOutput($parserOutput)
126141
{
@@ -132,14 +147,26 @@ private function processParserOutput($parserOutput)
132147
throw new XmlException(sprintf(self::DATA_NAME_ERROR_MSG, $name));
133148
}
134149

150+
$filename = $rawEntity[self::_FILENAME] ?? null;
151+
152+
// Check that data entity name is PascalCase
153+
if (!ctype_upper($name[0])) {
154+
$this->nameViolations[] = $name;
155+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
156+
"The data entity {$name} should be PascalCase with an uppercase first letter."
157+
. " See file {$filename}.",
158+
[],
159+
false
160+
);
161+
}
162+
135163
$type = $rawEntity[self::_TYPE] ?? null;
136164
$data = [];
137165
$deprecated = null;
138166
$linkedEntities = [];
139167
$uniquenessData = [];
140168
$vars = [];
141169
$parentEntity = null;
142-
$filename = $rawEntity[self::_FILENAME] ?? null;
143170

144171
if (array_key_exists(self::_DATA, $rawEntity)) {
145172
$data = $this->processDataElements($rawEntity);
@@ -188,7 +215,7 @@ private function processParserOutput($parserOutput)
188215

189216
$entityDataObjects[$entityDataObject->getName()] = $entityDataObject;
190217
}
191-
218+
$this->logViolationSummary();
192219
return $entityDataObjects;
193220
}
194221

@@ -220,7 +247,21 @@ private function processDataElements($entityData)
220247
{
221248
$dataValues = [];
222249
foreach ($entityData[self::_DATA] as $dataElement) {
223-
$dataElementKey = strtolower($dataElement[self::_KEY]);
250+
$originalDataElementKey = $dataElement[self::_KEY];
251+
$filename = $entityData[self::_FILENAME] ?? null;
252+
253+
// Check that data.keys are camelCase
254+
if (!ctype_lower($originalDataElementKey[0])) {
255+
$this->entityKeyViolations[] = $originalDataElementKey;
256+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
257+
"The data entity key {$originalDataElementKey} should be camelCase with a lowercase"
258+
. " first letter. See file {$filename}.",
259+
[],
260+
false
261+
);
262+
}
263+
264+
$dataElementKey = strtolower($originalDataElementKey);
224265
$dataElementValue = $dataElement[self::_VALUE] ?? "";
225266
$dataValues[$dataElementKey] = $dataElementValue;
226267
}
@@ -297,4 +338,32 @@ private function extendDataObject($dataObject)
297338
}
298339
return $dataObject;
299340
}
341+
342+
/**
343+
* Output to the log a summary of data entity name and data entity key violations.
344+
*
345+
* @throws TestFrameworkException
346+
* @return void
347+
*/
348+
private function logViolationSummary()
349+
{
350+
$nameViolationCount = count($this->nameViolations);
351+
if ($nameViolationCount > 0) {
352+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
353+
"{$nameViolationCount} data entity name violations detected. Data entity names must be"
354+
. " PascalCase with an uppercase first letter.",
355+
[],
356+
true
357+
);
358+
}
359+
$keyViolationCount = count($this->entityKeyViolations);
360+
if ($keyViolationCount > 0) {
361+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
362+
"{$keyViolationCount} data entity key violations detected. Data entity keys must be"
363+
. " camelCase with a lowercase first letter.",
364+
[],
365+
true
366+
);
367+
}
368+
}
300369
}

src/Magento/FunctionalTestingFramework/DataGenerator/Handlers/OperationDefinitionObjectHandler.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;
1010
use Magento\FunctionalTestingFramework\DataGenerator\Parsers\OperationDefinitionParser;
1111
use Magento\FunctionalTestingFramework\DataGenerator\Util\OperationElementExtractor;
12+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
1213
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
1314
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1415
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
@@ -136,7 +137,20 @@ private function initialize()
136137
$objectManager = ObjectManagerFactory::getObjectManager();
137138
$parser = $objectManager->create(OperationDefinitionParser::class);
138139
$parserOutput = $parser->readOperationMetadata()[OperationDefinitionObjectHandler::ENTITY_OPERATION_ROOT_TAG];
140+
141+
$nameViolations = [];
139142
foreach ($parserOutput as $dataDefName => $opDefArray) {
143+
// Check that operation names are PascalCase
144+
if (!ctype_upper($dataDefName[0])) {
145+
$nameViolations[] = $dataDefName;
146+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
147+
"The metadata operation {$dataDefName} should be PascalCase with an uppercase"
148+
. " first letter.",
149+
[],
150+
false
151+
);
152+
}
153+
140154
$operation = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE];
141155
$dataType = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE];
142156
$url = $opDefArray[OperationDefinitionObjectHandler::ENTITY_OPERATION_URL] ?? null;
@@ -230,6 +244,27 @@ private function initialize()
230244
$deprecated
231245
);
232246
}
247+
$this->logViolationSummary($nameViolations);
248+
}
249+
250+
/**
251+
* Output to the log a summary of metadata operation name violations.
252+
*
253+
* @param string[] $nameViolations An array of metadata operation names that are not PascalCase.
254+
* @throws TestFrameworkException
255+
* @return void
256+
*/
257+
private function logViolationSummary($nameViolations)
258+
{
259+
$nameViolationCount = count($nameViolations);
260+
if ($nameViolationCount > 0) {
261+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
262+
"{$nameViolationCount} metadata operation name violations detected. Metadata operation names"
263+
. " must be PascalCase with an uppercase first letter.",
264+
[],
265+
true
266+
);
267+
}
233268
}
234269

235270
/**

src/Magento/FunctionalTestingFramework/Page/Handlers/PageObjectHandler.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\FunctionalTestingFramework\Page\Handlers;
88

9+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
910
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
1011
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1112
use Magento\FunctionalTestingFramework\Page\Objects\PageObject;
@@ -54,10 +55,25 @@ private function __construct()
5455
return;
5556
}
5657

58+
$nameViolations = [];
5759
foreach ($parserOutput as $pageName => $pageData) {
5860
if (preg_match('/[^a-zA-Z0-9_]/', $pageName)) {
5961
throw new XmlException(sprintf(self::NAME_BLACKLIST_ERROR_MSG, $pageName));
6062
}
63+
64+
$filename = $pageData[self::FILENAME] ?? null;
65+
66+
// Check that page names are PascalCase
67+
if (!ctype_upper($pageName[0])) {
68+
$nameViolations[] = $pageName;
69+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
70+
"The page name {$pageName} should be PascalCase with an uppercase first letter."
71+
. " See file {$filename}.",
72+
[],
73+
false
74+
);
75+
}
76+
6177
$area = $pageData[self::AREA] ?? null;
6278
$url = $pageData[self::URL] ?? null;
6379

@@ -68,7 +84,6 @@ private function __construct()
6884
$module = $pageData[self::MODULE] ?? null;
6985
$sectionNames = array_keys($pageData[self::SECTION] ?? []);
7086
$parameterized = $pageData[self::PARAMETERIZED] ?? false;
71-
$filename = $pageData[self::FILENAME] ?? null;
7287
$deprecated = $pageData[self::OBJ_DEPRECATED] ?? null;
7388

7489
if ($deprecated !== null) {
@@ -81,6 +96,7 @@ private function __construct()
8196
$this->pageObjects[$pageName] =
8297
new PageObject($pageName, $url, $module, $sectionNames, $parameterized, $area, $filename, $deprecated);
8398
}
99+
$this->logViolationSummary($nameViolations);
84100
}
85101

86102
/**
@@ -122,4 +138,24 @@ public function getAllObjects()
122138
{
123139
return $this->pageObjects;
124140
}
141+
142+
/**
143+
* Output to the log a summary of page name violations.
144+
*
145+
* @param string[] $nameViolations An array of page names that are not PascalCase.
146+
* @throws TestFrameworkException
147+
* @return void
148+
*/
149+
private function logViolationSummary($nameViolations)
150+
{
151+
$nameViolationCount = count($nameViolations);
152+
if ($nameViolationCount > 0) {
153+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
154+
"{$nameViolationCount} page name violations detected. Page names must be PascalCase with"
155+
. " an uppercase first letter.",
156+
[],
157+
true
158+
);
159+
}
160+
}
125161
}

src/Magento/FunctionalTestingFramework/Page/Handlers/SectionObjectHandler.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
namespace Magento\FunctionalTestingFramework\Page\Handlers;
88

9+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
910
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
1011
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1112
use Magento\FunctionalTestingFramework\Page\Objects\ElementObject;
1213
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
13-
use Magento\FunctionalTestingFramework\Test\Util\TestObjectExtractor;
1414
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
1515
use Magento\FunctionalTestingFramework\XmlParser\SectionParser;
1616
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
@@ -47,6 +47,9 @@ class SectionObjectHandler implements ObjectHandlerInterface
4747
*
4848
* @constructor
4949
* @throws XmlException
50+
*
51+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
52+
* @SuppressWarnings(PHPMD.NPathComplexity)
5053
*/
5154
private function __construct()
5255
{
@@ -58,18 +61,45 @@ private function __construct()
5861
return;
5962
}
6063

64+
$sectionNameViolations = [];
65+
$elementNameViolations = [];
6166
foreach ($parserOutput as $sectionName => $sectionData) {
6267
$elements = [];
6368

6469
if (preg_match('/[^a-zA-Z0-9_]/', $sectionName)) {
6570
throw new XmlException(sprintf(self::SECTION_NAME_ERROR_MSG, $sectionName));
6671
}
6772

73+
$filename = $sectionData[self::FILENAME] ?? null;
74+
75+
// Check that section names are PascalCase
76+
if (!ctype_upper($sectionName[0])) {
77+
$sectionNameViolations[] = $sectionName;
78+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
79+
"The section name {$sectionName} should be PascalCase with an uppercase first letter."
80+
. " See file {$filename}.",
81+
[],
82+
false
83+
);
84+
}
85+
6886
try {
6987
foreach ($sectionData[SectionObjectHandler::ELEMENT] as $elementName => $elementData) {
7088
if (preg_match('/[^a-zA-Z0-9_]/', $elementName)) {
7189
throw new XmlException(sprintf(self::ELEMENT_NAME_ERROR_MSG, $elementName, $sectionName));
7290
}
91+
92+
// Check that element names are camelCase
93+
if (!ctype_lower($elementName[0])) {
94+
$elementNameViolations[] = $elementName;
95+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
96+
"The section element name {$elementName} should be camelCase with a lowercase"
97+
. " first letter. See file {$filename}.",
98+
[],
99+
false
100+
);
101+
}
102+
73103
$elementType = $elementData[SectionObjectHandler::TYPE] ?? null;
74104
$elementSelector = $elementData[SectionObjectHandler::SELECTOR] ?? null;
75105
$elementLocatorFunc = $elementData[SectionObjectHandler::LOCATOR_FUNCTION] ?? null;
@@ -96,7 +126,6 @@ private function __construct()
96126
throw new XmlException($exception->getMessage() . " in Section '{$sectionName}'");
97127
}
98128

99-
$filename = $sectionData[self::FILENAME] ?? null;
100129
$sectionDeprecated = $sectionData[self::OBJ_DEPRECATED] ?? null;
101130

102131
if ($sectionDeprecated !== null) {
@@ -113,6 +142,7 @@ private function __construct()
113142
$sectionDeprecated
114143
);
115144
}
145+
$this->logViolationSummary($sectionNameViolations, $elementNameViolations);
116146
}
117147

118148
/**
@@ -154,4 +184,34 @@ public function getAllObjects()
154184
{
155185
return $this->sectionObjects;
156186
}
187+
188+
/**
189+
* Output to the log a summary of section name and element name violations.
190+
*
191+
* @param string[] $sectionNameViolations A list of section names that are not PascalCase.
192+
* @param string[] $elementNameViolations A list of element names that are not camelCase.
193+
* @throws TestFrameworkException
194+
* @return void
195+
*/
196+
private function logViolationSummary($sectionNameViolations, $elementNameViolations)
197+
{
198+
$nameViolationCount = count($sectionNameViolations);
199+
if ($nameViolationCount > 0) {
200+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
201+
"{$nameViolationCount} section name violations detected. Section names must be PascalCase"
202+
. " with an uppercase first letter.",
203+
[],
204+
true
205+
);
206+
}
207+
$keyViolationCount = count($elementNameViolations);
208+
if ($keyViolationCount > 0) {
209+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
210+
"{$keyViolationCount} section element name violations detected. Element names must be"
211+
. " camelCase with a lowercase first letter.",
212+
[],
213+
true
214+
);
215+
}
216+
}
157217
}

0 commit comments

Comments
 (0)