8
8
9
9
namespace Magento \FunctionalTestingFramework \Console ;
10
10
11
+ use Magento \FunctionalTestingFramework \StaticCheck \StaticCheckInterface ;
11
12
use Magento \FunctionalTestingFramework \StaticCheck \StaticChecksList ;
12
- use Magento \FunctionalTestingFramework \StaticCheck \StaticCheckListInterface ;
13
13
use Magento \FunctionalTestingFramework \Util \Logger \LoggingUtil ;
14
14
use Symfony \Component \Console \Command \Command ;
15
+ use Symfony \Component \Console \Exception \InvalidArgumentException ;
16
+ use Symfony \Component \Console \Input \InputArgument ;
15
17
use Symfony \Component \Console \Input \InputInterface ;
16
18
use Symfony \Component \Console \Output \OutputInterface ;
17
19
use Exception ;
18
20
19
21
class StaticChecksCommand extends Command
20
22
{
21
23
/**
22
- * Pool of static check scripts to run
24
+ * Pool of all existing static check objects
23
25
*
24
- * @var StaticCheckListInterface
26
+ * @var StaticCheckInterface[]
25
27
*/
26
- private $ staticChecksList ;
28
+ private $ allStaticCheckObjects ;
29
+
30
+ /**
31
+ * Static checks to run
32
+ *
33
+ * @var StaticCheckInterface[]
34
+ */
35
+ private $ staticCheckObjects ;
27
36
28
37
/**
29
38
* Configures the current command.
@@ -32,13 +41,22 @@ class StaticChecksCommand extends Command
32
41
*/
33
42
protected function configure ()
34
43
{
44
+ $ list = new StaticChecksList ();
45
+ $ this ->allStaticCheckObjects = $ list ->getStaticChecks ();
46
+ $ 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 }" ;
35
49
$ this ->setName ('static-checks ' )
36
- ->setDescription ('This command will run all static checks on xml test materials. ' );
37
- $ this ->staticChecksList = new StaticChecksList ();
50
+ ->setDescription ($ description )
51
+ ->addArgument (
52
+ 'names ' ,
53
+ InputArgument::OPTIONAL | InputArgument::IS_ARRAY ,
54
+ 'name(s) of specific static check script(s) to run '
55
+ );
38
56
}
39
57
40
58
/**
41
- *
59
+ * Run required static check scripts
42
60
*
43
61
* @param InputInterface $input
44
62
* @param OutputInterface $output
@@ -47,11 +65,23 @@ protected function configure()
47
65
*/
48
66
protected function execute (InputInterface $ input , OutputInterface $ output )
49
67
{
50
- $ staticCheckObjects = $ this ->staticChecksList ->getStaticChecks ();
68
+ try {
69
+ $ this ->validateInputArguments ($ input , $ output );
70
+ } catch (InvalidArgumentException $ e ) {
71
+ LoggingUtil::getInstance ()->getLogger (StaticChecksCommand::class)->error ($ e ->getMessage ());
72
+ $ output ->writeln ($ e ->getMessage () . " Please fix input arguments and rerun. " );
73
+ return 1 ;
74
+ }
51
75
52
76
$ errors = [];
77
+ foreach ($ this ->staticCheckObjects as $ name => $ staticCheck ) {
78
+ 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
83
+ );
53
84
54
- foreach ($ staticCheckObjects as $ staticCheck ) {
55
85
$ staticCheck ->execute ($ input );
56
86
57
87
$ staticOutput = $ staticCheck ->getOutput ();
@@ -66,4 +96,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
66
96
return 1 ;
67
97
}
68
98
}
99
+
100
+ /**
101
+ * Validate input arguments
102
+ *
103
+ * @param InputInterface $input
104
+ * @return void
105
+ * @throws InvalidArgumentException
106
+ */
107
+ private function validateInputArguments (InputInterface $ input )
108
+ {
109
+ $ this ->staticCheckObjects = [];
110
+ $ 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
114
+ if (empty ($ requiredChecksNames )) {
115
+ $ this ->staticCheckObjects = $ this ->allStaticCheckObjects ;
116
+ } 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
+ }
124
+ }
125
+ }
126
+
127
+ if (!empty ($ invalidCheckNames )) {
128
+ throw new InvalidArgumentException (
129
+ "Invalid static check script(s): " . implode (', ' , $ invalidCheckNames ) . ". "
130
+ );
131
+ }
132
+ }
69
133
}
0 commit comments