loggerFactory/tests/ProcessorsTestTrait.php

136 lines
4.0 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\Logger;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
/**
* @coversNothing
*/
trait ProcessorsTestTrait
{
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::applyProcessors
*/
public function testApplyProcessors(): void
{
$logger = $this->getMockBuilder(Logger::class)
->disableOriginalConstructor()
->getMock();
$sut = $this->getMockBuilder(LoggerFactory::class)
->onlyMethods(['applyUidProcessor', 'applyFilterSensitiveProcessor'])
->getMock();
$sut->expects($this->once())->method('applyUidProcessor');
$sut->expects($this->once())->method('applyFilterSensitiveProcessor');
$this->assertInstanceOf(
Logger::class,
$this->callMethod(
$sut,
'applyProcessors',
[$logger, []]
)
);
}
/**
* @test
*
* @param array $configuration
* @param int $expectedCount
*
* @return void
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::applyUidProcessor
* @dataProvider applyUidProcessorDataProvider
*/
public function testApplyUidProcessor(array $configuration, int $expectedCount): void
{
/** @var Logger|MockObject $logger */
$logger = LoggerFactory::create()->getFileLogger('foo', 'logg.file');
$sut = LoggerFactory::create();
$this->callMethod(
$sut,
'applyUidProcessor',
[$configuration, $logger]
);
$this->assertCount(
$expectedCount,
$logger->getProcessors()
);
}
public static function applyUidProcessorDataProvider(): Generator
{
yield 'simple configuration' => [[LoggerFactory::PROCESSOR_UNIQUE_ID], 1];
yield 'advanced configuration' => [[LoggerFactory::PROCESSOR_UNIQUE_ID => ''], 1];
yield 'no configuration' => [[], 0];
}
/**
* @test
*
* @param array $configuration
* @param int $expectedCount
*
* @return void
* @throws ReflectionException
* @covers \D3\LoggerFactory\LoggerFactory::applyFilterSensitiveProcessor
* @dataProvider applyFilterProcessorDataProvider
*/
public function testApplyFilterProcessor(array $configuration, int $expectedCount, bool $expectException): void
{
/** @var Logger|MockObject $logger */
$logger = LoggerFactory::create()->getFileLogger('foo', 'logg.file');
$sut = LoggerFactory::create();
if ($expectException) {
$this->expectException(\RuntimeException::class);
}
$this->callMethod(
$sut,
'applyFilterSensitiveProcessor',
[$configuration, $logger]
);
$this->assertCount(
$expectedCount,
$logger->getProcessors()
);
}
public static function applyFilterProcessorDataProvider(): Generator
{
yield 'simple configuration' => [[LoggerFactory::PROCESSOR_FILTERSENSITIVE], 0, false];
yield 'advanced configuration' => [[LoggerFactory::PROCESSOR_FILTERSENSITIVE => [LoggerFactory::FILTERSENSITIVE_SECRETS => ['foo']]], 1, false];
yield 'misconfiguration' => [[LoggerFactory::PROCESSOR_FILTERSENSITIVE => [LoggerFactory::FILTERSENSITIVE_SECRETS => 'foo']], 0, true];
yield 'no configuration' => [[], 0, false];
}
}