diff --git a/src/commands/Command.php b/src/commands/Command.php index 594d948..b1b5b18 100644 --- a/src/commands/Command.php +++ b/src/commands/Command.php @@ -7,7 +7,10 @@ namespace Seacms\Command; +use Composer\Console\Application; use Composer\Command\BaseCommand; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -30,11 +33,21 @@ use Symfony\Component\Console\Output\OutputInterface; // the full command description shown when running the command with // the "--help" option - ->setHelp("Test seacms via command line.\n". - "(Only if phpunit available)\n") + ->setHelp( + <<addOption('toto', '', InputOption::VALUE_NONE, 'Display toto text') - ->addOption('text', 't', InputOption::VALUE_REQUIRED, 'Display the text') + ->addArgument('type', InputArgument::OPTIONAL, 'phpunit|test') + ->addArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'phpunit command line params|other test params') ; } @@ -49,14 +62,77 @@ use Symfony\Component\Console\Output\OutputInterface; { $output->writeln('Testing seacms'); - $totoOption = $input->getOption('toto'); - $textOption = $input->getOption('text'); + $args = $input->getArgument('args'); - if ($totoOption){ - $output->writeln('toto'); + switch ($input->getArgument('type')) { + case 'test': + $empty = true; + foreach([ + 'vendor/seacms/app/tests/', + 'vendor/seacms/composer-plugin/tests/', + 'vendor/picocms/plugins/PicoContentEditor/tests', + 'vendor/picocms/plugins/SeacmsApi/tests', + 'vendor/picocms/plugins/SeacmsAuth/tests', + ] as $path){ + if (is_dir($path)){ + $empty = false; + $args = [ + $path + ]; + $code = $this->runPhpUnitIfAvailable($input->getArgument('args'),$output); + if ($code != Command::SUCCESS){ + return $code; + } + } + } + if ($empty){ + $output->writeln('Nothing to test'); + } + return Command::SUCCESS; + case 'phpunit': + return $this->runPhpUnitIfAvailable($input->getArgument('args'),$output); + + default: + // default display help + $inputInternal = new ArrayInput([ + 'command' => 'help', + 'command_name' => 'seacms-test' + ]); + (new Application())->run($inputInternal,$output); + return Command::SUCCESS; } - if ($textOption){ - $output->writeln($textOption); + } + + /** + * test if phpunit available + * @return bool + */ + protected function canRunPhpunit(): bool + { + return is_file('vendor/bin/phpunit'); + } + + /** + * run php unit if available otherwise output a message + * @param array $args + * @param OutputInterface $output + * @return int $code + */ + protected function runPhpUnitIfAvailable(array $args, OutputInterface $output): int + { + if ($this->canRunPhpunit()){ + $def = [ + 'command' => 'exec', + 'binary' => 'phpunit' + ]; + if (!empty($args)){ + $def['args'] = array_map(function($v){ + return ($v == '--pversion') ? '--version' : $v; // hack to prevent bad caught + },$args); + } + (new Application())->run(new ArrayInput($def),$output); + } else { + $output->writeln('phpunit not installed !'); } return Command::SUCCESS; }