refactor: --ci option

This commit is contained in:
Nuno Maduro
2021-09-25 13:29:11 +01:00
parent a6e34d204c
commit 7f38de11b7
9 changed files with 99 additions and 82 deletions

View File

@ -12,9 +12,6 @@ interface HandlesArguments
/**
* Allows to handle custom command line arguments.
*
* PLEASE NOTE: it is necessary to remove any custom argument from the array
* because otherwise the application will complain about them
*
* @param array<int, string> $arguments
*
* @return array<int, string> the updated list of arguments

View File

@ -1,47 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
/**
* @internal
*/
final class Context implements HandlesArguments
{
public const ENV_CI = 'ci';
public const ENV_LOCAL = 'local';
/**
* @var \Pest\Plugins\Context
*/
private static $instance;
/**
* @var string
*/
public $env = 'local';
public static function getInstance(): Context
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function handleArguments(array $arguments): array
{
foreach ($arguments as $index => $argument) {
if ($argument === '--ci') {
unset($arguments[$index]);
self::getInstance()->env = 'ci';
}
}
return array_values($arguments);
}
}

View File

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
/**
* @internal
*/
final class Environment implements HandlesArguments
{
/**
* The continuous integration environment.
*/
public const CI = 'ci';
/**
* The local environment.
*/
public const LOCAL = 'local';
/**
* @var \Pest\Plugins\Environment|null
*/
private static $instance;
/**
* The current environment.
*
* @var string|null
*/
private static $name;
/**
* Allows to handle custom command line arguments.
*
* @param array<int, string> $arguments
*
* @return array<int, string> the updated list of arguments
*/
public function handleArguments(array $arguments): array
{
foreach ($arguments as $index => $argument) {
if ($argument === '--ci') {
unset($arguments[$index]);
self::$name = self::CI;
}
}
return array_values($arguments);
}
/**
* Gets the environment name.
*/
public static function name(string $name = null): string
{
if (is_string($name)) {
self::$name = $name;
}
return self::$name ?? self::LOCAL;
}
}

View File

@ -11,7 +11,7 @@ use Pest\Exceptions\TestAlreadyExist;
use Pest\Exceptions\TestCaseAlreadyInUse;
use Pest\Exceptions\TestCaseClassOrTraitNotFound;
use Pest\Factories\TestCaseFactory;
use Pest\Plugins\Context;
use Pest\Plugins\Environment;
use Pest\Support\Reflection;
use Pest\Support\Str;
use Pest\TestSuite;
@ -120,7 +120,7 @@ final class TestRepository
*/
private function testsUsingOnly(): array
{
if (Context::getInstance()->env === Context::ENV_CI) {
if (Environment::name() === Environment::CI) {
return [];
}