Compare commits

..

10 Commits

Author SHA1 Message Date
Jérémy Dufraisse
0511638d1a feat(comment) be able to pass parameters to phpunit 2023-04-03 12:12:31 +02:00
Jérémy Dufraisse
adc22cdee6 feat(composer): allow php ^7.2.5 2023-03-23 11:42:37 +01:00
Jérémy Dufraisse
00a9c6d298 fix(--verbose): test 2023-03-20 16:11:14 +01:00
Jérémy Dufraisse
1935601cc7 fix(test/comand): try to display error message from phpunit 2023-03-20 09:29:13 +01:00
Jérémy Dufraisse
f576ad2bcb fix(phpunit): args and autoload 2023-03-19 13:21:58 +01:00
Jérémy Dufraisse
42526c39b0 feat(Test): define CLI 2023-03-19 12:40:21 +01:00
Jérémy Dufraisse
379c9ba842 fix(composer): change phpunit rev 2023-03-19 11:34:31 +01:00
Jérémy Dufraisse
2dd0f53c44 feat(phpunit): require dev 2023-03-19 10:30:59 +01:00
Jérémy Dufraisse
bb11cec5c7 fix(CommandProvider): use it 2023-03-19 10:21:43 +01:00
Jérémy Dufraisse
eb547ef962 fix(Command): rename it to prevent errors with default name command 2023-03-19 10:11:39 +01:00
4 changed files with 163 additions and 12 deletions

View File

@ -37,8 +37,15 @@
"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,9 +7,16 @@
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
@ -17,7 +24,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Command extends BaseCommand class Command extends BaseCommand
{ {
/** /**
* set name * set name
*/ */
@ -30,11 +36,22 @@ 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)
->addOption('toto', '', InputOption::VALUE_NONE, 'Display toto text') Examples :
->addOption('text', 't', InputOption::VALUE_REQUIRED, 'Display the text') 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')
->addArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'phpunit command line params|other test params')
; ;
} }
@ -49,14 +66,116 @@ 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('test');
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;
$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

@ -0,0 +1,25 @@
<?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\\Command', 'Composer\Plugin\Capability\CommandProvider' => 'Seacms\\Command\\CommandProvider',
]; ];
} }
/* === implements EventsSubscriberInterface === */ /* === implements EventsSubscriberInterface === */