From 3c38e92f244b6c5dbdfb49aa36d698112e86be39 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Wed, 29 Nov 2023 17:44:59 -0500 Subject: [PATCH 1/3] Fix backwards-compatibility test --- .github/workflows/backwards-compatibility.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/backwards-compatibility.yml b/.github/workflows/backwards-compatibility.yml index 1c98c07..461b0c5 100644 --- a/.github/workflows/backwards-compatibility.yml +++ b/.github/workflows/backwards-compatibility.yml @@ -3,6 +3,9 @@ name: "Backwards compatibility" on: pull_request: +permissions: + contents: read + jobs: bc-check: name: "Backwards compatibility check" @@ -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: From 23f9b9f323e4f92ad23d49e22a4b8ce52a49848c Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Wed, 29 Nov 2023 17:46:16 -0500 Subject: [PATCH 2/3] Test on PHP 8.3 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index aad5fbe..7e1f6f6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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] From 5a3e2ede4c374c03c7a8aae8279ecd422363c3ce Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Tue, 28 Nov 2023 17:23:24 -0500 Subject: [PATCH 3/3] feat: remove requirement to pass a level --- README.md | 6 +++--- src/TestLogger.php | 18 +++++++++++------- tests/unit/TestLoggerTest.php | 31 ++++++++++++++++--------------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4ebfc7b..cae9159 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/src/TestLogger.php b/src/TestLogger.php index 2280cab..ae9ab93 100644 --- a/src/TestLogger.php +++ b/src/TestLogger.php @@ -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 $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]; @@ -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($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, $rec['message']) > 0; @@ -126,13 +130,13 @@ public function hasRecordThatMatches(string $regex, string|int $level): bool /** * @param callable(array, 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; } diff --git a/tests/unit/TestLoggerTest.php b/tests/unit/TestLoggerTest.php index e652fb5..2334257 100644 --- a/tests/unit/TestLoggerTest.php +++ b/tests/unit/TestLoggerTest.php @@ -14,15 +14,15 @@ 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()); } @@ -30,9 +30,9 @@ public function testHasRecords(string $level): void /** * @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)); @@ -40,7 +40,7 @@ public function testHasRecord(string $level): void $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)); @@ -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')); @@ -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')); @@ -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) { @@ -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'; @@ -211,5 +211,6 @@ public function provideLogLevels(): iterable yield [LogLevel::CRITICAL]; yield [LogLevel::ALERT]; yield [LogLevel::EMERGENCY]; + yield [null]; } }