aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Log/PsrLoggerAdapterTest.php
blob: cc9ddea378a07fa803c28d15574c64f71d9b2af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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],
		];
	}
}