mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
refactors to use a Plugin to parse --ci option
This commit is contained in:
7
bin/pest
7
bin/pest
@ -31,8 +31,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
$rootPath = dirname($autoloadPath, 2);
|
$rootPath = dirname($autoloadPath, 2);
|
||||||
$argv = new ArgvInput();
|
$argv = new ArgvInput();
|
||||||
|
|
||||||
$workingEnv = $argv->hasParameterOption('--ci') ? 'ci' : 'local';
|
$testSuite = TestSuite::getInstance($rootPath, $argv->getParameterOption('--test-directory', 'tests'));
|
||||||
$testSuite = TestSuite::getInstance($rootPath, $argv->getParameterOption('--test-directory', 'tests'), $workingEnv);
|
|
||||||
|
|
||||||
$isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never';
|
$isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never';
|
||||||
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);
|
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);
|
||||||
@ -46,9 +45,9 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
$args = $_SERVER['argv'];
|
$args = $_SERVER['argv'];
|
||||||
|
|
||||||
// Let's remove any arguments that PHPUnit does not understand
|
// Let's remove any arguments that PHPUnit does not understand
|
||||||
if ($argv->hasParameterOption(['--test-directory', '--ci'])) {
|
if ($argv->hasParameterOption('--test-directory')) {
|
||||||
foreach ($args as $key => $value) {
|
foreach ($args as $key => $value) {
|
||||||
if (strpos($value, '--test-directory') !== false || strpos($value, '--ci') !== false) {
|
if (strpos($value, '--test-directory') !== false) {
|
||||||
unset($args[$key]);
|
unset($args[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -79,7 +79,8 @@
|
|||||||
"plugins": [
|
"plugins": [
|
||||||
"Pest\\Plugins\\Coverage",
|
"Pest\\Plugins\\Coverage",
|
||||||
"Pest\\Plugins\\Init",
|
"Pest\\Plugins\\Init",
|
||||||
"Pest\\Plugins\\Version"
|
"Pest\\Plugins\\Version",
|
||||||
|
"Pest\\Plugins\\Context"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"laravel": {
|
"laravel": {
|
||||||
|
|||||||
47
src/Plugins/Context.php
Normal file
47
src/Plugins/Context.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,6 +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\Support\Reflection;
|
use Pest\Support\Reflection;
|
||||||
use Pest\Support\Str;
|
use Pest\Support\Str;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
@ -119,7 +120,7 @@ final class TestRepository
|
|||||||
*/
|
*/
|
||||||
private function testsUsingOnly(): array
|
private function testsUsingOnly(): array
|
||||||
{
|
{
|
||||||
if (TestSuite::getInstance()->workingEnv === 'ci') {
|
if (Context::getInstance()->env === Context::ENV_CI) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -73,13 +73,6 @@ final class TestSuite
|
|||||||
*/
|
*/
|
||||||
public $testPath;
|
public $testPath;
|
||||||
|
|
||||||
/**
|
|
||||||
* Holds the current working environment identifier.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $workingEnv;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds an instance of the test suite.
|
* Holds an instance of the test suite.
|
||||||
*
|
*
|
||||||
@ -100,17 +93,15 @@ final class TestSuite
|
|||||||
|
|
||||||
$this->rootPath = (string) realpath($rootPath);
|
$this->rootPath = (string) realpath($rootPath);
|
||||||
$this->testPath = $testPath;
|
$this->testPath = $testPath;
|
||||||
$this->workingEnv = 'local';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current instance of the test suite.
|
* 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)) {
|
if (is_string($rootPath) && is_string($testPath)) {
|
||||||
self::$instance = new TestSuite($rootPath, $testPath);
|
self::$instance = new TestSuite($rootPath, $testPath);
|
||||||
self::$instance->workingEnv = $workingEnv ?? 'local';
|
|
||||||
|
|
||||||
foreach (Plugin::$callables as $callable) {
|
foreach (Plugin::$callables as $callable) {
|
||||||
$callable();
|
$callable();
|
||||||
|
|||||||
@ -623,6 +623,10 @@
|
|||||||
✓ 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
|
||||||
|
✓ environment is set to CI when --ci option is used
|
||||||
|
✓ environment is set to Local when --ci option is not used
|
||||||
|
|
||||||
PASS Tests\Unit\Plugins\Version
|
PASS Tests\Unit\Plugins\Version
|
||||||
✓ it outputs the version when --version is used
|
✓ it outputs the version when --version is used
|
||||||
✓ it do not outputs version when --version is not used
|
✓ it do not outputs version when --version is not used
|
||||||
@ -682,5 +686,5 @@
|
|||||||
✓ it is a test
|
✓ it is a test
|
||||||
✓ it uses correct parent class
|
✓ it uses correct parent class
|
||||||
|
|
||||||
Tests: 4 incompleted, 9 skipped, 448 passed
|
Tests: 4 incompleted, 9 skipped, 450 passed
|
||||||
|
|
||||||
23
tests/Unit/Plugins/Context.php
Normal file
23
tests/Unit/Plugins/Context.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?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);
|
||||||
|
});
|
||||||
@ -2,13 +2,15 @@
|
|||||||
|
|
||||||
use Pest\Exceptions\DatasetMissing;
|
use Pest\Exceptions\DatasetMissing;
|
||||||
use Pest\Exceptions\TestAlreadyExist;
|
use Pest\Exceptions\TestAlreadyExist;
|
||||||
|
use Pest\Factories\TestCaseFactory;
|
||||||
|
use Pest\Plugins\Context;
|
||||||
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 () {
|
||||||
$testSuite = new TestSuite(getcwd(), 'tests');
|
$testSuite = new TestSuite(getcwd(), 'tests');
|
||||||
$test = function () {};
|
$test = function () {};
|
||||||
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
|
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
|
||||||
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
|
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
|
||||||
})->throws(
|
})->throws(
|
||||||
TestAlreadyExist::class,
|
TestAlreadyExist::class,
|
||||||
sprintf('A test with the description `%s` already exist in the filename `%s`.', 'foo', __FILE__),
|
sprintf('A test with the description `%s` already exist in the filename `%s`.', 'foo', __FILE__),
|
||||||
@ -17,7 +19,7 @@ it('does not allow to add the same test description twice', function () {
|
|||||||
it('alerts users about tests with arguments but no input', function () {
|
it('alerts users about tests with arguments but no input', function () {
|
||||||
$testSuite = new TestSuite(getcwd(), 'tests');
|
$testSuite = new TestSuite(getcwd(), 'tests');
|
||||||
$test = function (int $arg) {};
|
$test = function (int $arg) {};
|
||||||
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
|
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
|
||||||
})->throws(
|
})->throws(
|
||||||
DatasetMissing::class,
|
DatasetMissing::class,
|
||||||
sprintf("A test with the description '%s' has %d argument(s) ([%s]) and no dataset(s) provided in %s", 'foo', 1, 'int $arg', __FILE__),
|
sprintf("A test with the description '%s' has %d argument(s) ([%s]) and no dataset(s) provided in %s", 'foo', 1, 'int $arg', __FILE__),
|
||||||
@ -26,8 +28,8 @@ it('alerts users about tests with arguments but no input', function () {
|
|||||||
it('can return an array of all test suite filenames', function () {
|
it('can return an array of all test suite filenames', function () {
|
||||||
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
||||||
$test = function () {};
|
$test = function () {};
|
||||||
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
|
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
|
||||||
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'bar', $test));
|
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'bar', $test));
|
||||||
|
|
||||||
expect($testSuite->tests->getFilenames())->toEqual([
|
expect($testSuite->tests->getFilenames())->toEqual([
|
||||||
__FILE__,
|
__FILE__,
|
||||||
@ -39,11 +41,11 @@ it('can filter the test suite filenames to those with the only method', function
|
|||||||
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
||||||
$test = function () {};
|
$test = function () {};
|
||||||
|
|
||||||
$testWithOnly = new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test);
|
$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
|
||||||
$testWithOnly->only = true;
|
$testWithOnly->only = true;
|
||||||
$testSuite->tests->set($testWithOnly);
|
$testSuite->tests->set($testWithOnly);
|
||||||
|
|
||||||
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));
|
$testSuite->tests->set(new TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));
|
||||||
|
|
||||||
expect($testSuite->tests->getFilenames())->toEqual([
|
expect($testSuite->tests->getFilenames())->toEqual([
|
||||||
__FILE__,
|
__FILE__,
|
||||||
@ -51,18 +53,22 @@ 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 () {
|
||||||
$testSuite = TestSuite::getInstance(getcwd(), 'tests', 'ci');
|
$old_env = Context::getInstance()->env;
|
||||||
|
Context::getInstance()->env = Context::ENV_CI;
|
||||||
|
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
||||||
|
|
||||||
$test = function () {};
|
$test = function () {};
|
||||||
|
|
||||||
$testWithOnly = new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test);
|
$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
|
||||||
$testWithOnly->only = true;
|
$testWithOnly->only = true;
|
||||||
$testSuite->tests->set($testWithOnly);
|
$testSuite->tests->set($testWithOnly);
|
||||||
|
|
||||||
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));
|
$testSuite->tests->set(new TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));
|
||||||
|
|
||||||
expect($testSuite->tests->getFilenames())->toEqual([
|
expect($testSuite->tests->getFilenames())->toEqual([
|
||||||
__FILE__,
|
__FILE__,
|
||||||
'Baz/Bar/Boo.php',
|
'Baz/Bar/Boo.php',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Context::getInstance()->env = $old_env;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user