From 468bcb1c3dc72b766e9a798a4242435d7ee458b1 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 27 Dec 2024 00:02:40 +0100 Subject: [PATCH] add tests --- tests/GuzzleFactoryTest.php | 2 + tests/LoggerTestTrait.php | 118 ++++++++++++++++++++++ tests/MessageFormatterTestTrait.php | 146 ++++++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 tests/LoggerTestTrait.php create mode 100644 tests/MessageFormatterTestTrait.php diff --git a/tests/GuzzleFactoryTest.php b/tests/GuzzleFactoryTest.php index ba4f796..4fe715c 100644 --- a/tests/GuzzleFactoryTest.php +++ b/tests/GuzzleFactoryTest.php @@ -29,6 +29,8 @@ use ReflectionException; class GuzzleFactoryTest extends ApiTestCase { use HeaderTestTrait; + use LoggerTestTrait; + use MessageFormatterTestTrait; /** * @test diff --git a/tests/LoggerTestTrait.php b/tests/LoggerTestTrait.php new file mode 100644 index 0000000..3e47e12 --- /dev/null +++ b/tests/LoggerTestTrait.php @@ -0,0 +1,118 @@ + + * @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]; + } +} diff --git a/tests/MessageFormatterTestTrait.php b/tests/MessageFormatterTestTrait.php new file mode 100644 index 0000000..e634401 --- /dev/null +++ b/tests/MessageFormatterTestTrait.php @@ -0,0 +1,146 @@ + + * @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); + } +}