loggerFactory/tests/LoggerFactoryTest.php

266 lines
7.9 KiB
PHP
Raw Normal View History

2025-02-05 21:57:47 +01:00
<?php
namespace D3\LoggerFactory\tests;
use D3\LoggerFactory\LoggerFactory;
use Generator;
use Monolog\Handler\BufferHandler;
use Monolog\Handler\DeduplicationHandler;
use Monolog\Handler\FingersCrossedHandler;
2025-02-05 21:57:47 +01:00
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
use RuntimeException;
class LoggerFactoryTest extends ApiTestCase
{
/**
* @test
* @return void
* @covers \D3\LoggerFactory\LoggerFactory::create
*/
public function testCreate(): void
{
$instance = LoggerFactory::create();
$this->assertInstanceOf(LoggerFactory::class, $instance);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::getFileLogger
* @covers \D3\LoggerFactory\LoggerFactory::getFileLoggerStreamHandler
* @dataProvider getFileLoggerDataProvider
*/
public function testGetFileLogger(int $logLevel, ?int $maxFiles, string $expectedHandlerClass): void
{
$sut = $this->getMockBuilder(LoggerFactory::class)
->onlyMethods(['applySpecialHandlers'])
->getMock();
$sut->expects($this->once())->method('applySpecialHandlers')->willReturnArgument(0);
2025-02-05 21:57:47 +01:00
/** @var Logger|MockObject $logger */
$logger = $this->callMethod(
$sut,
'getFileLogger',
['nameFixture', 'file/path.log', $logLevel, $maxFiles]
);
$this->assertInstanceOf(Logger::class, $logger);
$this->assertInstanceOf($expectedHandlerClass, $logger->getHandlers()[0]);
$this->assertSame($logLevel, $logger->getHandlers()[0]->getLevel());
}
public static function getFileLoggerDataProvider(): Generator
{
yield 'no rotation' => [Logger::INFO, null, StreamHandler::class];
yield 'rotation 1' => [Logger::ERROR, 1, RotatingFileHandler::class];
yield 'rotation 20' => [Logger::DEBUG, 20, RotatingFileHandler::class];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::getCombinedOxidAndFileLogger
*/
public function testGetCombinedOxidAndFileLoggerWithoutOxid(): void
{
$sut = $this->getMockBuilder(LoggerFactory::class)
->onlyMethods(['applySpecialHandlers'])
->getMock();
$sut->expects($this->never())->method('applySpecialHandlers')->willReturnArgument(0);
2025-02-05 21:57:47 +01:00
$this->expectException(RuntimeException::class);
$this->callMethod(
$sut,
'getCombinedOxidAndFileLogger',
['nameFixture', 'file/path.log', 1, 5]
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::getOxidLogPath
*/
public function testGetOxidLogPathWithoutOxid(): void
{
$sut = LoggerFactory::create();
$this->expectException(RuntimeException::class);
$this->assertSame(
'foo',
$this->callMethod(
$sut,
'getOxidLogPath',
['fixture.log']
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::getCombinedOxidAndFileLogger
*/
public function testGetCombinedOxidAndFileLoggerInOxid(): void
2025-02-05 21:57:47 +01:00
{
require_once __DIR__.'/Helpers/classAliases.php';
$sut = $this->getMockBuilder(LoggerFactory::class)
->onlyMethods(['applySpecialHandlers'])
->getMock();
$sut->expects($this->exactly(2))->method('applySpecialHandlers')->willReturnArgument(0);
2025-02-05 21:57:47 +01:00
$logger = $this->callMethod(
$sut,
'getCombinedOxidAndFileLogger',
['nameFixture', 'file/path.log', 1, 5]
);
$this->assertInstanceOf(Logger::class, $logger);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::getOxidLogPath
*/
public function testGetOxidLogPathInOxid(): void
{
require_once __DIR__.'/Helpers/classAliases.php';
$sut = LoggerFactory::create();
$this->assertStringEndsWith(
'tests/Helpers/log/fixture.log',
$this->callMethod(
$sut,
'getOxidLogPath',
['fixture.log']
)
);
}
/**
* @test
* @covers \D3\LoggerFactory\LoggerFactory::applySpecialHandlers
* @throws ReflectionException
* @dataProvider applySpecialHandlersDataProvider
*/
public function testApplySpecialHandlers(array $options, string $expectedClass): void
{
$sut = LoggerFactory::create();
$handler = $this->getMockBuilder(StreamHandler::class)
->disableOriginalConstructor()
->getMock();
$this->assertInstanceOf(
$expectedClass,
$this->callMethod(
$sut,
'applySpecialHandlers',
[$handler, $options]
)
);
}
public static function applySpecialHandlersDataProvider(): Generator
{
yield 'empty config' => [[], StreamHandler::class];
yield 'simple buffering' => [[LoggerFactory::SPECIAL_HANDLERS_BUFFERING], BufferHandler::class];
yield 'advanced buffering' => [
[LoggerFactory::SPECIAL_HANDLERS_BUFFERING => [LoggerFactory::BUFFERING_OPTION_LIMIT => 10]],
BufferHandler::class
];
yield 'simple logOnErrorOnly' => [[LoggerFactory::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY], FingersCrossedHandler::class];
yield 'advanced logOnErrorOnly' => [
[LoggerFactory::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY => [LoggerFactory::LOGONERRORONLY_LEVEL => Logger::DEBUG]],
FingersCrossedHandler::class
];
yield 'simple deduplicate' => [[LoggerFactory::SPECIAL_HANDLERS_MAKE_UNIQUE], DeduplicationHandler::class];
yield 'advanced deduplicate' => [
[LoggerFactory::SPECIAL_HANDLERS_MAKE_UNIQUE => [LoggerFactory::MAKEUNIQUE_OPTION_TIME => 30]],
DeduplicationHandler::class
];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::setBuffering
*/
public function testSetBuffering(): void
{
$sut = LoggerFactory::create();
$handler = $this->getMockBuilder(StreamHandler::class)
->disableOriginalConstructor()
->getMock();
$this->assertInstanceOf(
BufferHandler::class,
$this->callMethod(
$sut,
'setBuffering',
[$handler]
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::setLogItemsOnErrorOnly
*/
public function testSetLogItemsOnErrorOnly(): void
{
$sut = LoggerFactory::create();
$handler = $this->getMockBuilder(StreamHandler::class)
->disableOriginalConstructor()
->getMock();
$this->assertInstanceOf(
FingersCrossedHandler::class,
$this->callMethod(
$sut,
'setLogItemsOnErrorOnly',
[$handler]
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::makeUnique
*/
public function testMakeUnique(): void
{
$sut = LoggerFactory::create();
$handler = $this->getMockBuilder(StreamHandler::class)
->disableOriginalConstructor()
->getMock();
$this->assertInstanceOf(
DeduplicationHandler::class,
$this->callMethod(
$sut,
'makeUnique',
[$handler]
)
);
}
2025-02-05 21:57:47 +01:00
}