diff --git a/bin/pest b/bin/pest index 9b66a2b6..ddb68f81 100755 --- a/bin/pest +++ b/bin/pest @@ -3,8 +3,10 @@ use Pest\Actions\ValidatesEnvironment; use Pest\Console\Command; +use Pest\Support\Container; use Pest\TestSuite; use Symfony\Component\Console\Output\ConsoleOutput; +use Symfony\Component\Console\Output\OutputInterface; (static function () { // Used when Pest is required using composer. @@ -24,8 +26,13 @@ use Symfony\Component\Console\Output\ConsoleOutput; $rootPath = getcwd(); $testSuite = TestSuite::getInstance($rootPath); + $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, true); + + $container = Container::getInstance(); + $container->add(TestSuite::class, $testSuite); + $container->add(OutputInterface::class, $output); ValidatesEnvironment::in($testSuite); - exit((new Command($testSuite, new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, true)))->run($_SERVER['argv'])); + exit($container->get(Command::class)->run($_SERVER['argv'])); })(); diff --git a/src/Console/Command.php b/src/Console/Command.php index 290dbe04..d7481372 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -62,7 +62,7 @@ final class Command extends BaseCommand /** @var HandlesArguments $plugin */ foreach ($plugins as $plugin) { - $argv = $plugin->handleArguments($this->testSuite, $argv); + $argv = $plugin->handleArguments($argv); } /* @@ -134,7 +134,7 @@ final class Command extends BaseCommand /** @var AddsOutput $plugin */ foreach ($plugins as $plugin) { - $result = $plugin->addOutput($this->testSuite, $this->output, $result); + $result = $plugin->addOutput($result); } exit($result); diff --git a/src/Contracts/Plugins/AddsOutput.php b/src/Contracts/Plugins/AddsOutput.php index 543eace9..a105e0d0 100644 --- a/src/Contracts/Plugins/AddsOutput.php +++ b/src/Contracts/Plugins/AddsOutput.php @@ -4,9 +4,6 @@ declare(strict_types=1); namespace Pest\Contracts\Plugins; -use Pest\TestSuite; -use Symfony\Component\Console\Output\OutputInterface; - /** * @internal */ @@ -15,5 +12,5 @@ interface AddsOutput /** * Allows to add custom output after the test suite was executed. */ - public function addOutput(TestSuite $testSuite, OutputInterface $output, int $testReturnCode): int; + public function addOutput(int $testReturnCode): int; } diff --git a/src/Contracts/Plugins/HandlesArguments.php b/src/Contracts/Plugins/HandlesArguments.php index b7f09068..e9c1b725 100644 --- a/src/Contracts/Plugins/HandlesArguments.php +++ b/src/Contracts/Plugins/HandlesArguments.php @@ -4,8 +4,6 @@ declare(strict_types=1); namespace Pest\Contracts\Plugins; -use Pest\TestSuite; - /** * @internal */ @@ -21,5 +19,5 @@ interface HandlesArguments * * @return array the updated list of arguments */ - public function handleArguments(TestSuite $testSuite, array $arguments): array; + public function handleArguments(array $arguments): array; } diff --git a/src/Support/Container.php b/src/Support/Container.php index bead50ed..85a5f9d2 100644 --- a/src/Support/Container.php +++ b/src/Support/Container.php @@ -13,11 +13,28 @@ use ReflectionParameter; */ final class Container { + /** + * @var self + */ + private static $instance; + /** * @var array */ private $instances = []; + /** + * Gets a new or already existing container. + */ + public static function getInstance(): self + { + if (static::$instance === null) { + static::$instance = new static(); + } + + return static::$instance; + } + /** * Gets a dependency from the container. * diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index ae43a75c..0d97fd98 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -143,10 +143,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success -<<<<<<< HEAD - Tests: 6 skipped, 71 passed - Time: 2.89s -======= - Tests: 6 skipped, 76 passed - Time: 3.32s ->>>>>>> 84c9078... Add basic container implementation + Tests: 6 skipped, 78 passed + Time: 3.37s