2025-01-10 15:21:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Friendica\Test\Unit\Core\Logger;
|
|
|
|
|
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
2025-01-11 08:05:50 +00:00
|
|
|
use Friendica\Core\Logger\Capability\LogChannel;
|
2025-01-10 15:21:56 +00:00
|
|
|
use Friendica\Core\Logger\LoggerManager;
|
|
|
|
use Friendica\Core\Logger\Type\ProfilerLogger;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
|
|
|
class LoggerManagerTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testGetLoggerReturnsPsrLogger(): void
|
|
|
|
{
|
2025-01-11 08:05:50 +00:00
|
|
|
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue(null, null);
|
|
|
|
|
2025-01-10 15:21:56 +00:00
|
|
|
$factory = new LoggerManager($this->createStub(IManageConfigValues::class));
|
|
|
|
|
|
|
|
$this->assertInstanceOf(LoggerInterface::class, $factory->getLogger());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLoggerReturnsSameObject(): void
|
|
|
|
{
|
2025-01-11 08:05:50 +00:00
|
|
|
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue(null, null);
|
|
|
|
|
2025-01-10 15:21:56 +00:00
|
|
|
$factory = new LoggerManager($this->createStub(IManageConfigValues::class));
|
|
|
|
|
|
|
|
$this->assertSame($factory->getLogger(), $factory->getLogger());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLoggerWithDebugDisabledReturnsNullLogger(): void
|
|
|
|
{
|
|
|
|
$config = $this->createStub(IManageConfigValues::class);
|
|
|
|
$config->method('get')->willReturnMap([
|
|
|
|
['system', 'debugging', null, false],
|
|
|
|
]);
|
|
|
|
|
2025-01-11 08:05:50 +00:00
|
|
|
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue(null, null);
|
|
|
|
|
2025-01-10 15:21:56 +00:00
|
|
|
$factory = new LoggerManager($config);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(NullLogger::class, $factory->getLogger());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLoggerWithProfilerEnabledReturnsProfilerLogger(): void
|
|
|
|
{
|
|
|
|
$config = $this->createStub(IManageConfigValues::class);
|
|
|
|
$config->method('get')->willReturnMap([
|
|
|
|
['system', 'debugging', null, false],
|
|
|
|
['system', 'profiling', null, true],
|
|
|
|
]);
|
|
|
|
|
2025-01-11 08:05:50 +00:00
|
|
|
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue(null, null);
|
|
|
|
|
2025-01-10 15:21:56 +00:00
|
|
|
$factory = new LoggerManager($config);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger());
|
|
|
|
}
|
2025-01-11 08:05:50 +00:00
|
|
|
|
|
|
|
public function testChangeChannelReturnsDifferentLogger(): void
|
|
|
|
{
|
|
|
|
$config = $this->createStub(IManageConfigValues::class);
|
|
|
|
$config->method('get')->willReturnMap([
|
|
|
|
['system', 'debugging', null, false],
|
|
|
|
['system', 'profiling', null, true],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue(null, null);
|
|
|
|
|
|
|
|
$factory = new LoggerManager($config);
|
|
|
|
|
|
|
|
$logger1 = $factory->getLogger();
|
|
|
|
|
|
|
|
$factory->changeLogChannel(LogChannel::CONSOLE);
|
|
|
|
|
|
|
|
$this->assertNotSame($logger1, $factory->getLogger());
|
|
|
|
}
|
2025-01-10 15:21:56 +00:00
|
|
|
}
|