Skip to content

Commit 6a6e3ce

Browse files
committed
MQE-2016: Add warning for test materials that violate naming convention
- Add pattern validation
1 parent 182343b commit 6a6e3ce

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ private function __construct()
5555
return;
5656
}
5757

58-
$pageNameValidator = new NameValidationUtil(NameValidationUtil::TYPE_PAGE_NAME);
58+
$pageNameCaseValidator = new NameValidationUtil(NameValidationUtil::TYPE_PAGE_NAME);
59+
$pageNamePatternValidator = new NameValidationUtil(NameValidationUtil::TYPE_PAGE);
5960
foreach ($parserOutput as $pageName => $pageData) {
6061
if (preg_match('/[^a-zA-Z0-9_]/', $pageName)) {
6162
throw new XmlException(sprintf(self::NAME_BLACKLIST_ERROR_MSG, $pageName));
6263
}
6364

6465
$filename = $pageData[self::FILENAME] ?? null;
65-
$pageNameValidator->validatePascalCase($pageName, $filename);
66+
$pageNameCaseValidator->validatePascalCase($pageName, $filename);
67+
$pageNamePatternValidator->validateNamePattern($pageName, $filename);
6668
$area = $pageData[self::AREA] ?? null;
6769
$url = $pageData[self::URL] ?? null;
6870

@@ -85,7 +87,8 @@ private function __construct()
8587
$this->pageObjects[$pageName] =
8688
new PageObject($pageName, $url, $module, $sectionNames, $parameterized, $area, $filename, $deprecated);
8789
}
88-
$pageNameValidator->logCountPascalCase();
90+
$pageNameCaseValidator->logCountPascalCase();
91+
$pageNamePatternValidator->logCountPattern();
8992
}
9093

9194
/**

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magento\FunctionalTestingFramework\Page\Handlers;
88

9-
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
109
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
1110
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1211
use Magento\FunctionalTestingFramework\Page\Objects\ElementObject;
@@ -59,7 +58,8 @@ private function __construct()
5958
return;
6059
}
6160

62-
$sectionNameValidator = new NameValidationUtil(NameValidationUtil::TYPE_SECTION_NAME);
61+
$sectionNameCaseValidator = new NameValidationUtil(NameValidationUtil::TYPE_SECTION_NAME);
62+
$sectionNamePatternValidator = new NameValidationUtil(NameValidationUtil::TYPE_SECTION);
6363
$elementNameValidator = new NameValidationUtil(NameValidationUtil::TYPE_ELEMENT_NAME);
6464
foreach ($parserOutput as $sectionName => $sectionData) {
6565
$elements = [];
@@ -69,7 +69,8 @@ private function __construct()
6969
}
7070

7171
$filename = $sectionData[self::FILENAME] ?? null;
72-
$sectionNameValidator->validatePascalCase($sectionName, $filename);
72+
$sectionNameCaseValidator->validatePascalCase($sectionName, $filename);
73+
$sectionNamePatternValidator->validateNamePattern($sectionName, $filename);
7374

7475
try {
7576
foreach ($sectionData[SectionObjectHandler::ELEMENT] as $elementName => $elementData) {
@@ -120,7 +121,8 @@ private function __construct()
120121
$sectionDeprecated
121122
);
122123
}
123-
$sectionNameValidator->logCountPascalCase();
124+
$sectionNameCaseValidator->logCountPascalCase();
125+
$sectionNamePatternValidator->logCountPattern();
124126
$elementNameValidator->logCountCamelCase();
125127
}
126128

src/Magento/FunctionalTestingFramework/Util/Validation/NameValidationUtil.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class NameValidationUtil
2121
const TYPE_TEST_NAME = "test name";
2222
const TYPE_SECTION_NAME = "section name";
2323
const TYPE_ELEMENT_NAME = "element name";
24+
const TYPE_PAGE = "Page";
25+
const TYPE_SECTION = "Section";
26+
const NAME_PATTERN_PREFIX = "{Admin or Storefront}{Description}";
2427

2528
/**
2629
* The number of violations this instance has detected.
@@ -140,6 +143,38 @@ public function validatePascalCase($str, $filename = null)
140143
}
141144
}
142145

146+
/**
147+
* Validates that the input string is of the pattern {Admin or Storefront}{Description}{Type}
148+
*
149+
* @param string $str
150+
* @param string $filename
151+
* @return void
152+
* @throws Exception
153+
*/
154+
public function validateNamePattern($str, $filename = null)
155+
{
156+
$isPrefixAdmin = substr($str, 0, 5) === "Admin";
157+
$isPrefixStorefront = substr($str, 0, 10) === "Storefront";
158+
$isSuffixType = substr($str, -strlen($this->type)) === $this->type;
159+
160+
if ((!$isPrefixAdmin && !$isPrefixStorefront) || !$isSuffixType) {
161+
$message = "The {$this->type} name {$str} should follow the pattern "
162+
. self::NAME_PATTERN_PREFIX . $this->type . ".";
163+
164+
if ($filename !== null) {
165+
$message .= " See file {$filename}.";
166+
}
167+
168+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
169+
$message,
170+
[],
171+
false
172+
);
173+
174+
$this->count++;
175+
}
176+
}
177+
143178
/**
144179
* Outputs a summary message of the count of violations this instance has tracked.
145180
*
@@ -177,4 +212,23 @@ public function logCountCamelCase()
177212
);
178213
}
179214
}
215+
216+
/**
217+
* Outputs a summary message of the count of violations this instance has tracked.
218+
*
219+
* @return void
220+
* @throws Exception
221+
*/
222+
public function logCountPattern()
223+
{
224+
if ($this->count > 0) {
225+
LoggingUtil::getInstance()->getLogger(self::class)->notification(
226+
"{$this->count} {$this->type} name pattern violations detected."
227+
. " Use " . self::NAME_PATTERN_PREFIX . $this->type . "."
228+
. " See mftf.log for details.",
229+
[],
230+
true
231+
);
232+
}
233+
}
180234
}

0 commit comments

Comments
 (0)