refactors to use a Plugin to parse --ci option

This commit is contained in:
Fabio Ivona
2021-09-22 14:53:16 +02:00
parent 05c1c82ae2
commit 601c4b01fc
8 changed files with 100 additions and 28 deletions

47
src/Plugins/Context.php Normal file
View File

@ -0,0 +1,47 @@
<?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

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

View File

@ -73,13 +73,6 @@ final class TestSuite
*/
public $testPath;
/**
* Holds the current working environment identifier.
*
* @var string
*/
public $workingEnv;
/**
* Holds an instance of the test suite.
*
@ -100,17 +93,15 @@ final class TestSuite
$this->rootPath = (string) realpath($rootPath);
$this->testPath = $testPath;
$this->workingEnv = 'local';
}
/**
* Returns the current instance of the test suite.
*/
public static function getInstance(string $rootPath = null, string $testPath = null, string $workingEnv = null): TestSuite
public static function getInstance(string $rootPath = null, string $testPath = null): TestSuite
{
if (is_string($rootPath) && is_string($testPath)) {
self::$instance = new TestSuite($rootPath, $testPath);
self::$instance->workingEnv = $workingEnv ?? 'local';
self::$instance = new TestSuite($rootPath, $testPath);
foreach (Plugin::$callables as $callable) {
$callable();