Skip to content

Commit c855f6d

Browse files
Add clear method to TestLogger (#9)
1 parent 20c45bc commit c855f6d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/TestLogger.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ public function log($level, Stringable|string $message, array $context = []): vo
151151
$this->records[] = new LogRecord($level, $message, $context);
152152
}
153153

154+
/**
155+
* Clears all stored records, resetting the storage to an empty state.
156+
*/
157+
public function clear(): void
158+
{
159+
$this->records = [];
160+
}
161+
154162
/**
155163
* @param Matcher $matcher
156164
*/

tests/unit/TestLoggerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Psr\Log\LogLevel;
1515
use Throwable;
1616

17+
use function assert;
18+
1719
/**
1820
* @phpstan-import-type Matcher from TestLogger
1921
*/
@@ -339,6 +341,29 @@ public function testNeverMatches(array $records, callable $matcher): void
339341
self::assertTrue($result);
340342
}
341343

344+
public function testDoesNotMatchAnythingAfterClearing(): void
345+
{
346+
$this->logger->log(LogLevel::INFO, 'Foo');
347+
348+
$matcher = TestLogger::level(LogLevel::INFO);
349+
assert($this->logger->once($matcher), 'Sanity check: should match before it is cleared');
350+
$this->logger->clear();
351+
352+
self::assertNotTrue($this->logger->once($matcher));
353+
}
354+
355+
public function testClearIsIdempotent(): void
356+
{
357+
$this->logger->log(LogLevel::INFO, 'Foo');
358+
359+
$matcher = TestLogger::level(LogLevel::INFO);
360+
assert($this->logger->once($matcher), 'Sanity check: should match before it is cleared');
361+
$this->logger->clear();
362+
$this->logger->clear();
363+
364+
self::assertNotTrue($this->logger->once($matcher));
365+
}
366+
342367
#[Override]
343368
protected function setUp(): void
344369
{

0 commit comments

Comments
 (0)