refacto: bootstrappers and parallel minor stuff

This commit is contained in:
Nuno Maduro
2023-02-12 23:40:05 +00:00
parent 8a58e984fe
commit 735f131222
7 changed files with 48 additions and 27 deletions

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,7 @@ use Pest\Contracts\Bootstrapper;
final class BootExceptionHandler implements Bootstrapper final class BootExceptionHandler implements Bootstrapper
{ {
/** /**
* Boots the Exception Handler. * Boots the "Collision" exception handler.
*/ */
public function boot(): void public function boot(): void
{ {

View File

@ -19,7 +19,7 @@ use SebastianBergmann\FileIterator\Facade as PhpUnitFileIterator;
final class BootFiles implements Bootstrapper final class BootFiles implements Bootstrapper
{ {
/** /**
* The Pest convention. * The structure of the tests directory.
* *
* @var array<int, string> * @var array<int, string>
*/ */
@ -32,7 +32,7 @@ final class BootFiles implements Bootstrapper
]; ];
/** /**
* Boots the Subscribers. * Boots the structure of the tests directory.
*/ */
public function boot(): void public function boot(): void
{ {

View File

@ -23,7 +23,7 @@ final class BootOverrides implements Bootstrapper
]; ];
/** /**
* Boots the Subscribers. * Boots the list of files to be overridden.
*/ */
public function boot(): void public function boot(): void
{ {

View File

@ -16,7 +16,7 @@ use PHPUnit\Event\Subscriber;
final class BootSubscribers implements Bootstrapper final class BootSubscribers implements Bootstrapper
{ {
/** /**
* The Kernel subscribers. * The list of Subscribers.
* *
* @var array<int, class-string<Subscriber>> * @var array<int, class-string<Subscriber>>
*/ */
@ -27,7 +27,7 @@ final class BootSubscribers implements Bootstrapper
]; ];
/** /**
* Creates a new Subscriber instance. * Creates a new instance of the Boot Subscribers.
*/ */
public function __construct( public function __construct(
private readonly Container $container, private readonly Container $container,
@ -35,7 +35,7 @@ final class BootSubscribers implements Bootstrapper
} }
/** /**
* Boots the Subscribers. * Boots the list of Subscribers.
*/ */
public function boot(): void public function boot(): void
{ {

View File

@ -13,6 +13,9 @@ use Symfony\Component\Console\Output\OutputInterface;
*/ */
final class BootView implements Bootstrapper final class BootView implements Bootstrapper
{ {
/**
* Creates a new instance of the Boot View.
*/
public function __construct( public function __construct(
private readonly OutputInterface $output private readonly OutputInterface $output
) { ) {

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pest\Plugins\Parallel\Handlers; namespace Pest\Plugins\Parallel\Handlers;
use Closure;
use Composer\InstalledVersions; use Composer\InstalledVersions;
use Illuminate\Testing\ParallelRunner; use Illuminate\Testing\ParallelRunner;
use ParaTest\Options; use ParaTest\Options;
@ -22,38 +23,53 @@ final class Laravel implements HandlesArguments
public function handleArguments(array $arguments): array public function handleArguments(array $arguments): array
{ {
if (! self::isALaravelApplication()) { return self::whenUsingLaravel($arguments, function (array $arguments): array {
$this->ensureRunnerIsResolvable();
$arguments = $this->ensureEnvironmentVariables($arguments);
return $this->ensureRunner($arguments);
});
}
/**
* Executes the given closure when running Laravel.
*
* @param array<int, string> $arguments
* @param CLosure(array<int, string>): array<int, string> $closure
* @return array<int, string>
*/
private static function whenUsingLaravel(array $arguments, Closure $closure): array
{
$isLaravelApplication = InstalledVersions::isInstalled('laravel/framework', false);
$isLaravelPackage = class_exists(\Orchestra\Testbench\TestCase::class);
if ($isLaravelApplication) {
return $closure($arguments);
}
if ($isLaravelPackage) {
return $closure($arguments);
}
return $arguments; return $arguments;
} }
$this->setLaravelParallelRunner(); /**
* Ensures the runner is resolvable.
$arguments = $this->setEnvironmentVariables($arguments); */
private function ensureRunnerIsResolvable(): void
return $this->useLaravelRunner($arguments);
}
private function setLaravelParallelRunner(): void
{ {
ParallelRunner::resolveRunnerUsing( // @phpstan-ignore-line ParallelRunner::resolveRunnerUsing( // @phpstan-ignore-line
fn (Options $options, OutputInterface $output): RunnerInterface => new WrapperRunner($options, $output) fn (Options $options, OutputInterface $output): RunnerInterface => new WrapperRunner($options, $output)
); );
} }
private static function isALaravelApplication(): bool
{
if (! InstalledVersions::isInstalled('laravel/framework', false)) {
return false;
}
return ! class_exists(\Orchestra\Testbench\TestCase::class);
}
/** /**
* Ensures the environment variables are set.
*
* @param array<int, string> $arguments * @param array<int, string> $arguments
* @return array<int, string> * @return array<int, string>
*/ */
private function setEnvironmentVariables(array $arguments): array private function ensureEnvironmentVariables(array $arguments): array
{ {
$_ENV['LARAVEL_PARALLEL_TESTING'] = 1; $_ENV['LARAVEL_PARALLEL_TESTING'] = 1;
@ -71,10 +87,12 @@ final class Laravel implements HandlesArguments
} }
/** /**
* Ensure the runner is set.
*
* @param array<int, string> $arguments * @param array<int, string> $arguments
* @return array<int, string> * @return array<int, string>
*/ */
private function useLaravelRunner(array $arguments): array private function ensureRunner(array $arguments): array
{ {
foreach ($arguments as $value) { foreach ($arguments as $value) {
if (str_starts_with($value, '--runner')) { if (str_starts_with($value, '--runner')) {