Skip to content

Commit c5b67c9

Browse files
committed
Update EOL handling in CliApplication tests
1 parent 92cb8f7 commit c5b67c9

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

tests/unit/Cli/CliApplicationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testCommandSynopsis(): void
5757
$_SERVER['argv'] = ['app.php'];
5858
$app = $this->App->oneCommand(TestOptions::class);
5959
$this->assertSame(1, $app->run()->getLastExitStatus());
60-
$this->assertSame([
60+
$this->assertSameConsoleMessages([
6161
[Level::ERROR, 'Error: --start required'],
6262
[Level::INFO, <<<'EOF'
6363
@@ -73,7 +73,7 @@ public function testCommandHelp(): void
7373
$_SERVER['argv'] = ['app.php', '--help'];
7474
$app = $this->App->oneCommand(TestOptions::class);
7575
$this->assertSame(0, $app->run()->getLastExitStatus());
76-
$this->assertSame([
76+
$this->assertSameConsoleMessages([
7777
[Level::INFO, <<<'EOF'
7878
NAME
7979
app - Test CliCommand options

tests/unit/TestCase.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Lkrms\Tests;
44

5+
use Lkrms\Console\Catalog\ConsoleLevel as Level;
56
use Lkrms\Utility\Pcre;
7+
use Lkrms\Utility\Str;
68
use Closure;
79
use Throwable;
810

@@ -14,22 +16,44 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
1416
* @param Closure(): mixed $callback
1517
* @param class-string<Throwable> $exception
1618
*/
17-
public function assertThrows(Closure $callback, string $exception, ?string $exceptionMessage = null, string $message = ''): void
18-
{
19+
public static function assertThrows(
20+
Closure $callback,
21+
string $exception,
22+
?string $exceptionMessage = null,
23+
string $message = ''
24+
): void {
1925
try {
2026
$callback();
2127
} catch (Throwable $ex) {
22-
$this->assertInstanceOf($exception, $ex, $message);
28+
static::assertInstanceOf($exception, $ex, $message);
2329
if ($exceptionMessage !== null) {
24-
$this->assertStringContainsString($exceptionMessage, $ex->getMessage(), $message);
30+
static::assertStringContainsString($exceptionMessage, $ex->getMessage(), $message);
2531
}
2632
return;
2733
}
28-
$this->fail($message === ''
34+
static::fail($message === ''
2935
? sprintf('Failed asserting that exception of type %s is thrown', $exception)
3036
: $message);
3137
}
3238

39+
/**
40+
* Assert that the given console messages are written, converting line
41+
* endings if necessary
42+
*
43+
* @param array<array{Level::*,string,2?:array<string,mixed>}> $expected
44+
* @param mixed[] $actual
45+
*/
46+
public static function assertSameConsoleMessages(array $expected, array $actual, string $message = ''): void
47+
{
48+
foreach ($expected as $i => &$expectedMessage) {
49+
$expectedMessage[1] = Str::eolFromNative($expectedMessage[1]);
50+
if (!isset($expectedMessage[2]) && isset($actual[$i][2])) {
51+
unset($actual[$i][2]);
52+
}
53+
}
54+
static::assertEquals($expected, $actual, $message);
55+
}
56+
3357
/**
3458
* Expect an exception if a given value is a string
3559
*

0 commit comments

Comments
 (0)