Skip to content

Commit 0b1490d

Browse files
authored
refactor: make random-order CLI tests deterministic (#9998)
* refactor: make random-order CLI tests deterministic * fix CLI::reset() * apply code suggestion
1 parent 3535c7a commit 0b1490d

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

system/CLI/CLI.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,35 @@ protected static function fwrite($handle, string $string)
11291129
/**
11301130
* Testing purpose only
11311131
*
1132-
* @testTag
1132+
* @internal
1133+
*/
1134+
public static function reset(): void
1135+
{
1136+
static::$initialized = false;
1137+
static::$segments = [];
1138+
static::$options = [];
1139+
static::$lastWrite = 'write';
1140+
static::$height = null;
1141+
static::$width = null;
1142+
static::$isColored = static::hasColorSupport(STDOUT);
1143+
1144+
static::resetInputOutput();
1145+
}
1146+
1147+
/**
1148+
* Testing purpose only
1149+
*
1150+
* @internal
1151+
*/
1152+
public static function resetLastWrite(): void
1153+
{
1154+
static::$lastWrite = null;
1155+
}
1156+
1157+
/**
1158+
* Testing purpose only
1159+
*
1160+
* @internal
11331161
*/
11341162
public static function setInputOutput(InputOutput $io): void
11351163
{
@@ -1139,7 +1167,7 @@ public static function setInputOutput(InputOutput $io): void
11391167
/**
11401168
* Testing purpose only
11411169
*
1142-
* @testTag
1170+
* @internal
11431171
*/
11441172
public static function resetInputOutput(): void
11451173
{

tests/system/CLI/CLITest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ protected function setUp(): void
3636
parent::setUp();
3737

3838
Services::injectMock('superglobals', new Superglobals());
39+
40+
CLI::init();
41+
}
42+
43+
protected function tearDown(): void
44+
{
45+
CLI::reset();
46+
47+
parent::tearDown();
3948
}
4049

4150
public function testNew(): void
@@ -286,10 +295,6 @@ public function testStreamSupports(): void
286295

287296
public function testColor(): void
288297
{
289-
// After the tests on NO_COLOR and TERM_PROGRAM above,
290-
// the $isColored variable is rigged. So we reset this.
291-
CLI::init();
292-
293298
$this->assertSame(
294299
"\033[1;37m\033[42m\033[4mtest\033[0m",
295300
CLI::color('test', 'white', 'green', 'underline'),
@@ -330,6 +335,8 @@ public function testPrintBackground(): void
330335

331336
public function testWrite(): void
332337
{
338+
CLI::resetLastWrite();
339+
333340
CLI::write('test');
334341

335342
$expected = PHP_EOL . 'test' . PHP_EOL;

tests/system/CLI/ConsoleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function setUp(): void
3737
parent::setUp();
3838

3939
Services::injectMock('superglobals', new Superglobals());
40+
CLI::init();
4041

4142
$env = new DotEnv(ROOTPATH);
4243
$env->load();
@@ -50,6 +51,13 @@ protected function setUp(): void
5051
$this->app->initialize();
5152
}
5253

54+
protected function tearDown(): void
55+
{
56+
CLI::reset();
57+
58+
parent::tearDown();
59+
}
60+
5361
public function testHeader(): void
5462
{
5563
$console = new Console();

0 commit comments

Comments
 (0)