From bfb4c71554ab2072bbbda4a0b3cc8a20b0946ef6 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Wed, 19 Jul 2023 12:17:00 +0300 Subject: [PATCH] add unit test for `Yii::log()` --- tests/LoggerTest.php | 22 ++++++++++++++++++++++ tests/TestCase.php | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 0484160..c653ccb 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -4,6 +4,7 @@ use CLogger; use Psr\Log\LogLevel; +use Yii; use yii1tech\psr\log\Logger; use yii1tech\psr\log\test\support\ArrayLogger; @@ -77,6 +78,27 @@ public function testWritePsrLog(): void ); } + /** + * @depends testWritePsrLog + */ + public function testYiiLogFacade(): void + { + $psrLogger = new ArrayLogger(); + + $logger = (new Logger()) + ->setPsrLogger($psrLogger); + + Yii::setLogger($logger); + + Yii::log('test message', LogLevel::INFO, ['category' => 'context-category']); + + $logs = $psrLogger->flush(); + $this->assertFalse(empty($logs[0])); + $this->assertSame(LogLevel::INFO, $logs[0]['level']); + $this->assertSame('test message', $logs[0]['message']); + $this->assertSame(['category' => 'context-category'], $logs[0]['context']); + } + public function testWriteYiiLog(): void { $logger = (new Logger()) diff --git a/tests/TestCase.php b/tests/TestCase.php index 8d64c7d..41cb7bc 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,7 @@ namespace yii1tech\psr\log\test; use CConsoleApplication; +use CLogger; use CMap; use Yii; @@ -15,6 +16,8 @@ protected function setUp(): void { parent::setUp(); + Yii::setLogger(new CLogger()); + $this->mockApplication(); } @@ -24,6 +27,8 @@ protected function setUp(): void protected function tearDown(): void { $this->destroyApplication(); + + Yii::setLogger(null); } /**