-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is meant to make it easier to do unit tests and also assert what's in the log. I know it's bad practices to test against implementation details but sometimes there are not other good options
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stefna\Logger\Logger; | ||
|
||
use PHPUnit\Framework\Assert; | ||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LogLevel; | ||
|
||
/** | ||
* @phpstan-type LogRecord array{ | ||
* level: string, | ||
* message: string|\Stringable, | ||
* context: array<string, mixed> | ||
* } | ||
*/ | ||
final class TestLogger extends AbstractLogger | ||
{ | ||
/** @var array<array-key, LogRecord> */ | ||
protected array $buffer = []; | ||
|
||
/** | ||
* @inheritdoc | ||
* @phpstan-param LogLevel::* $level | ||
* @param array<string, mixed> $context | ||
*/ | ||
public function log($level, string|\Stringable $message, array $context = []): void | ||
{ | ||
$this->buffer[] = [ | ||
'level' => $level, | ||
'message' => $message, | ||
'context' => $context, | ||
]; | ||
} | ||
|
||
/** | ||
* @param int $index | ||
* @return false|LogRecord | ||
*/ | ||
public function getLogAt(int $index): false|array | ||
{ | ||
if (!isset($this->buffer[$index])) { | ||
return false; | ||
} | ||
|
||
return $this->buffer[$index]; | ||
} | ||
|
||
public function assertBufferContainsMessage(string $message, int $expectedCount = 1): void | ||
{ | ||
$found = 0; | ||
foreach ($this->buffer as $buffer) { | ||
if ($buffer['message'] === $message) { | ||
$found++; | ||
} | ||
} | ||
|
||
Assert::assertSame($expectedCount, $found); | ||
} | ||
|
||
/** | ||
* @param \Closure(LogRecord): bool $check | ||
*/ | ||
public function assertBufferContains(\Closure $check): void | ||
{ | ||
$found = 0; | ||
foreach ($this->buffer as $buffer) { | ||
if ($check($buffer)) { | ||
$found++; | ||
} | ||
} | ||
|
||
Assert::assertTrue($found !== 0); | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->buffer = []; | ||
} | ||
|
||
/** | ||
* @return array<array-key, LogRecord> | ||
*/ | ||
public function getBuffer(): array | ||
{ | ||
return $this->buffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stefna\Logger\Logger; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class TestLoggerTest extends TestCase | ||
{ | ||
public function testAssert(): void | ||
{ | ||
$logger = new TestLogger(); | ||
$logger->debug('Test message'); | ||
$logger->info('Info message'); | ||
$logger->info('Info message'); | ||
$logger->error('Error message'); | ||
|
||
$logger->assertBufferContainsMessage('Info message', 2); | ||
} | ||
|
||
public function testCustomCheck(): void | ||
{ | ||
$logger = new TestLogger(); | ||
$logger->debug('Test message', [ | ||
'test' => 1, | ||
]); | ||
$logger->notice('Test message', [ | ||
'test' => 1, | ||
]); | ||
$logger->info('Test message', [ | ||
'test' => 1, | ||
]); | ||
$logger->error('Test message', [ | ||
'test' => 1, | ||
]); | ||
|
||
$logger->assertBufferContains(fn (array $record) => $record['context']['test'] === 1); | ||
} | ||
|
||
public function testReset(): void | ||
{ | ||
$logger = new TestLogger(); | ||
$logger->debug('Test message'); | ||
|
||
$this->assertCount(1, $logger->getBuffer()); | ||
|
||
$logger->reset(); | ||
|
||
$this->assertCount(0, $logger->getBuffer()); | ||
} | ||
} |