Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Nov 29, 2023
2 parents dbb5797 + b1f66a3 commit 8f17ac8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/backwards-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: "Backwards compatibility"
on:
pull_request:

permissions:
contents: read

jobs:
bc-check:
name: "Backwards compatibility check"
Expand All @@ -11,10 +14,17 @@ jobs:

steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
uses: "actions/checkout@v4"
with:
fetch-depth: 0

# User in the container seems to be different than the cloned repo's owner.
# Git doesn't like that as the repo will then be unusable by the owner.
# We don't care about this here since this is only used for running one test.
# See https://github.com/actions/runner/issues/2033
- name: Workaround directory permissions
run: mkdir -p /home/runner/work/_temp/_github_home && printf "[safe]\n\tdirectory = /github/workspace" > /home/runner/work/_temp/_github_home/.gitconfig

- name: "BC Check"
uses: docker://nyholm/roave-bc-check-ga
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2']
php: ['8.0', '8.1', '8.2', '8.3']
psrlog: ['^1.0', '^2.0', '^3.0']
stable: [true]
coverage: [true]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ hasNotice(string|array $record): bool
hasInfo(string|array $record): bool
hasDebug(string|array $record): bool
hasRecordThatContains(string $message, string|int $level): bool
hasRecordThatContains(string $message, string|int|null $level = null): bool
hasEmergencyThatContains(string $message): bool
hasAlertThatContains(string $message): bool
Expand All @@ -57,7 +57,7 @@ hasNoticeThatContains(string $message): bool
hasInfoThatContains(string $message): bool
hasDebugThatContains(string $message): bool
hasRecordThatMatches(string $regex, string|int $level): bool
hasRecordThatMatches(string $regex, string|int|null $level = null): bool
hasEmergencyThatMatches(string $regex): bool
hasAlertThatMatches(string $regex): bool
Expand All @@ -68,7 +68,7 @@ hasNoticeThatMatches(string $regex): bool
hasInfoThatMatches(string $regex): bool
hasDebugThatMatches(string $regex): bool
hasRecordThatPasses(callable $predicate, string|int $level): bool
hasRecordThatPasses(callable $predicate, string|int|null $level = null): bool
hasEmergencyThatPasses(callable $predicate): bool
hasAlertThatPasses(callable $predicate): bool
Expand Down
18 changes: 11 additions & 7 deletions src/TestLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ public function log($level, $message, array $context = []): void
$this->records[] = $record;
}

public function hasRecords(string|int $level): bool
public function hasRecords(string|int|null $level = null): bool
{
if ($level === null) {
return \count($this->records) !== 0;
}

return isset($this->recordsByLevel[$level]);
}

/**
* @param string|array<string, mixed> $record
*/
public function hasRecord(string|array $record, string|int $level): bool
public function hasRecord(string|array $record, string|int|null $level = null): bool
{
if (\is_string($record)) {
$record = ['message' => $record];
Expand All @@ -109,14 +113,14 @@ public function hasRecord(string|array $record, string|int $level): bool
}, $level);
}

public function hasRecordThatContains(string $message, string|int $level): bool
public function hasRecordThatContains(string $message, string|int|null $level = null): bool
{
return $this->hasRecordThatPasses(static function (array $rec) use ($message) {
return \str_contains((string) $rec['message'], $message);
}, $level);
}

public function hasRecordThatMatches(string $regex, string|int $level): bool
public function hasRecordThatMatches(string $regex, string|int|null $level = null): bool
{
return $this->hasRecordThatPasses(static function ($rec) use ($regex) {
return \preg_match($regex, (string) $rec['message']) > 0;
Expand All @@ -126,13 +130,13 @@ public function hasRecordThatMatches(string $regex, string|int $level): bool
/**
* @param callable(array<string, mixed>, int): bool $predicate
*/
public function hasRecordThatPasses(callable $predicate, string|int $level): bool
public function hasRecordThatPasses(callable $predicate, string|int|null $level = null): bool
{
if (! isset($this->recordsByLevel[$level])) {
if (! $this->hasRecords($level)) {
return false;
}

foreach ($this->recordsByLevel[$level] as $i => $rec) {
foreach ($level === null ? $this->records : $this->recordsByLevel[$level] as $i => $rec) {
if (\call_user_func($predicate, $rec, $i)) {
return true;
}
Expand Down
31 changes: 16 additions & 15 deletions tests/unit/TestLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ final class TestLoggerTest extends TestCase
/**
* @dataProvider provideLogLevels
*/
public function testHasRecords(string $level): void
public function testHasRecords(string|null $level): void
{
$magicMethod = 'has' . \ucfirst($level) . 'Records';
$magicMethod = 'has' . \ucfirst($level ?? '') . 'Records';

$logger = new TestLogger();
$this->assertFalse($logger->hasRecords($level));
$this->assertFalse($logger->$magicMethod());

$logger->log($level, 'Test');
$logger->log($level ?? LogLevel::INFO, 'Test');
$this->assertTrue($logger->hasRecords($level));
$this->assertTrue($logger->$magicMethod());
}

/**
* @dataProvider provideLogLevels
*/
public function testHasRecord(string $level): void
public function testHasRecord(string|null $level): void
{
$magicMethod = 'has' . \ucfirst($level);
$magicMethod = 'has' . \ucfirst($level ?? 'Record');

$logger = new TestLogger();
$this->assertFalse($logger->hasRecord('Test', $level));
$this->assertFalse($logger->hasRecord(['message' => 'Test'], $level));
$this->assertFalse($logger->$magicMethod('Test'));
$this->assertFalse($logger->$magicMethod(['message' => 'Test']));

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

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

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

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

$this->assertTrue($logger->hasRecordThatContains('Test', $level));
$this->assertTrue($logger->$magicMethod('Test'));
Expand All @@ -73,15 +73,15 @@ public function testHasRecordThatContains(string $level): void
/**
* @dataProvider provideLogLevels
*/
public function testHasRecordThatMatches(string $level): void
public function testHasRecordThatMatches(string|null $level): void
{
$magicMethod = 'has' . \ucfirst($level) . 'ThatMatches';
$magicMethod = 'has' . \ucfirst($level ?? 'Record') . 'ThatMatches';

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

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

$this->assertTrue($logger->hasRecordThatMatches('/test/i', $level));
$this->assertTrue($logger->$magicMethod('/test/i'));
Expand All @@ -90,9 +90,9 @@ public function testHasRecordThatMatches(string $level): void
/**
* @dataProvider provideLogLevels
*/
public function testHasRecordThatPasses(string $level): void
public function testHasRecordThatPasses(string|null $level): void
{
$magicMethod = 'has' . \ucfirst($level) . 'ThatPasses';
$magicMethod = 'has' . \ucfirst($level ?? 'Record') . 'ThatPasses';

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

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

$this->assertTrue($logger->hasRecordThatPasses(static function ($record) {
return $record['message'] === 'Test';
Expand Down Expand Up @@ -211,5 +211,6 @@ public function provideLogLevels(): iterable
yield [LogLevel::CRITICAL];
yield [LogLevel::ALERT];
yield [LogLevel::EMERGENCY];
yield [null];
}
}

0 comments on commit 8f17ac8

Please sign in to comment.