@@ -23,6 +23,11 @@ class LiveCodeTest extends TestCase
2323 */
2424 private static $ reportDir = '' ;
2525
26+ /**
27+ * @var string
28+ */
29+ private static $ changeCheckDir = '' ;
30+
2631 /**
2732 * Setup basics for all tests
2833 */
@@ -32,6 +37,8 @@ public static function setUpBeforeClass(): void
3237 if (!is_dir (self ::$ reportDir )) {
3338 mkdir (self ::$ reportDir , 0770 );
3439 }
40+
41+ self ::$ changeCheckDir = BP . '/app/code/Magento ' ;
3542 }
3643
3744 /**
@@ -50,4 +57,96 @@ public function testCodeStyle(): void
5057 "PHP Code Sniffer detected {$ result } violation(s): " . PHP_EOL . $ report
5158 );
5259 }
60+
61+ /**
62+ * Test if there is corresponding GraphQL module change for each magento core modules
63+ */
64+ public function testCorrespondingGraphQlChangeExists (): void
65+ {
66+ $ changedModules = self ::getChangedCoreModules ();
67+
68+ // Check if for each module change, a graphQL module change happened
69+ foreach ($ changedModules as $ changedModule ) {
70+
71+ // get the git diff status of the module files
72+ $ fileStat = self ::getGitDiff (self ::$ changeCheckDir . '/ ' . $ changedModule );
73+ $ fileChanged = $ fileStat ['insertions ' ] >= 5 || $ fileStat ['deletions ' ] >= 5 ;
74+
75+ // check if there is a reasonable change happened in the module
76+ if ($ fileChanged ) {
77+ // get the git diff status of the graphQL module files
78+ $ graphQlFileStat = self ::getGitDiff (self ::$ changeCheckDir . '/ ' . $ changedModule . 'GraphQl ' );
79+
80+ // assert if there is change in graphql module
81+ $ this ->assertTrue (
82+ $ graphQlFileStat ['insertions ' ] >= 1 || $ graphQlFileStat ['deletions ' ] >= 1 ,
83+ $ changedModule . "'s corresponding GraphQl module change is missing "
84+ );
85+ }
86+ }
87+ }
88+
89+ /**
90+ * returns a multi array with the list of core and graphql modules names
91+ *
92+ * @return array
93+ * @throws \Magento\Framework\Exception\LocalizedException
94+ */
95+ private static function getChangedCoreModules (): array
96+ {
97+ $ whitelistFiles = PHPCodeTest::getWhitelist (['php ' , 'graphqls ' ], '' , '' , '/_files/whitelist/graphql.txt ' );
98+
99+ $ changedModules = [];
100+ foreach ($ whitelistFiles as $ whitelistFile ) {
101+ $ fileName = substr ($ whitelistFile , strlen (self ::$ changeCheckDir ));
102+ $ changedModule = explode ('/ ' , $ fileName );
103+
104+ $ isGraphQlModule = str_ends_with ($ changedModule [1 ], 'GraphQl ' );
105+ $ isGraphQlModuleExists = file_exists (self ::$ changeCheckDir . '/ ' . $ changedModule [1 ] . 'GraphQl ' );
106+
107+ if (!$ isGraphQlModule && $ isGraphQlModuleExists &&
108+ (
109+ in_array ($ changedModule [2 ], ["Controller " , "Model " , "Block " ]) ||
110+ (($ changedModule [2 ] == "Ui " ) && in_array ($ changedModule [3 ], ["Component " , "DataProvider " ]))
111+ )
112+ ) {
113+ $ changedModules [] = $ changedModule [1 ];
114+ }
115+ }
116+
117+ return array_unique ($ changedModules );
118+ }
119+
120+ /**
121+ * @param string $directory
122+ * @return array
123+ * @throws \Magento\Framework\Exception\LocalizedException
124+ */
125+ private static function getGitDiff ($ directory = '' ): array
126+ {
127+ $ shell = new \Magento \Framework \Shell (
128+ new \Magento \Framework \Shell \CommandRenderer ()
129+ );
130+
131+ $ fileStat = explode (
132+ PHP_EOL ,
133+ $ shell ->execute ('git diff --stat ' . $ directory )
134+ );
135+
136+ $ insertions = 0 ;
137+ $ deletions = 0 ;
138+ $ fileChanges = 0 ;
139+ if (array_key_exists (3 , $ fileStat )) {
140+ list ($ fileChanges , $ insertions , $ deletions ) = explode (", " , $ fileStat [3 ]);
141+ $ fileChanges = intval ($ fileChanges );
142+ $ insertions = intval ($ insertions );
143+ $ deletions = intval ($ deletions );
144+ }
145+
146+ return [
147+ 'fileChanges ' => $ fileChanges ,
148+ 'insertions ' => $ insertions ,
149+ 'deletions ' => $ deletions
150+ ];
151+ }
53152}
0 commit comments