Cette révision appartient à :
Daniel Seifert 2024-12-27 00:02:40 +01:00
Parent ff342fe11d
révision 468bcb1c3d
Signé par: DanielS
ID de la clé GPG: 6A513E13AEE66170
3 fichiers modifiés avec 266 ajouts et 0 suppressions

Voir le fichier

@ -29,6 +29,8 @@ use ReflectionException;
class GuzzleFactoryTest extends ApiTestCase
{
use HeaderTestTrait;
use LoggerTestTrait;
use MessageFormatterTestTrait;
/**
* @test

118
tests/LoggerTestTrait.php Fichier normal
Voir le fichier

@ -0,0 +1,118 @@
<?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\GuzzleFactory\tests;
use D3\GuzzleFactory\GuzzleFactory;
use Generator;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use ReflectionException;
trait LoggerTestTrait
{
/**
* @test
* @throws ReflectionException
* @covers \D3\GuzzleFactory\GuzzleFactory::addFileLogger
* @dataProvider addFileLoggerDataProvider
*/
public function testAddFileLogger(int $logLevel, ?int $maxFiles, string $expectedHandlerClass): void
{
$sut = GuzzleFactory::create();
$this->callMethod(
$sut,
'addFileLogger',
['nameFixture', 'file/path.log', $logLevel, $maxFiles]
);
$loggers = $this->getValue($sut, 'loggers');
$this->assertArrayHasKey('nameFixture', $loggers);
$this->assertInstanceOf(Logger::class, $loggers['nameFixture']);
$this->assertInstanceOf($expectedHandlerClass, $loggers['nameFixture']->getHandlers()[0]);
$this->assertSame($logLevel, $loggers['nameFixture']->popHandler('nameFixture')->getLevel());
}
public static function addFileLoggerDataProvider(): 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\GuzzleFactory\GuzzleFactory::addConfiguredLogger
* @covers \D3\GuzzleFactory\GuzzleFactory::getLoggers
*/
public function testAddConfiguredLogger(): void
{
$loggerFixture = new Logger('fixturename');
$loggerFixture->pushHandler(new StreamHandler('log.log', Logger::DEBUG));
$sut = GuzzleFactory::create();
$this->callMethod(
$sut,
'addConfiguredLogger',
[$loggerFixture]
);
$loggers = $this->callMethod(
$sut,
'getLoggers',
);
$logger = $loggers[array_key_first($loggers)];
$this->assertSame($loggerFixture, $logger);
}
/**
* @test
* @param int $level
* @return void
* @throws ReflectionException
* @covers \D3\GuzzleFactory\GuzzleFactory::setMessageLevel
* @covers \D3\GuzzleFactory\GuzzleFactory::getMessageLevel
* @dataProvider setGetMessageLevelDataProvider
*/
public function testSetGetMessageLevel(int $level): void
{
$sut = GuzzleFactory::create();
$this->callMethod(
$sut,
'setMessageLevel',
[$level]
);
$this->assertSame(
$level,
$this->callMethod(
$sut,
'getMessageLevel',
)
);
}
public static function setGetMessageLevelDataProvider(): Generator
{
yield 'info' => [Logger::INFO];
yield 'error' => [Logger::ERROR];
}
}

Voir le fichier

@ -0,0 +1,146 @@
<?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\GuzzleFactory\tests;
use D3\GuzzleFactory\GuzzleFactory;
use D3\SensitiveMessageFormatter\sensitiveMessageFormatter;
use Generator;
use GuzzleHttp\MessageFormatter;
use ReflectionException;
trait MessageFormatterTestTrait
{
/**
* @test
* @throws ReflectionException
* @covers \D3\GuzzleFactory\GuzzleFactory::setMessageFormatter
* @covers \D3\GuzzleFactory\GuzzleFactory::getMessageFormatter
* @dataProvider setGetMessageFormatterDataProvider
*/
public function testSetGetMessageFormatter(
?string $template,
?array $anonymizations,
string $expectedFormatterClass,
string $expectedTemplate
): void {
$sut = GuzzleFactory::create();
$this->callMethod(
$sut,
'setMessageFormatter',
[$template, $anonymizations]
);
$formatter = $this->callMethod($sut, 'getMessageFormatter');
$this->assertInstanceOf($expectedFormatterClass, $formatter);
try {
$this->assertSame($expectedTemplate, $this->getValue($formatter, 'template'));
} catch (ReflectionException) {
// can't get template from sensitiveMessagerFormater because it's in private scope
}
}
/**
* @return Generator
* @throws ReflectionException
*/
public static function setGetMessageFormatterDataProvider(): Generator
{
$sut = GuzzleFactory::create();
$test = new GuzzleFactoryTest('fixture');
yield 'no template, insensitive' => [
null,
null,
MessageFormatter::class,
$test->callMethod($sut, 'getDefaultFormatterTemplate'),
];
yield 'cust template, insensitive' => [
'{req_body}',
null,
MessageFormatter::class,
'{req_body}',
];
yield 'no template, sensitive' => [
null,
['foo', 'bar'],
sensitiveMessageFormatter::class,
$test->callMethod($sut, 'getDefaultFormatterTemplate'),
];
yield 'cust template, sensitive' => [
'{req_body}',
['foo', 'bar'],
sensitiveMessageFormatter::class,
'{req_body}',
];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\GuzzleFactory\GuzzleFactory::getDefaultMessageFormatter
* @dataProvider getDefaultMessageFormatterProvider
*/
public function testGetDefaultMessageFormatter(?string $template, string $expectedTemplate): void
{
$sut = GuzzleFactory::create();
$formatter = $this->callMethod(
$sut,
'getDefaultMessageFormatter',
[$template]
);
$this->assertInstanceOf(MessageFormatter::class, $formatter);
$this->assertSame(
$expectedTemplate,
$this->getValue(
$formatter,
'template'
)
);
}
/**
* @return Generator
* @throws ReflectionException
*/
public static function getDefaultMessageFormatterProvider(): Generator
{
$sut = GuzzleFactory::create();
$test = new GuzzleFactoryTest('fixture');
yield 'default' => [null, $test->callMethod($sut, 'getDefaultFormatterTemplate')];
yield 'custom' => ['{req_body}', '{req_body}'];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\GuzzleFactory\GuzzleFactory::getDefaultFormatterTemplate
*/
public function testGetDefaultFormatterTemplate(): void
{
$sut = GuzzleFactory::create();
$template = $this->callMethod(
$sut,
'getDefaultFormatterTemplate',
);
$this->assertIsString($template);
$this->assertTrue(strlen($template) > 50);
}
}