diff --git a/src/Platform/Platform.php b/src/Platform/Platform.php index 7b90595..5b97cb3 100644 --- a/src/Platform/Platform.php +++ b/src/Platform/Platform.php @@ -47,6 +47,11 @@ public function init(string $type, array $params = []): void case Service::TYPE_TASK: $adapter = $params['adapter'] ?? new Generic(); $this->cli ??= new CLI($adapter); + + if (isset($params['container'])) { + $this->cli->setContainer($params['container']); + } + $this->initTasks($services); break; case Service::TYPE_GRAPHQL: diff --git a/tests/Platform/TestActionCLIInjection.php b/tests/Platform/TestActionCLIInjection.php new file mode 100644 index 0000000..faaac23 --- /dev/null +++ b/tests/Platform/TestActionCLIInjection.php @@ -0,0 +1,22 @@ +inject('test') + ->callback(function ($test) { + $this->action($test); + }); + } + + public function action($test) + { + echo $test; + } +} diff --git a/tests/Platform/TestServiceCLI.php b/tests/Platform/TestServiceCLI.php index 17e1428..d0af382 100644 --- a/tests/Platform/TestServiceCLI.php +++ b/tests/Platform/TestServiceCLI.php @@ -11,5 +11,6 @@ public function __construct() $this->type = Service::TYPE_TASK; $this->addAction('build', new TestActionCLI()); $this->addAction('build2', new TestActionCLI()); + $this->addAction('inject', new TestActionCLIInjection()); } } diff --git a/tests/e2e/CLITest.php b/tests/e2e/CLITest.php index f7c6ba1..bd89735 100644 --- a/tests/e2e/CLITest.php +++ b/tests/e2e/CLITest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\TestCase; use Utopia\CLI\Adapters\Generic; use Utopia\CLI\CLI; +use Utopia\DI\Container; use Utopia\Platform\Service; class CLITest extends TestCase @@ -33,6 +34,34 @@ public function testCLISetup() $result = ob_get_clean(); $this->assertEquals('me@example.com-item1-item2', $result); - $this->assertCount(2, $cli->getTasks()); + $this->assertCount(3, $cli->getTasks()); + } + + public function testCLISetupWithProvidedContainer() + { + $argv = $_SERVER['argv'] ?? []; + + try { + $_SERVER['argv'] = ['test.php', 'inject']; + ob_start(); + + $container = new Container(); + $container->set('test', fn () => 'test-value'); + + $platform = new TestPlatform(); + $platform->init(Service::TYPE_TASK, [ + 'adapter' => new Generic(), + 'container' => $container, + ]); + + $cli = $platform->getCli(); + $cli->run(); + + $result = ob_get_clean(); + } finally { + $_SERVER['argv'] = $argv; + } + + $this->assertEquals('test-value', $result); } }