Skip to content

Commit 5263b43

Browse files
committed
[BUGFIX] Check error level is higher
When the error level is set to warning on the spy errors should also be found as fail.
1 parent 2198f48 commit 5263b43

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

packages/guides-cli/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"require": {
2828
"php": "^8.1",
29-
"monolog/monolog": "^2.9 || ^3.0",
29+
"monolog/monolog": "^3.0",
3030
"phpdocumentor/guides": "^1.0 || ^2.0",
3131
"phpdocumentor/guides-restructured-text": "^1.0 || ^2.0",
3232
"symfony/config": "^5.4 || ^6.3 || ^7.0",

packages/guides-cli/src/Logger/SpyProcessor.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace phpDocumentor\Guides\Cli\Logger;
1515

16+
use Monolog\Level;
1617
use Monolog\LogRecord;
1718
use Monolog\Processor\ProcessorInterface;
1819
use Psr\Log\LogLevel;
@@ -27,9 +28,15 @@
2728
final class SpyProcessor implements ProcessorInterface
2829
{
2930
private bool $hasBeenCalled = false;
31+
private Level $level;
3032

31-
public function __construct(private string|null $level = LogLevel::WARNING)
33+
public function __construct(string|null $level = LogLevel::WARNING)
3234
{
35+
if ($level === null) {
36+
$level = LogLevel::WARNING;
37+
}
38+
39+
$this->level = Level::fromName(strtolower($level));
3340
}
3441

3542
public function hasBeenCalled(): bool
@@ -39,7 +46,8 @@ public function hasBeenCalled(): bool
3946

4047
public function __invoke(array|LogRecord $record): array|LogRecord
4148
{
42-
if (strtolower($record['level_name']) === $this->level) {
49+
$recordLevel = Level::fromName($record['level_name']);
50+
if ($this->level->includes($recordLevel)) {
4351
$this->hasBeenCalled = true;
4452
}
4553

packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace phpDocumentor\Guides\Cli\Logger;
1515

1616
use PHPUnit\Framework\TestCase;
17+
use Psr\Log\LogLevel;
1718

1819
final class SpyProcessorTest extends TestCase
1920
{
@@ -30,4 +31,18 @@ public function testItKnowsWhenALogIsEmitted(): void
3031
$process(['channel' => 'test', 'level_name' => 'warning']);
3132
self::assertTrue($process->hasBeenCalled());
3233
}
34+
35+
public function testItKnowsWhenAErrorIsEmitted(): void
36+
{
37+
$process = new SpyProcessor();
38+
$process(['channel' => 'test', 'level_name' => 'error']);
39+
self::assertTrue($process->hasBeenCalled());
40+
}
41+
42+
public function testIsNotCalledWhenLevelIsTolow(): void
43+
{
44+
$process = new SpyProcessor(LogLevel::ERROR);
45+
$process(['channel' => 'test', 'level_name' => 'warning']);
46+
self::assertFalse($process->hasBeenCalled());
47+
}
3348
}

0 commit comments

Comments
 (0)