From 6c4be0190e9493702a976b996bbbf5150cc6bb53 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 21 May 2020 21:44:05 +0200 Subject: [PATCH] feat: adds plugin uses api --- composer.json | 5 ++++- src/Plugin.php | 30 ++++++++++++++++++++++++++++++ src/TestSuite.php | 8 +++++++- tests/Autoload.php | 10 ++++++++++ tests/Plugins/Traits.php | 3 +++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/Plugin.php create mode 100644 tests/Plugins/Traits.php diff --git a/composer.json b/composer.json index cc08702c..3f9a60ae 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,10 @@ "autoload-dev": { "psr-4": { "Tests\\": "tests/PHPUnit/" - } + }, + "files": [ + "tests/Autoload.php" + ] }, "require-dev": { "ergebnis/phpstan-rules": "^0.14.4", diff --git a/src/Plugin.php b/src/Plugin.php new file mode 100644 index 00000000..f71d7ab6 --- /dev/null +++ b/src/Plugin.php @@ -0,0 +1,30 @@ + + * + * @internal + */ + public static $callables = []; + + /** + * Lazy loads an `uses` call on the context of plugins. + */ + public static function uses(...$traits): void + { + self::$callables[] = function () use ($traits) { + uses(...$traits)->in(TestSuite::getInstance()->rootPath . DIRECTORY_SEPARATOR . 'tests'); + }; + } +} diff --git a/src/TestSuite.php b/src/TestSuite.php index 1bd52e04..311fa644 100644 --- a/src/TestSuite.php +++ b/src/TestSuite.php @@ -106,7 +106,13 @@ final class TestSuite public static function getInstance(string $rootPath = null): TestSuite { if (is_string($rootPath)) { - return self::$instance ?? self::$instance = new TestSuite($rootPath); + self::$instance = new TestSuite($rootPath); + + foreach (Plugin::$callables as $callable) { + $callable(); + } + + return self::$instance; } if (self::$instance === null) { diff --git a/tests/Autoload.php b/tests/Autoload.php index e796a649..f2a37c5f 100644 --- a/tests/Autoload.php +++ b/tests/Autoload.php @@ -3,3 +3,13 @@ if (class_exists(NunoMaduro\Collision\Provider::class)) { (new NunoMaduro\Collision\Provider())->register(); } + +trait PluginTrait +{ + function assertPluginTraitGotRegistered(): void + { + assertTrue(true); + } +} + +Pest\Plugin::uses(PluginTrait::class); diff --git a/tests/Plugins/Traits.php b/tests/Plugins/Traits.php new file mode 100644 index 00000000..bd318c28 --- /dev/null +++ b/tests/Plugins/Traits.php @@ -0,0 +1,3 @@ +assertPluginTraitGotRegistered();