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();