From ff342fe11d488599a2f0c876bf3e0db37893ddc1 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 25 Dec 2024 23:31:01 +0100 Subject: [PATCH] add tests --- tests/GuzzleFactoryTest.php | 58 ++++++++++ tests/HeaderTestTrait.php | 167 +++++++++++++++++++++++++++++ tests/Helpers/OxidRegistryStub.php | 2 +- tests/Helpers/classAliases.php | 2 +- 4 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 tests/HeaderTestTrait.php diff --git a/tests/GuzzleFactoryTest.php b/tests/GuzzleFactoryTest.php index f4e521b..ba4f796 100644 --- a/tests/GuzzleFactoryTest.php +++ b/tests/GuzzleFactoryTest.php @@ -18,12 +18,18 @@ declare(strict_types=1); namespace D3\GuzzleFactory\tests; use D3\GuzzleFactory\GuzzleFactory; +use GuzzleHttp\Client; +use GuzzleHttp\HandlerStack; +use Monolog\Logger; +use ReflectionException; /** * @coversNothing */ class GuzzleFactoryTest extends ApiTestCase { + use HeaderTestTrait; + /** * @test * @return void @@ -35,4 +41,56 @@ class GuzzleFactoryTest extends ApiTestCase $this->assertInstanceOf(GuzzleFactory::class, $instance); } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::getGuzzle + */ + public function testGetGuzzle(): void + { + $sutMock = $this->getMockBuilder(GuzzleFactory::class) + ->onlyMethods(['getStack', 'getContentType', 'getAccept', 'getUserAgent']) + ->getMock(); + $sutMock->expects($this->once())->method('getStack'); + $sutMock->expects($this->once())->method('getContentType'); + $sutMock->expects($this->once())->method('getAccept'); + $sutMock->expects($this->once())->method('getUserAgent'); + + $this->assertInstanceOf( + Client::class, + $this->callMethod( + $sutMock, + 'getGuzzle', + ['https://google.com'] + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::getStack + */ + public function testGetStack() + { + $sutMock = $this->getMockBuilder(GuzzleFactory::class) + ->onlyMethods(['getLoggers', 'getMessageLevel', 'getMessageFormatter']) + ->getMock(); + $sutMock->expects($this->once())->method('getLoggers')->willReturn([ + new Logger('logger1'), new Logger('logger2'), + ]); + $sutMock->expects($this->exactly(2))->method('getMessageLevel')->willReturn(Logger::INFO); + $sutMock->expects($this->exactly(2))->method('getMessageFormatter'); + + $this->assertInstanceOf( + HandlerStack::class, + $this->callMethod( + $sutMock, + 'getStack' + ) + ); + } } diff --git a/tests/HeaderTestTrait.php b/tests/HeaderTestTrait.php new file mode 100644 index 0000000..ca20299 --- /dev/null +++ b/tests/HeaderTestTrait.php @@ -0,0 +1,167 @@ + + * @link https://www.oxidmodule.com + */ + +namespace D3\GuzzleFactory\tests; + +use D3\GuzzleFactory\GuzzleFactory; +use Generator; +use ReflectionException; + +trait HeaderTestTrait +{ + /** + * @test + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::getAccept + * @dataProvider getAcceptDataProvider + */ + public function testGetAccept(string $expected, bool $viaClassProperty): void + { + $sut = GuzzleFactory::create(); + + if ($viaClassProperty) { + $this->setValue($sut, 'accept', $expected); + } + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'getAccept', + ) + ); + } + + public static function getAcceptDataProvider(): Generator + { + yield 'default' => ['application/json', false]; + yield 'via property' => ['application/xml', true]; + } + + /** + * @test + * @param string $expected + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::setAccept + * @dataProvider setAcceptDataProvider + */ + public function testSetAccept(string $expected): void + { + $sut = GuzzleFactory::create(); + $sut->setAccept($expected); + + $this->assertSame( + $expected, + $this->getValue($sut, 'accept') + ); + } + + public static function setAcceptDataProvider(): Generator + { + yield 'default' => ['application/json']; + yield 'via property' => ['application/xml']; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::getContentType + * @dataProvider getAcceptDataProvider + */ + public function testGetContentType(string $expected, bool $viaClassProperty): void + { + $sut = GuzzleFactory::create(); + + if ($viaClassProperty) { + $this->setValue($sut, 'contentType', $expected); + } + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'getContentType', + ) + ); + } + + /** + * @test + * @param string $expected + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::setContentType + * @dataProvider setAcceptDataProvider + */ + public function testSetContentType(string $expected): void + { + $sut = GuzzleFactory::create(); + $sut->setContentType($expected); + + $this->assertSame( + $expected, + $this->getValue($sut, 'contentType') + ); + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::getUserAgent + * @dataProvider getUserAgentDataProvider + */ + public function testGetUserAgent(string $expected, bool $viaClassProperty): void + { + $sut = GuzzleFactory::create(); + + if ($viaClassProperty) { + $this->setValue($sut, 'userAgent', $expected); + } + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'getUserAgent', + ) + ); + } + + public static function getUserAgentDataProvider(): Generator + { + yield 'default' => ['GuzzleHttp/7', false]; + yield 'via property' => ['myApp/1.0.0', true]; + } + + /** + * @test + * @param string $expected + * @return void + * @throws ReflectionException + * @covers \D3\GuzzleFactory\GuzzleFactory::setUserAgent + * @dataProvider setAcceptDataProvider + */ + public function testSetUserAgent(string $expected): void + { + $sut = GuzzleFactory::create(); + $sut->setUserAgent($expected); + + $this->assertSame( + $expected, + $this->getValue($sut, 'userAgent') + ); + } +} diff --git a/tests/Helpers/OxidRegistryStub.php b/tests/Helpers/OxidRegistryStub.php index 7910fcd..2ddea01 100644 --- a/tests/Helpers/OxidRegistryStub.php +++ b/tests/Helpers/OxidRegistryStub.php @@ -13,7 +13,7 @@ * @link https://www.oxidmodule.com */ -namespace D3\OxidGuzzleFactory\tests\Helpers; +namespace D3\GuzzleFactory\tests\Helpers; use Monolog\Logger; diff --git a/tests/Helpers/classAliases.php b/tests/Helpers/classAliases.php index dd46e69..08e0c17 100644 --- a/tests/Helpers/classAliases.php +++ b/tests/Helpers/classAliases.php @@ -13,7 +13,7 @@ * @link https://www.oxidmodule.com */ -use D3\OxidGuzzleFactory\tests\Helpers\OxidRegistryStub; +use D3\GuzzleFactory\tests\Helpers\OxidRegistryStub; const OX_BASE_PATH = __DIR__;