Skip to content

Commit 521be73

Browse files
committed
AC-9497: static test Modules Require GraphQL Change
1 parent 8b9d440 commit 521be73

File tree

1 file changed

+62
-79
lines changed

1 file changed

+62
-79
lines changed

dev/tests/static/testsuite/Magento/Test/GraphQl/LiveCodeTest.php

Lines changed: 62 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ class LiveCodeTest extends TestCase
2828
*/
2929
private static $changeCheckDir = '';
3030

31+
/**
32+
* @var array
33+
*/
3134
private static $uiDataComponentInterface = [
32-
'Magento\Framework\Api\ExtensibleDataInterface',
33-
'Magento\Framework\Api\CustomAttributesDataInterface',
34-
'Magento\Framework\DataObject\IdentityInterface',
35+
'Magento\Framework\App\ActionInterface',
36+
'Magento\Framework\View\Element\BlockInterface',
3537
'Magento\Framework\View\Element\UiComponentInterface',
3638
'Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface',
3739
];
40+
//'Magento\Framework\DataObject\IdentityInterface',
3841

3942
/**
4043
* Setup basics for all tests
@@ -72,115 +75,95 @@ public function testCodeStyle(): void
7275
* @return void
7376
* @throws \Magento\Framework\Exception\LocalizedException
7477
*/
75-
public function testCorrespondingGraphQlChangeExists(): void
78+
public function testModulesRequireGraphQLChange(): void
7679
{
77-
$modulesWithViewLayerChanges = self::getModulesWithViewLayerChanges();
78-
$changedGraphQlModules = self::getChangedGraphQlModules();
79-
80-
// Check if for each module change, a graphQL module change happened
81-
foreach ($modulesWithViewLayerChanges as $module) {
82-
$this->assertArrayHasKey(
83-
$module . 'GraphQl',
84-
$changedGraphQlModules,
85-
$module . " module: Required GraphQL changes to module (".
86-
$module . "GraphQl) are not included in the pull request"
87-
);
88-
}
80+
$modulesRequireGraphQLChange = self::getModulesRequiringGraphQLChange();
81+
$this->assertEmpty(
82+
$modulesRequireGraphQLChange,
83+
"Required GraphQL changes to module: (" .
84+
implode(", ", $modulesRequireGraphQLChange) .") are not included in the pull request"
85+
);
8986
}
9087

9188
/**
92-
* returns a array with the list of modules having view later change
89+
* returns a array with the list of graphql modules which require changes
9390
*
9491
* @return array
9592
*/
96-
private static function getModulesWithViewLayerChanges(): array
93+
private static function getModulesRequiringGraphQLChange(): array
9794
{
9895
$whitelistFiles = PHPCodeTest::getWhitelist(
99-
['php'],
96+
['php', 'graphqls'],
10097
'',
10198
'',
10299
'/_files/whitelist/graphql.txt'
103100
);
104101

105102
$affectedModules = [];
103+
$requireGraphQLChanges = [];
106104
foreach ($whitelistFiles as $whitelistFile) {
107-
$PathParts = self::getFileReferencePathParts($whitelistFile);
108-
109-
if (array_key_exists(1, $PathParts)) {
110-
$isGraphQlModule = str_ends_with($PathParts[1], 'GraphQl');
111-
$isGraphQlModuleExists = file_exists(
112-
self::$changeCheckDir . '/' . $PathParts[1] . 'GraphQl'
113-
);
114-
115-
if (!$isGraphQlModule && $isGraphQlModuleExists &&
116-
(
117-
in_array($PathParts[2], ["Controller", "Block"]) ||
118-
self::checkIfImplementsUiDataInterfaces($whitelistFile)
119-
)
120-
) {
121-
$affectedModules[] = $PathParts[1];
122-
}
105+
$moduleName = self::getModuleName($whitelistFile);
106+
107+
if (!$moduleName) {
108+
continue;
109+
}
110+
111+
$isGraphQlModule = str_ends_with($moduleName, 'GraphQl');
112+
if (!in_array($moduleName, $affectedModules) && $isGraphQlModule) {
113+
$affectedModules[] = $moduleName;
114+
continue;
115+
}
116+
117+
if (!in_array($moduleName, $requireGraphQLChanges) && self::isUiComponent($whitelistFile)) {
118+
$requireGraphQLChanges[] = $moduleName . "GraphQl";
123119
}
124120
}
125-
return $affectedModules;
121+
return array_diff($requireGraphQLChanges, $affectedModules);
126122
}
127123

128124
/**
129-
* returns a array with the list of graphql module having changes
125+
* Returns the module name of the file from the path
130126
*
131-
* @return array
127+
* @param string $filePath
128+
* @return string
132129
*/
133-
private static function getChangedGraphQlModules(): array
130+
private static function getModuleName(string $filePath): string
134131
{
135-
$whitelistFiles = PHPCodeTest::getWhitelist(
136-
['php', 'graphqls'],
137-
'',
138-
'',
139-
'/_files/whitelist/graphql.txt'
140-
);
132+
$fileName = substr($filePath, strlen(self::$changeCheckDir));
133+
$pathParts = explode('/', $fileName);
141134

142-
$affectedModules = [];
143-
foreach ($whitelistFiles as $whitelistFile) {
144-
$PathParts = self::getFileReferencePathParts($whitelistFile);
145-
146-
if (array_key_exists(1, $PathParts)) {
147-
$isGraphQlModule = str_ends_with($PathParts[1], 'GraphQl');
148-
149-
if ($isGraphQlModule) {
150-
$affectedModules[] = $PathParts[1];
151-
}
152-
}
153-
}
154-
return $affectedModules;
135+
return $pathParts[1] ?? '';
155136
}
156137

157138
/**
158-
* @param string $whitelistFile
159-
* @return array
139+
* Return true if the class implements any of the defined interfaces
140+
*
141+
* @param string $filePath
142+
* @return bool
160143
*/
161-
private static function getFileReferencePathParts(string $whitelistFile): array
144+
private static function isUiComponent(string $filePath): bool
162145
{
163-
$fileName = substr($whitelistFile, strlen(self::$changeCheckDir));
164-
return explode('/', $fileName);
146+
$className = self::getClassNameWithNamespace($filePath);
147+
if (!$className) {
148+
return false;
149+
}
150+
151+
$implementingInterfaces = array_values(class_implements($className));
152+
return !empty(array_intersect($implementingInterfaces, self::$uiDataComponentInterface));
165153
}
166154

167-
private static function checkIfImplementsUiDataInterfaces(string $filename): bool
155+
/**
156+
* Returns the files namespace using regular expression
157+
*
158+
* @param string $filePath
159+
* @return string
160+
*/
161+
private static function getClassNameWithNamespace(string $filePath): string
168162
{
169-
$classes = get_declared_classes();
170-
include $filename;
171-
$diff = array_diff(get_declared_classes(), $classes);
172-
173-
$interfaces = [];
174-
if (count($diff)) {
175-
$interfaces = array_values(class_implements(array_values($diff)[0]));
176-
}
177-
178-
if (
179-
count(array_intersect($interfaces, self::$uiDataComponentInterface))
180-
) {
181-
return true;
163+
$className = str_replace('.php', '', basename($filePath));
164+
if (preg_match('#^namespace\s+(.+?);$#sm', file_get_contents($filePath), $m)) {
165+
return ($m[1] && $className) ? $m[1] . "\\" . $className : '';
182166
}
183-
184-
return false;
167+
return '';
185168
}
186169
}

0 commit comments

Comments
 (0)