Skip to content

Commit 8ca64a9

Browse files
authored
fix: ensure output buffer is closed after use of command() (#10099)
1 parent fbfc982 commit 8ca64a9

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

system/Common.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,14 @@ function command(string $command)
185185
$params[$arg] = $value;
186186
}
187187

188-
ob_start();
189-
service('commands')->run($command, $params);
188+
try {
189+
ob_start();
190+
service('commands')->run($command, $params);
190191

191-
return ob_get_clean();
192+
return ob_get_contents();
193+
} finally {
194+
ob_end_clean();
195+
}
192196
}
193197
}
194198

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Commands;
15+
16+
use RuntimeException;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class DestructiveCommand extends AbstractInfo
22+
{
23+
protected $group = 'demo';
24+
protected $name = 'app:destructive';
25+
protected $description = 'This command is destructive.';
26+
27+
public function run(array $params): never
28+
{
29+
throw new RuntimeException('This command is destructive and should not be run.');
30+
}
31+
}

tests/system/Commands/CommandTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\Test\StreamFilterTrait;
2020
use PHPUnit\Framework\Attributes\DataProvider;
2121
use PHPUnit\Framework\Attributes\Group;
22+
use RuntimeException;
2223
use Tests\Support\Commands\AppInfo;
2324
use Tests\Support\Commands\ParamsReveal;
2425

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

140+
public function testDestructiveCommandIsNotRisky(): void
141+
{
142+
$this->expectException(RuntimeException::class);
143+
144+
command('app:destructive');
145+
}
146+
139147
/**
140148
* @param list<string> $expected
141149
*/

tests/system/Commands/Translation/LocalizationSyncTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ public function testSyncWithNullableOriginalLangValue(): void
171171
TEXT_WRAP;
172172

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

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

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

197195
$this->expectException(LogicException::class);
198196
$this->expectExceptionMessageMatches('/Only "array" or "string" is allowed/');

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Bugs Fixed
3333
**********
3434

3535
- **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.
36+
- **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.
3637

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

0 commit comments

Comments
 (0)