Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Platform/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions tests/Platform/TestActionCLIInjection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Utopia\Tests;

use Utopia\Platform\Action;

class TestActionCLIInjection extends Action
{
public function __construct()
{
$this
->inject('test')
->callback(function ($test) {
$this->action($test);
});
}

public function action($test)
{
echo $test;
}
}
1 change: 1 addition & 0 deletions tests/Platform/TestServiceCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
31 changes: 30 additions & 1 deletion tests/e2e/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Comment on lines +44 to +63
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Output buffer not closed on exception path

ob_start() is called inside the try block, but ob_get_clean() only runs on the happy path. If $cli->run() throws, the buffer is never flushed or discarded, which can corrupt output for every subsequent test in the suite. Additionally, $result is referenced after the try/finally block but is only assigned inside the try — if an exception escapes, $result would be undefined at the assertEquals call.

Moving the buffer teardown into finally fixes both issues:

$argv = $_SERVER['argv'] ?? [];
$result = '';

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();
} finally {
    $result = ob_get_clean();
    $_SERVER['argv'] = $argv;
}

$this->assertEquals('test-value', $result);


$this->assertEquals('test-value', $result);
}
}
Loading