feat: adds plugin uses api

This commit is contained in:
Nuno Maduro
2020-05-21 21:44:05 +02:00
parent 5c6bb43d8d
commit 6c4be0190e
5 changed files with 54 additions and 2 deletions

View File

@ -34,7 +34,10 @@
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/PHPUnit/"
}
},
"files": [
"tests/Autoload.php"
]
},
"require-dev": {
"ergebnis/phpstan-rules": "^0.14.4",

30
src/Plugin.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Pest;
use Pest\TestSuite;
final class Plugin
{
/**
* The lazy callables to be executed
* once the test suite boots.
*
* @var array<int, callable>
*
* @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');
};
}
}

View File

@ -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) {

View File

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

3
tests/Plugins/Traits.php Normal file
View File

@ -0,0 +1,3 @@
<?php
it('allows global uses')->assertPluginTraitGotRegistered();