88
99namespace Magento \FunctionalTestingFramework \Console ;
1010
11+ use Magento \FunctionalTestingFramework \StaticCheck \StaticCheckInterface ;
1112use Magento \FunctionalTestingFramework \StaticCheck \StaticChecksList ;
12- use Magento \FunctionalTestingFramework \StaticCheck \StaticCheckListInterface ;
1313use Magento \FunctionalTestingFramework \Util \Logger \LoggingUtil ;
1414use Symfony \Component \Console \Command \Command ;
15+ use Symfony \Component \Console \Exception \InvalidArgumentException ;
16+ use Symfony \Component \Console \Input \InputArgument ;
1517use Symfony \Component \Console \Input \InputInterface ;
1618use Symfony \Component \Console \Output \OutputInterface ;
1719use Exception ;
1820
1921class StaticChecksCommand extends Command
2022{
2123 /**
22- * Pool of static check scripts to run
24+ * Pool of all existing static check objects
2325 *
24- * @var StaticCheckListInterface
26+ * @var StaticCheckInterface[]
2527 */
26- private $ staticChecksList ;
28+ private $ allStaticCheckObjects ;
29+
30+ /**
31+ * Static checks to run
32+ *
33+ * @var StaticCheckInterface[]
34+ */
35+ private $ staticCheckObjects ;
2736
2837 /**
2938 * Configures the current command.
@@ -32,13 +41,22 @@ class StaticChecksCommand extends Command
3241 */
3342 protected function configure ()
3443 {
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 }" ;
3549 $ 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+ );
3856 }
3957
4058 /**
41- *
59+ * Run required static check scripts
4260 *
4361 * @param InputInterface $input
4462 * @param OutputInterface $output
@@ -47,11 +65,23 @@ protected function configure()
4765 */
4866 protected function execute (InputInterface $ input , OutputInterface $ output )
4967 {
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+ }
5175
5276 $ 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+ );
5384
54- foreach ($ staticCheckObjects as $ staticCheck ) {
5585 $ staticCheck ->execute ($ input );
5686
5787 $ staticOutput = $ staticCheck ->getOutput ();
@@ -66,4 +96,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
6696 return 1 ;
6797 }
6898 }
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+ }
69133}
0 commit comments