Skip to content

Commit 5a3e2ed

Browse files
kbondcolinodell
authored andcommitted
feat: remove requirement to pass a level
1 parent 9dc8ead commit 5a3e2ed

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ hasNotice(string|array $record): bool
4646
hasInfo(string|array $record): bool
4747
hasDebug(string|array $record): bool
4848
49-
hasRecordThatContains(string $message, string|int $level): bool
49+
hasRecordThatContains(string $message, string|int|null $level = null): bool
5050
5151
hasEmergencyThatContains(string $message): bool
5252
hasAlertThatContains(string $message): bool
@@ -57,7 +57,7 @@ hasNoticeThatContains(string $message): bool
5757
hasInfoThatContains(string $message): bool
5858
hasDebugThatContains(string $message): bool
5959
60-
hasRecordThatMatches(string $regex, string|int $level): bool
60+
hasRecordThatMatches(string $regex, string|int|null $level = null): bool
6161
6262
hasEmergencyThatMatches(string $regex): bool
6363
hasAlertThatMatches(string $regex): bool
@@ -68,7 +68,7 @@ hasNoticeThatMatches(string $regex): bool
6868
hasInfoThatMatches(string $regex): bool
6969
hasDebugThatMatches(string $regex): bool
7070
71-
hasRecordThatPasses(callable $predicate, string|int $level): bool
71+
hasRecordThatPasses(callable $predicate, string|int|null $level = null): bool
7272
7373
hasEmergencyThatPasses(callable $predicate): bool
7474
hasAlertThatPasses(callable $predicate): bool

src/TestLogger.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,19 @@ public function log($level, $message, array $context = []): void
8686
$this->records[] = $record;
8787
}
8888

89-
public function hasRecords(string|int $level): bool
89+
public function hasRecords(string|int|null $level = null): bool
9090
{
91+
if ($level === null) {
92+
return \count($this->records) !== 0;
93+
}
94+
9195
return isset($this->recordsByLevel[$level]);
9296
}
9397

9498
/**
9599
* @param string|array<string, mixed> $record
96100
*/
97-
public function hasRecord(string|array $record, string|int $level): bool
101+
public function hasRecord(string|array $record, string|int|null $level = null): bool
98102
{
99103
if (\is_string($record)) {
100104
$record = ['message' => $record];
@@ -109,14 +113,14 @@ public function hasRecord(string|array $record, string|int $level): bool
109113
}, $level);
110114
}
111115

112-
public function hasRecordThatContains(string $message, string|int $level): bool
116+
public function hasRecordThatContains(string $message, string|int|null $level = null): bool
113117
{
114118
return $this->hasRecordThatPasses(static function (array $rec) use ($message) {
115119
return \str_contains($rec['message'], $message);
116120
}, $level);
117121
}
118122

119-
public function hasRecordThatMatches(string $regex, string|int $level): bool
123+
public function hasRecordThatMatches(string $regex, string|int|null $level = null): bool
120124
{
121125
return $this->hasRecordThatPasses(static function ($rec) use ($regex) {
122126
return \preg_match($regex, $rec['message']) > 0;
@@ -126,13 +130,13 @@ public function hasRecordThatMatches(string $regex, string|int $level): bool
126130
/**
127131
* @param callable(array<string, mixed>, int): bool $predicate
128132
*/
129-
public function hasRecordThatPasses(callable $predicate, string|int $level): bool
133+
public function hasRecordThatPasses(callable $predicate, string|int|null $level = null): bool
130134
{
131-
if (! isset($this->recordsByLevel[$level])) {
135+
if (! $this->hasRecords($level)) {
132136
return false;
133137
}
134138

135-
foreach ($this->recordsByLevel[$level] as $i => $rec) {
139+
foreach ($level === null ? $this->records : $this->recordsByLevel[$level] as $i => $rec) {
136140
if (\call_user_func($predicate, $rec, $i)) {
137141
return true;
138142
}

tests/unit/TestLoggerTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@ final class TestLoggerTest extends TestCase
1414
/**
1515
* @dataProvider provideLogLevels
1616
*/
17-
public function testHasRecords(string $level): void
17+
public function testHasRecords(string|null $level): void
1818
{
19-
$magicMethod = 'has' . \ucfirst($level) . 'Records';
19+
$magicMethod = 'has' . \ucfirst($level ?? '') . 'Records';
2020

2121
$logger = new TestLogger();
2222
$this->assertFalse($logger->hasRecords($level));
2323
$this->assertFalse($logger->$magicMethod());
2424

25-
$logger->log($level, 'Test');
25+
$logger->log($level ?? LogLevel::INFO, 'Test');
2626
$this->assertTrue($logger->hasRecords($level));
2727
$this->assertTrue($logger->$magicMethod());
2828
}
2929

3030
/**
3131
* @dataProvider provideLogLevels
3232
*/
33-
public function testHasRecord(string $level): void
33+
public function testHasRecord(string|null $level): void
3434
{
35-
$magicMethod = 'has' . \ucfirst($level);
35+
$magicMethod = 'has' . \ucfirst($level ?? 'Record');
3636

3737
$logger = new TestLogger();
3838
$this->assertFalse($logger->hasRecord('Test', $level));
3939
$this->assertFalse($logger->hasRecord(['message' => 'Test'], $level));
4040
$this->assertFalse($logger->$magicMethod('Test'));
4141
$this->assertFalse($logger->$magicMethod(['message' => 'Test']));
4242

43-
$logger->log($level, 'Test');
43+
$logger->log($level ?? LogLevel::INFO, 'Test');
4444

4545
$this->assertTrue($logger->hasRecord('Test', $level));
4646
$this->assertTrue($logger->hasRecord(['message' => 'Test'], $level));
@@ -56,15 +56,15 @@ public function testHasRecord(string $level): void
5656
/**
5757
* @dataProvider provideLogLevels
5858
*/
59-
public function testHasRecordThatContains(string $level): void
59+
public function testHasRecordThatContains(string|null $level): void
6060
{
61-
$magicMethod = 'has' . \ucfirst($level) . 'ThatContains';
61+
$magicMethod = 'has' . \ucfirst($level ?? 'Record') . 'ThatContains';
6262

6363
$logger = new TestLogger();
6464
$this->assertFalse($logger->hasRecordThatContains('Test', $level));
6565
$this->assertFalse($logger->$magicMethod('Test'));
6666

67-
$logger->log($level, 'This Is A Test');
67+
$logger->log($level ?? LogLevel::INFO, 'This Is A Test');
6868

6969
$this->assertTrue($logger->hasRecordThatContains('Test', $level));
7070
$this->assertTrue($logger->$magicMethod('Test'));
@@ -73,15 +73,15 @@ public function testHasRecordThatContains(string $level): void
7373
/**
7474
* @dataProvider provideLogLevels
7575
*/
76-
public function testHasRecordThatMatches(string $level): void
76+
public function testHasRecordThatMatches(string|null $level): void
7777
{
78-
$magicMethod = 'has' . \ucfirst($level) . 'ThatMatches';
78+
$magicMethod = 'has' . \ucfirst($level ?? 'Record') . 'ThatMatches';
7979

8080
$logger = new TestLogger();
8181
$this->assertFalse($logger->hasRecordThatMatches('/test/i', $level));
8282
$this->assertFalse($logger->$magicMethod('/test/i'));
8383

84-
$logger->log($level, 'This Is A Test');
84+
$logger->log($level ?? LogLevel::INFO, 'This Is A Test');
8585

8686
$this->assertTrue($logger->hasRecordThatMatches('/test/i', $level));
8787
$this->assertTrue($logger->$magicMethod('/test/i'));
@@ -90,9 +90,9 @@ public function testHasRecordThatMatches(string $level): void
9090
/**
9191
* @dataProvider provideLogLevels
9292
*/
93-
public function testHasRecordThatPasses(string $level): void
93+
public function testHasRecordThatPasses(string|null $level): void
9494
{
95-
$magicMethod = 'has' . \ucfirst($level) . 'ThatPasses';
95+
$magicMethod = 'has' . \ucfirst($level ?? 'Record') . 'ThatPasses';
9696

9797
$logger = new TestLogger();
9898
$this->assertFalse($logger->hasRecordThatPasses(static function ($record) {
@@ -102,7 +102,7 @@ public function testHasRecordThatPasses(string $level): void
102102
return $record['message'] === 'Test';
103103
}));
104104

105-
$logger->log($level, 'Test');
105+
$logger->log($level ?? LogLevel::INFO, 'Test');
106106

107107
$this->assertTrue($logger->hasRecordThatPasses(static function ($record) {
108108
return $record['message'] === 'Test';
@@ -211,5 +211,6 @@ public function provideLogLevels(): iterable
211211
yield [LogLevel::CRITICAL];
212212
yield [LogLevel::ALERT];
213213
yield [LogLevel::EMERGENCY];
214+
yield [null];
214215
}
215216
}

0 commit comments

Comments
 (0)