feat(Test): define CLI

master
dufraissejeremy 2 years ago
parent 379c9ba842
commit 42526c39b0
  1. 96
      src/commands/Command.php

@ -7,7 +7,10 @@
namespace Seacms\Command; namespace Seacms\Command;
use Composer\Console\Application;
use Composer\Command\BaseCommand; 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\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; 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 full command description shown when running the command with
// the "--help" option // the "--help" option
->setHelp("Test seacms via command line.\n". ->setHelp(
"(Only if phpunit available)\n") <<<STR
Test seacms via command line.
(Only if phpunit available)
Examples :
composer seacms-test phpunit -- -h : display phpunit help (do not forget --)
composer seacms-test phpunit -- --pversion : display phpunit version
composer seacms-test help : show this help
composer seacms-test : show this help
composer seacms-test test : run tests
STR)
->addOption('toto', '', InputOption::VALUE_NONE, 'Display toto text') ->addArgument('type', InputArgument::OPTIONAL, 'phpunit|test')
->addOption('text', 't', InputOption::VALUE_REQUIRED, 'Display the text') ->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('<info>Testing seacms</info>'); $output->writeln('<info>Testing seacms</info>');
$totoOption = $input->getOption('toto'); $args = $input->getArgument('args');
$textOption = $input->getOption('text');
if ($totoOption){ switch ($input->getArgument('type')) {
$output->writeln('toto'); 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('<info>Nothing to test</info>');
}
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('<info>phpunit not installed !</info>');
} }
return Command::SUCCESS; return Command::SUCCESS;
} }

Loading…
Cancel
Save