feat(Test): define CLI
This commit is contained in:
parent
379c9ba842
commit
42526c39b0
@ -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(
|
||||
<<<STR
|
||||
Test seacms via command line.
|
||||
(Only if phpunit available)
|
||||
|
||||
->addOption('toto', '', InputOption::VALUE_NONE, 'Display toto text')
|
||||
->addOption('text', 't', InputOption::VALUE_REQUIRED, 'Display the text')
|
||||
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)
|
||||
|
||||
->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('<info>Testing seacms</info>');
|
||||
|
||||
$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 ($textOption){
|
||||
$output->writeln($textOption);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user