15
15
use Symfony \Component \Console \Exception \InvalidArgumentException ;
16
16
use Symfony \Component \Console \Input \InputArgument ;
17
17
use Symfony \Component \Console \Input \InputInterface ;
18
+ use Symfony \Component \Console \Input \InputOption ;
18
19
use Symfony \Component \Console \Output \OutputInterface ;
19
20
use Exception ;
21
+ use Symfony \Component \Console \Style \SymfonyStyle ;
20
22
21
23
class StaticChecksCommand extends Command
22
24
{
25
+ /**
26
+ * Associative array containing static ruleset properties.
27
+ *
28
+ * @var array
29
+ */
30
+ private $ ruleSet ;
31
+
23
32
/**
24
33
* Pool of all existing static check objects
25
34
*
@@ -34,6 +43,13 @@ class StaticChecksCommand extends Command
34
43
*/
35
44
private $ staticCheckObjects ;
36
45
46
+ /**
47
+ * Console output style
48
+ *
49
+ * @var SymfonyStyle
50
+ */
51
+ protected $ ioStyle ;
52
+
37
53
/**
38
54
* Configures the current command.
39
55
*
@@ -44,14 +60,20 @@ protected function configure()
44
60
$ list = new StaticChecksList ();
45
61
$ this ->allStaticCheckObjects = $ list ->getStaticChecks ();
46
62
$ staticCheckNames = implode (', ' , array_keys ($ this ->allStaticCheckObjects ));
47
- $ description = " This command will run all static checks on xml test materials. "
48
- . " Available static check scripts are: \n{ $ staticCheckNames}" ;
63
+ $ description = ' This command will run all static checks on xml test materials. '
64
+ . ' Available static check scripts are: ' . PHP_EOL . $ staticCheckNames ;
49
65
$ this ->setName ('static-checks ' )
50
66
->setDescription ($ description )
51
67
->addArgument (
52
68
'names ' ,
53
69
InputArgument::OPTIONAL | InputArgument::IS_ARRAY ,
54
70
'name(s) of specific static check script(s) to run '
71
+ )->addOption (
72
+ 'path ' ,
73
+ 'p ' ,
74
+ InputOption::VALUE_OPTIONAL ,
75
+ 'Path to a MFTF test module to run "deprecatedEntityUsage" static check script. ' . PHP_EOL
76
+ . 'Option is ignored by other static check scripts. ' . PHP_EOL
55
77
);
56
78
}
57
79
@@ -65,32 +87,41 @@ protected function configure()
65
87
*/
66
88
protected function execute (InputInterface $ input , OutputInterface $ output )
67
89
{
90
+ $ this ->ioStyle = new SymfonyStyle ($ input , $ output );
68
91
try {
69
- $ this ->validateInputArguments ($ input, $ output );
92
+ $ this ->validateInput ($ input );
70
93
} catch (InvalidArgumentException $ e ) {
71
94
LoggingUtil::getInstance ()->getLogger (StaticChecksCommand::class)->error ($ e ->getMessage ());
72
- $ output -> writeln ($ e ->getMessage () . " Please fix input arguments and rerun. " );
95
+ $ this -> ioStyle -> error ($ e ->getMessage () . ' Please fix input argument(s) or option(s) and rerun. ' );
73
96
return 1 ;
74
97
}
75
98
99
+ $ cmdFailed = false ;
76
100
$ errors = [];
77
101
foreach ($ this ->staticCheckObjects as $ name => $ staticCheck ) {
78
102
LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->info (
79
- "\nRunning static check script for: " . $ name
80
- );
81
- $ output ->writeln (
82
- "\nRunning static check script for: " . $ name
103
+ 'Running static check script for: ' . $ name . PHP_EOL
83
104
);
84
105
85
- $ staticCheck ->execute ($ input );
106
+ $ this ->ioStyle ->text (PHP_EOL . 'Running static check script for: ' . $ name . PHP_EOL );
107
+ $ start = microtime (true );
108
+ try {
109
+ $ staticCheck ->execute ($ input );
110
+ } catch (Exception $ e ) {
111
+ $ cmdFailed = true ;
112
+ LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->error ($ e ->getMessage () . PHP_EOL );
113
+ $ this ->ioStyle ->error ($ e ->getMessage ());
114
+ }
115
+ $ end = microtime (true );
116
+ $ errors += $ staticCheck ->getErrors ();
86
117
87
118
$ staticOutput = $ staticCheck ->getOutput ();
88
119
LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->info ($ staticOutput );
89
- $ output ->writeln ($ staticOutput );
90
- $ errors += $ staticCheck ->getErrors ();
91
- }
120
+ $ this ->ioStyle ->text ($ staticOutput );
92
121
93
- if (empty ($ errors )) {
122
+ $ this ->ioStyle ->text ('Total execution time is ' . (string )($ end - $ start ) . ' seconds. ' . PHP_EOL );
123
+ }
124
+ if (!$ cmdFailed && empty ($ errors )) {
94
125
return 0 ;
95
126
} else {
96
127
return 1 ;
@@ -104,30 +135,63 @@ protected function execute(InputInterface $input, OutputInterface $output)
104
135
* @return void
105
136
* @throws InvalidArgumentException
106
137
*/
107
- private function validateInputArguments (InputInterface $ input )
138
+ private function validateInput (InputInterface $ input )
108
139
{
109
140
$ this ->staticCheckObjects = [];
110
141
$ requiredChecksNames = $ input ->getArgument ('names ' );
111
- $ invalidCheckNames = [];
112
- // Found user required static check script(s) to run,
113
- // If no static check name is supplied, run all static check scripts
142
+ // Build list of static check names to run.
143
+ if (empty ($ requiredChecksNames )) {
144
+ $ this ->parseRulesetJson ();
145
+ $ requiredChecksNames = $ this ->ruleSet ['tests ' ] ?? null ;
146
+ }
114
147
if (empty ($ requiredChecksNames )) {
115
148
$ this ->staticCheckObjects = $ this ->allStaticCheckObjects ;
116
149
} else {
117
- for ($ index = 0 ; $ index < count ($ requiredChecksNames ); $ index ++) {
118
- if (in_array ($ requiredChecksNames [$ index ], array_keys ($ this ->allStaticCheckObjects ))) {
119
- $ this ->staticCheckObjects [$ requiredChecksNames [$ index ]] =
120
- $ this ->allStaticCheckObjects [$ requiredChecksNames [$ index ]];
121
- } else {
122
- $ invalidCheckNames [] = $ requiredChecksNames [$ index ];
123
- }
150
+ $ this ->validateTestNames ($ requiredChecksNames );
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Validates that all passed in static-check names match an existing static check
156
+ * @param string[] $requiredChecksNames
157
+ * @return void
158
+ */
159
+ private function validateTestNames ($ requiredChecksNames )
160
+ {
161
+ $ invalidCheckNames = [];
162
+ for ($ index = 0 ; $ index < count ($ requiredChecksNames ); $ index ++) {
163
+ if (in_array ($ requiredChecksNames [$ index ], array_keys ($ this ->allStaticCheckObjects ))) {
164
+ $ this ->staticCheckObjects [$ requiredChecksNames [$ index ]] =
165
+ $ this ->allStaticCheckObjects [$ requiredChecksNames [$ index ]];
166
+ } else {
167
+ $ invalidCheckNames [] = $ requiredChecksNames [$ index ];
124
168
}
125
169
}
126
170
127
171
if (!empty ($ invalidCheckNames )) {
128
172
throw new InvalidArgumentException (
129
- " Invalid static check script(s): " . implode (', ' , $ invalidCheckNames ) . " . "
173
+ ' Invalid static check script(s): ' . implode (', ' , $ invalidCheckNames ) . ' . '
130
174
);
131
175
}
132
176
}
177
+
178
+ /**
179
+ * Parses and sets local ruleSet. If not found, simply returns and lets script continue.
180
+ * @return void;
181
+ */
182
+ private function parseRulesetJson ()
183
+ {
184
+ $ pathAddition = "/dev/tests/acceptance/ " ;
185
+ // MFTF is both NOT attached and no MAGENTO_BP defined in .env
186
+ if (MAGENTO_BP === FW_BP ) {
187
+ $ pathAddition = "/dev/ " ;
188
+ }
189
+ $ pathToRuleset = MAGENTO_BP . $ pathAddition . "staticRuleset.json " ;
190
+ if (!file_exists ($ pathToRuleset )) {
191
+ $ this ->ioStyle ->text ("No ruleset under $ pathToRuleset " . PHP_EOL );
192
+ return ;
193
+ }
194
+ $ this ->ioStyle ->text ("Using ruleset under $ pathToRuleset " . PHP_EOL );
195
+ $ this ->ruleSet = json_decode (file_get_contents ($ pathToRuleset ), true );
196
+ }
133
197
}
0 commit comments