From 7f38de11b7d40b68d52cafa752e1d8395283609a Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sat, 25 Sep 2021 13:29:11 +0100 Subject: [PATCH] refactor: `--ci` option --- composer.json | 2 +- src/Contracts/Plugins/HandlesArguments.php | 3 - src/Plugins/Context.php | 47 --------------- src/Plugins/Environment.php | 67 ++++++++++++++++++++++ src/Repositories/TestRepository.php | 4 +- tests/.snapshots/success.txt | 2 +- tests/Unit/Plugins/Context.php | 23 -------- tests/Unit/Plugins/Environment.php | 23 ++++++++ tests/Unit/TestSuite.php | 10 ++-- 9 files changed, 99 insertions(+), 82 deletions(-) delete mode 100644 src/Plugins/Context.php create mode 100644 src/Plugins/Environment.php delete mode 100644 tests/Unit/Plugins/Context.php create mode 100644 tests/Unit/Plugins/Environment.php diff --git a/composer.json b/composer.json index cf6b5b79..74985375 100644 --- a/composer.json +++ b/composer.json @@ -80,7 +80,7 @@ "Pest\\Plugins\\Coverage", "Pest\\Plugins\\Init", "Pest\\Plugins\\Version", - "Pest\\Plugins\\Context" + "Pest\\Plugins\\Environment" ] }, "laravel": { diff --git a/src/Contracts/Plugins/HandlesArguments.php b/src/Contracts/Plugins/HandlesArguments.php index e9c1b725..3814150b 100644 --- a/src/Contracts/Plugins/HandlesArguments.php +++ b/src/Contracts/Plugins/HandlesArguments.php @@ -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 $arguments * * @return array the updated list of arguments diff --git a/src/Plugins/Context.php b/src/Plugins/Context.php deleted file mode 100644 index f7bafbe5..00000000 --- a/src/Plugins/Context.php +++ /dev/null @@ -1,47 +0,0 @@ - $argument) { - if ($argument === '--ci') { - unset($arguments[$index]); - self::getInstance()->env = 'ci'; - } - } - - return array_values($arguments); - } -} diff --git a/src/Plugins/Environment.php b/src/Plugins/Environment.php new file mode 100644 index 00000000..4bb80a0e --- /dev/null +++ b/src/Plugins/Environment.php @@ -0,0 +1,67 @@ + $arguments + * + * @return array 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; + } +} diff --git a/src/Repositories/TestRepository.php b/src/Repositories/TestRepository.php index f10012a9..73bddbc6 100644 --- a/src/Repositories/TestRepository.php +++ b/src/Repositories/TestRepository.php @@ -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 []; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index ccef8873..0c6a57ab 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -657,7 +657,7 @@ ✓ 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 - PASS Tests\Unit\Plugins\Context + PASS Tests\Unit\Plugins\Environment ✓ environment is set to CI when --ci option is used ✓ environment is set to Local when --ci option is not used diff --git a/tests/Unit/Plugins/Context.php b/tests/Unit/Plugins/Context.php deleted file mode 100644 index 13f2f783..00000000 --- a/tests/Unit/Plugins/Context.php +++ /dev/null @@ -1,23 +0,0 @@ -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); -}); diff --git a/tests/Unit/Plugins/Environment.php b/tests/Unit/Plugins/Environment.php new file mode 100644 index 00000000..4a969ad6 --- /dev/null +++ b/tests/Unit/Plugins/Environment.php @@ -0,0 +1,23 @@ +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); +}); diff --git a/tests/Unit/TestSuite.php b/tests/Unit/TestSuite.php index 7e58f93d..84212879 100644 --- a/tests/Unit/TestSuite.php +++ b/tests/Unit/TestSuite.php @@ -3,7 +3,7 @@ use Pest\Exceptions\DatasetMissing; use Pest\Exceptions\TestAlreadyExist; use Pest\Factories\TestCaseFactory; -use Pest\Plugins\Context; +use Pest\Plugins\Environment; use Pest\TestSuite; 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 () { - $testSuite = TestSuite::getInstance(getcwd(), 'tests'); + $testSuite = new TestSuite(getcwd(), 'tests'); $test = function () {}; $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 () { - $old_env = Context::getInstance()->env; - Context::getInstance()->env = Context::ENV_CI; + $previousEnvironment = Environment::name(); + Environment::name(Environment::CI); $testSuite = TestSuite::getInstance(getcwd(), 'tests'); $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', ]); - Context::getInstance()->env = $old_env; + Environment::name($previousEnvironment); });