-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6704 from paulbalandan/assert-logged-approx
feat: Check logs against parts of the message only
- Loading branch information
Showing
7 changed files
with
124 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'); |