@@ -28,13 +28,16 @@ class LiveCodeTest extends TestCase
28
28
*/
29
29
private static $ changeCheckDir = '' ;
30
30
31
+ /**
32
+ * @var array
33
+ */
31
34
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 ' ,
35
37
'Magento\Framework\View\Element\UiComponentInterface ' ,
36
38
'Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface ' ,
37
39
];
40
+ //'Magento\Framework\DataObject\IdentityInterface',
38
41
39
42
/**
40
43
* Setup basics for all tests
@@ -72,115 +75,95 @@ public function testCodeStyle(): void
72
75
* @return void
73
76
* @throws \Magento\Framework\Exception\LocalizedException
74
77
*/
75
- public function testCorrespondingGraphQlChangeExists (): void
78
+ public function testModulesRequireGraphQLChange (): void
76
79
{
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
+ );
89
86
}
90
87
91
88
/**
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
93
90
*
94
91
* @return array
95
92
*/
96
- private static function getModulesWithViewLayerChanges (): array
93
+ private static function getModulesRequiringGraphQLChange (): array
97
94
{
98
95
$ whitelistFiles = PHPCodeTest::getWhitelist (
99
- ['php ' ],
96
+ ['php ' , ' graphqls ' ],
100
97
'' ,
101
98
'' ,
102
99
'/_files/whitelist/graphql.txt '
103
100
);
104
101
105
102
$ affectedModules = [];
103
+ $ requireGraphQLChanges = [];
106
104
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 " ;
123
119
}
124
120
}
125
- return $ affectedModules ;
121
+ return array_diff ( $ requireGraphQLChanges , $ affectedModules) ;
126
122
}
127
123
128
124
/**
129
- * returns a array with the list of graphql module having changes
125
+ * Returns the module name of the file from the path
130
126
*
131
- * @return array
127
+ * @param string $filePath
128
+ * @return string
132
129
*/
133
- private static function getChangedGraphQlModules ( ): array
130
+ private static function getModuleName ( string $ filePath ): string
134
131
{
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 );
141
134
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 ] ?? '' ;
155
136
}
156
137
157
138
/**
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
160
143
*/
161
- private static function getFileReferencePathParts (string $ whitelistFile ): array
144
+ private static function isUiComponent (string $ filePath ): bool
162
145
{
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 ));
165
153
}
166
154
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
168
162
{
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 : '' ;
182
166
}
183
-
184
- return false ;
167
+ return '' ;
185
168
}
186
169
}
0 commit comments