-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Check logs against parts of the message only #6704
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -349,8 +349,6 @@ protected function mockSession() | |
* @param string|null $expectedMessage | ||
* | ||
* @return bool | ||
* | ||
* @throws Exception | ||
*/ | ||
public function assertLogged(string $level, $expectedMessage = null) | ||
{ | ||
|
@@ -365,6 +363,21 @@ public function assertLogged(string $level, $expectedMessage = null) | |
return $result; | ||
} | ||
|
||
/** | ||
* Asserts that there is a log record that contains `$logMessage` in the message. | ||
*/ | ||
public function assertLogContains(string $level, string $logMessage, string $message = ''): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No preference from me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a little uncomfortable with both $needle and $needleMessage. So let's leave it as it is. |
||
{ | ||
$this->assertTrue( | ||
TestLogger::didLog($level, $logMessage, false), | ||
$message ?: sprintf( | ||
'Failed asserting that logs have a record of message containing "%s" with level "%s".', | ||
$logMessage, | ||
$level | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Hooks into CodeIgniter's Events system to check if a specific | ||
* event was triggered or not. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Test; | ||
|
||
use Config\Logger; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TestLoggerTest extends CIUnitTestCase | ||
{ | ||
/** | ||
* @dataProvider provideDidLogCases | ||
*/ | ||
public function testDidLogMethod(bool $expected, string $level, string $message, bool $exact): void | ||
{ | ||
(new TestLogger(new Logger()))->log('error', 'Some variable did not contain a value.'); | ||
|
||
$this->assertSame( | ||
$expected, | ||
TestLogger::didLog($level, $message, $exact), | ||
); | ||
} | ||
|
||
public function provideDidLogCases(): iterable | ||
{ | ||
yield 'exact' => [ | ||
true, | ||
'error', | ||
'Some variable did not contain a value.', | ||
true, | ||
]; | ||
|
||
yield 'wrong level' => [ | ||
false, | ||
'warning', | ||
'Some variable did not contain a value.', | ||
true, | ||
]; | ||
|
||
yield 'wrong message' => [ | ||
false, | ||
'error', | ||
'Some variables did not contain a value.', | ||
true, | ||
]; | ||
|
||
yield 'approximate' => [ | ||
true, | ||
'error', | ||
'Some variable did not', | ||
false, | ||
]; | ||
|
||
yield 'approximate but wrong level' => [ | ||
false, | ||
'warning', | ||
'Some variable did not', | ||
false, | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
<?php | ||
|
||
$config = new LoggerConfig(); | ||
$logger = new Logger($config); | ||
$config = new Config\Logger(); | ||
$logger = new CodeIgniter\Log\Logger($config); | ||
|
||
// ... do something that you expect a log entry from | ||
// check verbatim the log message | ||
$logger->log('error', "That's no moon"); | ||
|
||
$this->assertLogged('error', "That's no moon"); | ||
|
||
// check that a portion of the message is found in the logs | ||
$exception = new RuntimeException('Hello world.'); | ||
$logger->log('error', $exception->getTraceAsString()); | ||
$this->assertLogContains('error', '{main}'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not related to this PR, but why
$expectedMessage
can be null?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️