diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-08-17 00:25:08 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-14 17:35:11 +0200 |
commit | 81df2d2c12917ec9d442f2f5362f0e5d609a830b (patch) | |
tree | f22c31cbde974b0c1920926a80abd3c32c0a929d /tests | |
parent | a751ff7b93039b9e24e6a29fec50482dd07f7dbf (diff) | |
download | nextcloud-server-81df2d2c12917ec9d442f2f5362f0e5d609a830b.tar.gz nextcloud-server-81df2d2c12917ec9d442f2f5362f0e5d609a830b.zip |
feat(PsrLoggerAdapter): Allow to use `Psr\Log\LogLevel` for `log` method
There is the `Psr\Log\LogLevel` class defining loglevel constants,
to be fully compatible we should at least support those logging levels.
Moreover this is the last part that was still required from `ILogger` interface,
as we did not have alternatives for the loglevel constants.
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/Log/PsrLoggerAdapterTest.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/lib/Log/PsrLoggerAdapterTest.php b/tests/lib/Log/PsrLoggerAdapterTest.php new file mode 100644 index 00000000000..cc9ddea378a --- /dev/null +++ b/tests/lib/Log/PsrLoggerAdapterTest.php @@ -0,0 +1,88 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Log; + +use OC\Log; +use OC\Log\PsrLoggerAdapter; +use OCP\ILogger; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\InvalidArgumentException; +use Psr\Log\LogLevel; +use Test\TestCase; + +class PsrLoggerAdapterTest extends TestCase { + protected Log&MockObject $logger; + protected PsrLoggerAdapter $loggerAdapter; + + protected function setUp(): void { + parent::setUp(); + + $this->logger = $this->createMock(Log::class); + $this->loggerAdapter = new PsrLoggerAdapter($this->logger); + } + + /** + * @dataProvider dataPsrLoggingLevels + */ + public function testLoggingWithPsrLogLevels(string $level, int $expectedLevel): void { + $this->logger->expects(self::once()) + ->method('log') + ->with($expectedLevel, 'test message', ['app' => 'test']); + $this->loggerAdapter->log($level, 'test message', ['app' => 'test']); + } + + /** + * @dataProvider dataPsrLoggingLevels + */ + public function testLogLevelToInt(string $level, int $expectedLevel): void { + $this->assertEquals($expectedLevel, PsrLoggerAdapter::logLevelToInt($level)); + } + + public function dataPsrLoggingLevels(): array { + return [ + [LogLevel::ALERT, ILogger::ERROR], + [LogLevel::CRITICAL, ILogger::ERROR], + [LogLevel::DEBUG, ILogger::DEBUG], + [LogLevel::EMERGENCY, ILogger::FATAL], + [LogLevel::ERROR, ILogger::ERROR], + [LogLevel::INFO, ILogger::INFO], + [LogLevel::NOTICE, ILogger::INFO], + [LogLevel::WARNING, ILogger::WARN], + ]; + } + + /** + * @dataProvider dataInvalidLoggingLevel + */ + public function testInvalidLoggingLevel($level): void { + $this->logger->expects(self::never()) + ->method('log'); + $this->expectException(InvalidArgumentException::class); + + $this->loggerAdapter->log($level, 'valid message'); + } + + public function dataInvalidLoggingLevel(): array { + return [ + // invalid string + ['this is not a level'], + // int out of range + [ILogger::DEBUG - 1], + [ILogger::FATAL + 1], + // float is not allowed + [1.2345], + // boolean is not a level + [true], + [false], + // + [null], + ]; + } +} |