Compare commits

..

No commits in common. "0511638d1a972aed2497f6ab4ddc3d0108a0eaae" and "817ba4501d2f97106c048c677d209c118a7b06a7" have entirely different histories.

4 changed files with 12 additions and 163 deletions

View File

@ -37,15 +37,8 @@
"plugin-modifies-downloads": true "plugin-modifies-downloads": true
}, },
"config": { "config": {
"platform": {
"php": "7.2.5"
},
"platform-check": true,
"allow-plugins": { "allow-plugins": {
"topfloor/composer-cleanup-vcs-dirs": false "topfloor/composer-cleanup-vcs-dirs": false
} }
},
"require-dev": {
"phpunit/phpunit": "^8.5"
} }
} }

View File

@ -7,16 +7,9 @@
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\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
/** /**
* Command to lauch tests from command line accross all seacms dependencies * Command to lauch tests from command line accross all seacms dependencies
@ -24,6 +17,7 @@ use Throwable;
class Command extends BaseCommand class Command extends BaseCommand
{ {
/** /**
* set name * set name
*/ */
@ -36,22 +30,11 @@ use Throwable;
// 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( ->setHelp("Test seacms via command line.\n".
<<<STR "(Only if phpunit available)\n")
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
composer seacms-test test -- --filter AppTest::testInit : run tests with phpunit parameters
STR)
->addArgument('type', InputArgument::OPTIONAL, 'phpunit|test') ->addOption('toto', '', InputOption::VALUE_NONE, 'Display toto text')
->addArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'phpunit command line params|other test params') ->addOption('text', 't', InputOption::VALUE_REQUIRED, 'Display the text')
; ;
} }
@ -66,116 +49,14 @@ use Throwable;
{ {
$output->writeln('<info>Testing seacms</info>'); $output->writeln('<info>Testing seacms</info>');
$args = $input->getArgument('args'); $totoOption = $input->getOption('toto');
$textOption = $input->getOption('test');
switch ($input->getArgument('type')) { if ($totoOption){
case 'test': $output->writeln('toto');
$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;
$newargs = array_merge([
'--bootstrap',
'vendor/autoload.php',
$path
],$args);
$code = $this->runPhpUnitIfAvailable($newargs,$output);
if ($code != Command::SUCCESS){
return $code;
}
}
}
if ($empty){
$output->writeln('<info>Nothing to test</info>');
}
return Command::SUCCESS;
case 'phpunit':
return $this->runPhpUnitIfAvailable($args,$output);
default:
// default display help
return $this->runApplicationSafe([
'command' => 'help',
'command_name' => 'seacms-test'
],$output);
} }
} if ($textOption){
$output->writeln($textOption);
/**
* run safe new Application
* @param array $def
* @param OutputInterface $output
* @return int errorCode
*/
protected function runApplicationSafe(array $def, OutputInterface $output): int
{
$app = new Application();
$app->setCatchExceptions(false); // force not catching exceptions
$app->setAutoExit(false); // force not exiting
try {
// if ($output instanceof ConsoleOutputInterface){
// $errBuffer = new BufferedOutput();
// $output->setErrorOutput($errBuffer);
// }
$code = $app->run(new ArrayInput($def),$output);
if ($code != 0){
$output->writeln("<info>Commands {$def['command']} return code $code</info>");
// if ($errBuffer){
// $errorOutput = $errBuffer->fetch();
// $output->writeln("<warning>Error</warning>");
// $output->writeln($errorOutput);
// }
return Command::FAILURE;
}
} catch (Throwable $th){
$output->writeln("<info>Something went wrong ({$th->getMessage()} - code :{$th->getCode()} )</info>");
$output->writeln("<info>In file {$th->getFile()}(line {$th->getLine()})</infos>");
return Command::FAILURE;
}
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',
// '-v' => '-v', // only for debug command exec
];
$args = array_merge([
// '-v' => '--verbose', // verbse phpunit
// '--debug' => '--debug', // debug phpunit
],$args);
if (!empty($args)){
$def['args'] = array_map(function($v){
return ($v == '--pversion') ? '--version' : $v; // hack to prevent bad caught
},$args);
}
return $this->runApplicationSafe($def,$output);
} else {
$output->writeln('<info>phpunit not installed !</info>');
} }
return Command::SUCCESS; return Command::SUCCESS;
} }

View File

@ -1,25 +0,0 @@
<?php
/**
* SPDX-License-Identifier: EUPL-1.2
* Authors: see /README.md
*/
namespace Seacms\Command;
use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability;
use Seacms\Command\Command;
/**
* Provide list of commands
*/
class CommandProvider implements CommandProviderCapability
{
public function getCommands()
{
return [
new Command()
];
}
}

View File

@ -233,7 +233,7 @@ class Plugin extends ParentPlugin
public function getCapabilities() public function getCapabilities()
{ {
return [ return [
'Composer\Plugin\Capability\CommandProvider' => 'Seacms\\Command\\CommandProvider', 'Composer\Plugin\Capability\CommandProvider' => 'Seacms\\Command\\Command',
]; ];
} }
/* === implements EventsSubscriberInterface === */ /* === implements EventsSubscriberInterface === */