mirror of
https://github.com/pestphp/pest.git
synced 2026-03-11 10:17:23 +01:00
refactor: --ci option
This commit is contained in:
@ -80,7 +80,7 @@
|
|||||||
"Pest\\Plugins\\Coverage",
|
"Pest\\Plugins\\Coverage",
|
||||||
"Pest\\Plugins\\Init",
|
"Pest\\Plugins\\Init",
|
||||||
"Pest\\Plugins\\Version",
|
"Pest\\Plugins\\Version",
|
||||||
"Pest\\Plugins\\Context"
|
"Pest\\Plugins\\Environment"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"laravel": {
|
"laravel": {
|
||||||
|
|||||||
@ -12,9 +12,6 @@ interface HandlesArguments
|
|||||||
/**
|
/**
|
||||||
* Allows to handle custom command line arguments.
|
* 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
|
* @param array<int, string> $arguments
|
||||||
*
|
*
|
||||||
* @return array<int, string> the updated list of arguments
|
* @return array<int, string> the updated list of arguments
|
||||||
|
|||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
67
src/Plugins/Environment.php
Normal file
67
src/Plugins/Environment.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,7 +11,7 @@ use Pest\Exceptions\TestAlreadyExist;
|
|||||||
use Pest\Exceptions\TestCaseAlreadyInUse;
|
use Pest\Exceptions\TestCaseAlreadyInUse;
|
||||||
use Pest\Exceptions\TestCaseClassOrTraitNotFound;
|
use Pest\Exceptions\TestCaseClassOrTraitNotFound;
|
||||||
use Pest\Factories\TestCaseFactory;
|
use Pest\Factories\TestCaseFactory;
|
||||||
use Pest\Plugins\Context;
|
use Pest\Plugins\Environment;
|
||||||
use Pest\Support\Reflection;
|
use Pest\Support\Reflection;
|
||||||
use Pest\Support\Str;
|
use Pest\Support\Str;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
@ -120,7 +120,7 @@ final class TestRepository
|
|||||||
*/
|
*/
|
||||||
private function testsUsingOnly(): array
|
private function testsUsingOnly(): array
|
||||||
{
|
{
|
||||||
if (Context::getInstance()->env === Context::ENV_CI) {
|
if (Environment::name() === Environment::CI) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -657,7 +657,7 @@
|
|||||||
✓ it show the actual dataset of multiple non-named datasets in their description
|
✓ it show the actual dataset of multiple non-named datasets in their description
|
||||||
✓ it show the correct description for mixed named and not-named datasets
|
✓ it show the correct description for mixed named and not-named datasets
|
||||||
|
|
||||||
PASS Tests\Unit\Plugins\Context
|
PASS Tests\Unit\Plugins\Environment
|
||||||
✓ environment is set to CI when --ci option is used
|
✓ environment is set to CI when --ci option is used
|
||||||
✓ environment is set to Local when --ci option is not used
|
✓ environment is set to Local when --ci option is not used
|
||||||
|
|
||||||
|
|||||||
@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use Pest\Plugins\Context;
|
|
||||||
|
|
||||||
test('environment is set to CI when --ci option is used', function () {
|
|
||||||
$old_env = Context::getInstance()->env;
|
|
||||||
|
|
||||||
$plugin = new Context();
|
|
||||||
|
|
||||||
$plugin->handleArguments(['foo', '--ci', 'bar']);
|
|
||||||
|
|
||||||
expect(Context::getInstance()->env)->toBe(Context::ENV_CI);
|
|
||||||
|
|
||||||
Context::getInstance()->env = $old_env;
|
|
||||||
});
|
|
||||||
|
|
||||||
test('environment is set to Local when --ci option is not used', function () {
|
|
||||||
$plugin = new Context();
|
|
||||||
|
|
||||||
$plugin->handleArguments(['foo', 'bar', 'baz']);
|
|
||||||
|
|
||||||
expect(Context::getInstance()->env)->toBe(Context::ENV_LOCAL);
|
|
||||||
});
|
|
||||||
23
tests/Unit/Plugins/Environment.php
Normal file
23
tests/Unit/Plugins/Environment.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pest\Plugins\Environment;
|
||||||
|
|
||||||
|
test('environment is set to CI when --ci option is used', function () {
|
||||||
|
$previousName = Environment::name();
|
||||||
|
|
||||||
|
$plugin = new Environment();
|
||||||
|
|
||||||
|
$plugin->handleArguments(['foo', '--ci', 'bar']);
|
||||||
|
|
||||||
|
expect(Environment::name())->toBe(Environment::CI);
|
||||||
|
|
||||||
|
Environment::name($previousName);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('environment is set to Local when --ci option is not used', function () {
|
||||||
|
$plugin = new Environment();
|
||||||
|
|
||||||
|
$plugin->handleArguments(['foo', 'bar', 'baz']);
|
||||||
|
|
||||||
|
expect(Environment::name())->toBe(Environment::LOCAL);
|
||||||
|
});
|
||||||
@ -3,7 +3,7 @@
|
|||||||
use Pest\Exceptions\DatasetMissing;
|
use Pest\Exceptions\DatasetMissing;
|
||||||
use Pest\Exceptions\TestAlreadyExist;
|
use Pest\Exceptions\TestAlreadyExist;
|
||||||
use Pest\Factories\TestCaseFactory;
|
use Pest\Factories\TestCaseFactory;
|
||||||
use Pest\Plugins\Context;
|
use Pest\Plugins\Environment;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
|
|
||||||
it('does not allow to add the same test description twice', function () {
|
it('does not allow to add the same test description twice', function () {
|
||||||
@ -38,7 +38,7 @@ it('can return an array of all test suite filenames', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('can filter the test suite filenames to those with the only method', function () {
|
it('can filter the test suite filenames to those with the only method', function () {
|
||||||
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
$testSuite = new TestSuite(getcwd(), 'tests');
|
||||||
$test = function () {};
|
$test = function () {};
|
||||||
|
|
||||||
$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
|
$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
|
||||||
@ -53,8 +53,8 @@ it('can filter the test suite filenames to those with the only method', function
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not filter the test suite filenames to those with the only method when working in CI pipeline', function () {
|
it('does not filter the test suite filenames to those with the only method when working in CI pipeline', function () {
|
||||||
$old_env = Context::getInstance()->env;
|
$previousEnvironment = Environment::name();
|
||||||
Context::getInstance()->env = Context::ENV_CI;
|
Environment::name(Environment::CI);
|
||||||
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
||||||
|
|
||||||
$test = function () {};
|
$test = function () {};
|
||||||
@ -70,5 +70,5 @@ it('does not filter the test suite filenames to those with the only method when
|
|||||||
'Baz/Bar/Boo.php',
|
'Baz/Bar/Boo.php',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Context::getInstance()->env = $old_env;
|
Environment::name($previousEnvironment);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user