283 Zeilen
8.3 KiB
PHP
283 Zeilen
8.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*
|
|
* https://www.d3data.de
|
|
*
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
* @link https://www.oxidmodule.com
|
|
*/
|
|
|
|
namespace D3\LoggerFactory\tests;
|
|
|
|
use D3\LoggerFactory\LoggerFactory;
|
|
use Generator;
|
|
use Monolog\Handler\BufferHandler;
|
|
use Monolog\Handler\DeduplicationHandler;
|
|
use Monolog\Handler\FingersCrossedHandler;
|
|
use Monolog\Handler\RotatingFileHandler;
|
|
use Monolog\Handler\StreamHandler;
|
|
use Monolog\Logger;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use ReflectionException;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* @coversNothing
|
|
*/
|
|
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);
|
|
|
|
/** @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);
|
|
|
|
$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
|
|
{
|
|
require_once __DIR__.'/Helpers/classAliases.php';
|
|
|
|
$sut = $this->getMockBuilder(LoggerFactory::class)
|
|
->onlyMethods(['applySpecialHandlers'])
|
|
->getMock();
|
|
$sut->expects($this->exactly(2))->method('applySpecialHandlers')->willReturnArgument(0);
|
|
|
|
$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]
|
|
)
|
|
);
|
|
}
|
|
}
|