Skip to content

Commit

Permalink
Merge pull request #161 from magento-lynx/gl-graphql-AC-9497
Browse files Browse the repository at this point in the history
AC-9497: Static test to check if required GraphQL changes are included in a pull request
  • Loading branch information
sumesh-GL authored Oct 26, 2023
2 parents 94f014f + 5ce1f11 commit 368179a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
117 changes: 117 additions & 0 deletions dev/tests/static/testsuite/Magento/Test/GraphQl/LiveCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ class LiveCodeTest extends TestCase
*/
private static $reportDir = '';

/**
* @var string
*/
private static $changeCheckDir = '';

/**
* @var array
*/
private static $uiDataComponentInterface = [
'Magento\Framework\App\ActionInterface',
'Magento\Framework\View\Element\BlockInterface',
'Magento\Framework\View\Element\UiComponentInterface',
'Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface',
];

/**
* Setup basics for all tests
*/
Expand All @@ -32,6 +47,8 @@ public static function setUpBeforeClass(): void
if (!is_dir(self::$reportDir)) {
mkdir(self::$reportDir, 0770);
}

self::$changeCheckDir = BP . '/app/code/Magento';
}

/**
Expand All @@ -50,4 +67,104 @@ public function testCodeStyle(): void
"PHP Code Sniffer detected {$result} violation(s): " . PHP_EOL . $report
);
}

/**
* Test if there is corresponding GraphQL module change for each magento core modules
*
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function testModulesRequireGraphQLChange(): void
{
$modulesRequireGraphQLChange = self::getModulesRequiringGraphQLChange();
$graphQlModules = implode(", ", $modulesRequireGraphQLChange);
$this->assertEmpty(
$modulesRequireGraphQLChange,
"The view layer changes have been detected in the " .
str_replace("GraphQl", "", $graphQlModules) . " module. " .
"The " . $graphQlModules ." module is expected to be updated to reflect these changes."
);
}

/**
* returns a array with the list of graphql modules which require changes
*
* @return array
*/
private static function getModulesRequiringGraphQLChange(): array
{
$whitelistFiles = PHPCodeTest::getWhitelist(
['php', 'graphqls'],
'',
'',
'/_files/whitelist/graphql.txt'
);

$updatedGraphQlModules = [];
$requireGraphQLChanges = [];
foreach ($whitelistFiles as $whitelistFile) {
$moduleName = self::getModuleName($whitelistFile);

if (!$moduleName) {
continue;
}

$isGraphQlModule = str_ends_with($moduleName, 'GraphQl');
if (!in_array($moduleName, $updatedGraphQlModules) && $isGraphQlModule) {
$updatedGraphQlModules[] = $moduleName;
continue;
}

if (!in_array($moduleName, $requireGraphQLChanges) && self::isViewLayerClass($whitelistFile)) {
$requireGraphQLChanges[] = $moduleName . "GraphQl";
}
}
return array_diff($requireGraphQLChanges, $updatedGraphQlModules);
}

/**
* Returns the module name of the file from the path
*
* @param string $filePath
* @return string
*/
private static function getModuleName(string $filePath): string
{
$fileName = substr($filePath, strlen(self::$changeCheckDir));
$pathParts = explode('/', $fileName);

return $pathParts[1] ?? '';
}

/**
* Return true if the class implements any of the defined interfaces
*
* @param string $filePath
* @return bool
*/
private static function isViewLayerClass(string $filePath): bool
{
$className = self::getClassNameWithNamespace($filePath);
if (!$className) {
return false;
}

$implementingInterfaces = array_values(class_implements($className));
return !empty(array_intersect($implementingInterfaces, self::$uiDataComponentInterface));
}

/**
* Returns the files namespace using regular expression
*
* @param string $filePath
* @return string
*/
private static function getClassNameWithNamespace(string $filePath): string
{
$className = str_replace('.php', '', basename($filePath));
if (preg_match('#^namespace\s+(.+?);$#sm', file_get_contents($filePath), $m)) {
return ($m[1] && $className) ? $m[1] . "\\" . $className : '';
}
return '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Format: <componentType=module|library|theme|language|*> <componentName> <globPattern> or simply <globPattern>
app/code/Magento

0 comments on commit 368179a

Please sign in to comment.