Fixes --version and --help

This commit is contained in:
Nuno Maduro
2022-09-17 23:47:47 +01:00
parent 08b62f6633
commit 0e0e2adfbe
26 changed files with 511 additions and 86 deletions

View File

@ -10,7 +10,7 @@ use Pest\Plugin\Loader;
/**
* @internal
*/
final class AddsOutput
final class CallsAddsOutput
{
/**
* Executes the Plugin action.

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins\Actions;
use Pest\Contracts\Plugins;
use Pest\Plugin\Loader;
/**
* @internal
*/
final class CallsBoot
{
/**
* Executes the Plugin action.
*
* Provides an opportunity for any plugins to boot..
*/
public function __invoke(): void
{
$plugins = Loader::getPlugins(Plugins\Bootable::class);
/** @var Plugins\Bootable $plugin */
foreach ($plugins as $plugin) {
$plugin->boot();
}
}
}

View File

@ -10,7 +10,7 @@ use Pest\Plugin\Loader;
/**
* @internal
*/
final class HandleArguments
final class CallsHandleArguments
{
/**
* Executes the Plugin action.

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins\Actions;
use Pest\Contracts\Plugins;
use Pest\Plugin\Loader;
/**
* @internal
*/
final class CallsShutdown
{
/**
* Executes the Plugin action.
*
* Provides an opportunity for any plugins to shutdown.
*/
public function __invoke(): void
{
$plugins = Loader::getPlugins(Plugins\Shutdownable::class);
/** @var Plugins\Shutdownable $plugin */
foreach ($plugins as $plugin) {
$plugin->shutdown();
}
}
}

105
src/Plugins/Help.php Normal file
View File

@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Support\View;
use function Pest\version;
use PHPUnit\TextUI\Help as PHPUnitHelp;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class Help implements HandlesArguments
{
use Concerns\HandleArguments;
/**
* Creates a new Plugin instance.
*/
public function __construct(
private readonly OutputInterface $output
) {
// ..
}
/**
* {@inheritDoc}
*/
public function handleArguments(array $arguments): array
{
if ($this->hasArgument('--help', $arguments)) {
View::render('version', [
'version' => version(),
]);
View::render('usage');
foreach ($this->getContent() as $title => $options) {
if ($title === 'Usage') {
continue;
}
$this->output->writeln([
'',
sprintf(' <fg=blue;options=bold>%s:</>', mb_strtoupper($title)),
]);
foreach ($options as $option) {
if (! array_key_exists('arg', $option)) {
continue;
}
[
'arg' => $argument,
'desc' => $description,
] = $option;
View::render('components.two-column-detail', [
'left' => $argument,
'right' => $description,
]);
}
}
$this->output->write('', true);
exit(0);
}
return $arguments;
}
/**
* @return array<string, array<int, array{arg?: string, desc: string}>>
*/
private function getContent(): array
{
// Access the PHPUnit help class's private const HELP
$helpReflection = new \ReflectionClass(PHPUnitHelp::class);
/** @var array<string, array<int, array{arg: string, desc: string}>> $content */
$content = $helpReflection->getConstant('HELP_TEXT');
$content['Configuration'] = [[
'arg' => '--init',
'desc' => 'Initialise a standard Pest configuration',
]] + $content['Configuration'];
$content['Code Coverage'] = [
[
'arg' => '--coverage ',
'desc' => 'Generate code coverage report and output to standard output',
],
[
'arg' => '--coverage --min',
'desc' => 'Set the minimum required coverage percentage, and fail if not met',
],
] + $content['Code Coverage'];
return $content;
}
}

View File

@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Support\View;
use function Pest\version;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
@ -15,24 +15,17 @@ final class Version implements HandlesArguments
{
use Concerns\HandleArguments;
/**
* Creates a new Plugin instance.
*/
public function __construct(
private readonly OutputInterface $output
) {
// ..
}
/**
* {@inheritDoc}
*/
public function handleArguments(array $arguments): array
{
if ($this->hasArgument('--version', $arguments)) {
$this->output->writeln(
sprintf('Pest %s', version()),
);
View::render('version', [
'version' => version(),
]);
exit(0);
}
return $arguments;