PsrLoggerAdapterTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Log;
  8. use OC\Log;
  9. use OC\Log\PsrLoggerAdapter;
  10. use OCP\ILogger;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use Psr\Log\InvalidArgumentException;
  13. use Psr\Log\LogLevel;
  14. use Test\TestCase;
  15. class PsrLoggerAdapterTest extends TestCase {
  16. protected Log&MockObject $logger;
  17. protected PsrLoggerAdapter $loggerAdapter;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->logger = $this->createMock(Log::class);
  21. $this->loggerAdapter = new PsrLoggerAdapter($this->logger);
  22. }
  23. /**
  24. * @dataProvider dataPsrLoggingLevels
  25. */
  26. public function testLoggingWithPsrLogLevels(string $level, int $expectedLevel): void {
  27. $this->logger->expects(self::once())
  28. ->method('log')
  29. ->with($expectedLevel, 'test message', ['app' => 'test']);
  30. $this->loggerAdapter->log($level, 'test message', ['app' => 'test']);
  31. }
  32. /**
  33. * @dataProvider dataPsrLoggingLevels
  34. */
  35. public function testLogLevelToInt(string $level, int $expectedLevel): void {
  36. $this->assertEquals($expectedLevel, PsrLoggerAdapter::logLevelToInt($level));
  37. }
  38. public function dataPsrLoggingLevels(): array {
  39. return [
  40. [LogLevel::ALERT, ILogger::ERROR],
  41. [LogLevel::CRITICAL, ILogger::ERROR],
  42. [LogLevel::DEBUG, ILogger::DEBUG],
  43. [LogLevel::EMERGENCY, ILogger::FATAL],
  44. [LogLevel::ERROR, ILogger::ERROR],
  45. [LogLevel::INFO, ILogger::INFO],
  46. [LogLevel::NOTICE, ILogger::INFO],
  47. [LogLevel::WARNING, ILogger::WARN],
  48. ];
  49. }
  50. /**
  51. * @dataProvider dataInvalidLoggingLevel
  52. */
  53. public function testInvalidLoggingLevel($level): void {
  54. $this->logger->expects(self::never())
  55. ->method('log');
  56. $this->expectException(InvalidArgumentException::class);
  57. $this->loggerAdapter->log($level, 'valid message');
  58. }
  59. public function dataInvalidLoggingLevel(): array {
  60. return [
  61. // invalid string
  62. ['this is not a level'],
  63. // int out of range
  64. [ILogger::DEBUG - 1],
  65. [ILogger::FATAL + 1],
  66. // float is not allowed
  67. [1.2345],
  68. // boolean is not a level
  69. [true],
  70. [false],
  71. //
  72. [null],
  73. ];
  74. }
  75. }