Skip to content
Merged
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
10 changes: 7 additions & 3 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ function command(string $command)
$params[$arg] = $value;
}

ob_start();
service('commands')->run($command, $params);
try {
ob_start();
service('commands')->run($command, $params);

return ob_get_clean();
return ob_get_contents();
} finally {
ob_end_clean();
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/_support/Commands/DestructiveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Commands;

use RuntimeException;

/**
* @internal
*/
final class DestructiveCommand extends AbstractInfo
{
protected $group = 'demo';
protected $name = 'app:destructive';
protected $description = 'This command is destructive.';

public function run(array $params): never
{
throw new RuntimeException('This command is destructive and should not be run.');
}
}
8 changes: 8 additions & 0 deletions tests/system/Commands/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use RuntimeException;
use Tests\Support\Commands\AppInfo;
use Tests\Support\Commands\ParamsReveal;

Expand Down Expand Up @@ -136,6 +137,13 @@ public function testInexistentCommandsButWithManyAlternatives(): void
$this->assertStringContainsString(':clear', $this->getBuffer());
}

public function testDestructiveCommandIsNotRisky(): void
{
$this->expectException(RuntimeException::class);

command('app:destructive');
}

/**
* @param list<string> $expected
*/
Expand Down
2 changes: 0 additions & 2 deletions tests/system/Commands/Translation/LocalizationSyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public function testSyncWithNullableOriginalLangValue(): void
TEXT_WRAP;

file_put_contents(self::$languageTestPath . self::$locale . '/SyncInvalid.php', $langWithNullValue);
ob_get_flush();

$this->expectException(LogicException::class);
$this->expectExceptionMessageMatches('/Only "array" or "string" is allowed/');
Expand All @@ -192,7 +191,6 @@ public function testSyncWithIntegerOriginalLangValue(): void
TEXT_WRAP;

file_put_contents(self::$languageTestPath . self::$locale . '/SyncInvalid.php', $langWithIntegerValue);
ob_get_flush();

$this->expectException(LogicException::class);
$this->expectExceptionMessageMatches('/Only "array" or "string" is allowed/');
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Bugs Fixed
**********

- **Autoloader:** Fixed a bug where ``Autoloader::unregister()`` (used during tests) silently failed to remove handlers from the SPL autoload stack, causing closures to accumulate permanently.
- **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
Loading