From 1742a81498d92a5064159729220eac5cf1bea4d7 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Sun, 26 Jun 2022 23:36:40 +0200 Subject: [PATCH 01/19] Add FactoryDecoratorTrait --- .../Psr/Message/FactoryDecoratorTraitTest.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/FactoryDecoratorTraitTest.php diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/FactoryDecoratorTraitTest.php b/tests/Unit/SDK/Common/Http/Psr/Message/FactoryDecoratorTraitTest.php new file mode 100644 index 000000000..9b182888d --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/FactoryDecoratorTraitTest.php @@ -0,0 +1,39 @@ +createImplementation(); + $propagator = $this->createMock(TextMapPropagatorInterface::class); + + $instance->setPropagator($propagator); + + $this->assertSame( + $propagator, + $instance->getPropagator() + ); + } + + private function createImplementation(): object + { + return new class() { + use FactoryDecoratorTrait; + public function setPropagator(TextMapPropagatorInterface $propagator): void + { + $this->propagator = $propagator; + } + }; + } +} From f19c92bf7ec150f453d3f9d0115ef8c4b81035d8 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 00:53:15 +0200 Subject: [PATCH 02/19] Add RequestFactoryDecoratorTrait --- .../Psr/Message/FactoryDecoratorTrait.php | 25 +++++ .../Message/RequestFactoryDecoratorTrait.php | 44 ++++++++ .../RequestFactoryDecoratorTraitTest.php | 101 ++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 src/SDK/Common/Http/Psr/Message/FactoryDecoratorTrait.php create mode 100644 src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php diff --git a/src/SDK/Common/Http/Psr/Message/FactoryDecoratorTrait.php b/src/SDK/Common/Http/Psr/Message/FactoryDecoratorTrait.php new file mode 100644 index 000000000..8d215a3cd --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/FactoryDecoratorTrait.php @@ -0,0 +1,25 @@ +propagator; + } +} diff --git a/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php new file mode 100644 index 000000000..f8d792987 --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php @@ -0,0 +1,44 @@ +createServerRequest($method, $uri, $serverParams) + : $factory->createRequest($method, $uri); + + $headers = []; + + $propagator->inject($headers); + + foreach ($headers as $name => $value) { + $request = $request->withHeader($name, $value); + } + + return $request; + } +} diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php new file mode 100644 index 000000000..bd6b9c33c --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php @@ -0,0 +1,101 @@ +createImplementation(); + $request = $this->createMock(RequestInterface::class); + $request->method('withHeader') + ->willReturn($request); + $factory = $this->createMock(RequestFactoryInterface::class); + $factory->method('createRequest') + ->willReturn($request); + $propagator = $this->createPropagatorMock(); + + $this->assertInstanceOf( + RequestInterface::class, + $instance->createRequest( + $factory, + $propagator, + 'POST', + 'https://example.com' + ) + ); + } + public function test_create_server_request(): void + { + $instance = $this->createImplementation(); + $request = $this->createMock(ServerRequestInterface::class); + $request->method('withHeader') + ->willReturn($request); + $factory = $this->createMock(ServerRequestFactoryInterface::class); + $factory->method('createServerRequest') + ->willReturn($request); + $propagator = $this->createPropagatorMock(); + + $this->assertInstanceOf( + ServerRequestInterface::class, + $instance->createRequest( + $factory, + $propagator, + 'POST', + 'https://example.com', + [] + ) + ); + } + + private function createImplementation(): object + { + return new class() { + use RequestFactoryDecoratorTrait; + + /** + * @param $factory RequestFactoryInterface|ServerRequestFactoryInterface + * @return RequestInterface|ServerRequestInterface + */ + public static function createRequest( + $factory, + TextMapPropagatorInterface $propagator, + string $method, + $uri, + array $serverParams = [] + ) { + return self::doCreateRequest( + $factory, + $propagator, + $method, + $uri, + $serverParams + ); + } + }; + } + + private function createPropagatorMock(): TextMapPropagatorInterface + { + $propagator = $this->createMock(TextMapPropagatorInterface::class); + $propagator->method('inject') + ->willReturnCallback(static function (array &$carrier) { + $carrier['foo'] = 'bar'; + }); + + return $propagator; + } +} From 880c444c383fb1efdc583824659703e5184354f6 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 01:51:13 +0200 Subject: [PATCH 03/19] Add RequestFactoryDecorator --- .../Psr/Message/FactoryDecoratorInterface.php | 12 ++++++ .../Psr/Message/RequestFactoryDecorator.php | 35 ++++++++++++++++++ .../RequestFactoryDecoratorInterface.php | 11 ++++++ .../Http/Psr/Message/CreatesMockTrait.php | 17 +++++++++ .../Message/RequestFactoryDecoratorTest.php | 37 +++++++++++++++++++ .../RequestFactoryDecoratorTraitTest.php | 24 +++--------- .../Psr/Message/UsesRequestFactoryTrait.php | 29 +++++++++++++++ .../Message/UsesTextMapPropagatorTrait.php | 23 ++++++++++++ 8 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 src/SDK/Common/Http/Psr/Message/FactoryDecoratorInterface.php create mode 100644 src/SDK/Common/Http/Psr/Message/RequestFactoryDecorator.php create mode 100644 src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorInterface.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/CreatesMockTrait.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/UsesRequestFactoryTrait.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php diff --git a/src/SDK/Common/Http/Psr/Message/FactoryDecoratorInterface.php b/src/SDK/Common/Http/Psr/Message/FactoryDecoratorInterface.php new file mode 100644 index 000000000..666b19df3 --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/FactoryDecoratorInterface.php @@ -0,0 +1,12 @@ +decorated = $decorated; + $this->propagator = $propagator; + } + + public static function create(RequestFactoryInterface $decorated, TextMapPropagatorInterface $propagator): self + { + return new self($decorated, $propagator); + } + + public function createRequest(string $method, $uri): RequestInterface + { + return self::doCreateRequest( + $this->decorated, + $this->propagator, + $method, + $uri + ); + } +} diff --git a/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorInterface.php b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorInterface.php new file mode 100644 index 000000000..3b043c48d --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorInterface.php @@ -0,0 +1,11 @@ + $originalClassName + * @psalm-return MockObject&RealInstanceType + */ + abstract protected function createMock(string $originalClassName): MockObject; +} diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php new file mode 100644 index 000000000..5f8f9a47e --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php @@ -0,0 +1,37 @@ +createRequestFactoryMock($method, $uri), + $this->createPropagatorMock() + ); + + $this->assertSame( + $method, + $instance->createRequest($method, $uri)->getMethod() + ); + $this->assertSame( + $uri, + $instance->createRequest($method, $uri)->getUri() + ); + } +} diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php index bd6b9c33c..6d4e0cad5 100644 --- a/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php +++ b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTraitTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\SDK\Common\Http\Psr\Message; +namespace OpenTelemetry\Tests\Unit\SDK\Common\Http\Psr\Message; use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; use OpenTelemetry\SDK\Common\Http\Psr\Message\RequestFactoryDecoratorTrait; @@ -17,15 +17,13 @@ */ class RequestFactoryDecoratorTraitTest extends TestCase { + use UsesTextMapPropagatorTrait; + use UsesRequestFactoryTrait; + public function test_create_request(): void { $instance = $this->createImplementation(); - $request = $this->createMock(RequestInterface::class); - $request->method('withHeader') - ->willReturn($request); - $factory = $this->createMock(RequestFactoryInterface::class); - $factory->method('createRequest') - ->willReturn($request); + $factory = $this->createRequestFactoryMock(); $propagator = $this->createPropagatorMock(); $this->assertInstanceOf( @@ -38,6 +36,7 @@ public function test_create_request(): void ) ); } + public function test_create_server_request(): void { $instance = $this->createImplementation(); @@ -87,15 +86,4 @@ public static function createRequest( } }; } - - private function createPropagatorMock(): TextMapPropagatorInterface - { - $propagator = $this->createMock(TextMapPropagatorInterface::class); - $propagator->method('inject') - ->willReturnCallback(static function (array &$carrier) { - $carrier['foo'] = 'bar'; - }); - - return $propagator; - } } diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/UsesRequestFactoryTrait.php b/tests/Unit/SDK/Common/Http/Psr/Message/UsesRequestFactoryTrait.php new file mode 100644 index 000000000..e816e6f6b --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/UsesRequestFactoryTrait.php @@ -0,0 +1,29 @@ +createMock(RequestInterface::class); + $request->method('withHeader') + ->willReturn($request); + $request->method('getMethod') + ->willReturn((string) $requestMethod); + $request->method('getUri') + ->willReturn((string) $requestUri); + $factory = $this->createMock(RequestFactoryInterface::class); + $factory->method('createRequest') + ->willReturn($request); + + return $factory; + } +} diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php b/tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php new file mode 100644 index 000000000..94fa5d0a6 --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php @@ -0,0 +1,23 @@ +createMock(TextMapPropagatorInterface::class); + $propagator->method('inject') + ->willReturnCallback(static function (array &$carrier) { + $carrier['foo'] = 'bar'; + }); + + return $propagator; + } +} From aca50babb32d304213f239c5d0c8d606bc8f3c88 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 02:03:59 +0200 Subject: [PATCH 04/19] Add ServerRequestFactoryDecorator --- .../Psr/Message/RequestFactoryDecorator.php | 2 +- .../Message/ServerRequestFactoryDecorator.php | 36 ++++++++++++++++++ ...ServerRequestFactoryDecoratorInterface.php | 9 +++++ .../RequestFactoryDecoratorTraitTest.php | 8 +--- .../ServerRequestFactoryDecoratorTest.php | 37 +++++++++++++++++++ .../Message/UsesServerRequestFactoryTrait.php | 29 +++++++++++++++ 6 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php create mode 100644 src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/UsesServerRequestFactoryTrait.php diff --git a/src/SDK/Common/Http/Psr/Message/RequestFactoryDecorator.php b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecorator.php index f7586c6ef..3eeb92d68 100644 --- a/src/SDK/Common/Http/Psr/Message/RequestFactoryDecorator.php +++ b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecorator.php @@ -8,7 +8,7 @@ use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; -class RequestFactoryDecorator implements RequestFactoryDecoratorInterface +final class RequestFactoryDecorator implements RequestFactoryDecoratorInterface { use RequestFactoryDecoratorTrait; diff --git a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php new file mode 100644 index 000000000..471fb46b8 --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php @@ -0,0 +1,36 @@ +decorated = $decorated; + $this->propagator = $propagator; + } + + public static function create(ServerRequestFactoryInterface $decorated, TextMapPropagatorInterface $propagator): self + { + return new self($decorated, $propagator); + } + + public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface + { + return self::doCreateRequest( + $this->decorated, + $this->propagator, + $method, + $uri, + $serverParams + ); + } +} diff --git a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php new file mode 100644 index 000000000..15dd2af7d --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php @@ -0,0 +1,9 @@ +createImplementation(); - $request = $this->createMock(ServerRequestInterface::class); - $request->method('withHeader') - ->willReturn($request); - $factory = $this->createMock(ServerRequestFactoryInterface::class); - $factory->method('createServerRequest') - ->willReturn($request); + $factory = $this->createServerRequestFactoryMock(); $propagator = $this->createPropagatorMock(); $this->assertInstanceOf( diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php b/tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php new file mode 100644 index 000000000..895cda197 --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php @@ -0,0 +1,37 @@ +createServerRequestFactoryMock($method, $uri), + $this->createPropagatorMock() + ); + + $this->assertSame( + $method, + $instance->createServerRequest($method, $uri)->getMethod() + ); + $this->assertSame( + $uri, + $instance->createServerRequest($method, $uri)->getUri() + ); + } +} diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/UsesServerRequestFactoryTrait.php b/tests/Unit/SDK/Common/Http/Psr/Message/UsesServerRequestFactoryTrait.php new file mode 100644 index 000000000..2b7007ad8 --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/UsesServerRequestFactoryTrait.php @@ -0,0 +1,29 @@ +createMock(ServerRequestInterface::class); + $request->method('withHeader') + ->willReturn($request); + $request->method('getMethod') + ->willReturn((string) $requestMethod); + $request->method('getUri') + ->willReturn((string) $requestUri); + $factory = $this->createMock(ServerRequestFactoryInterface::class); + $factory->method('createServerRequest') + ->willReturn($request); + + return $factory; + } +} From 6b4e9d03921326a956638fc7993d25387356663e Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 11:55:57 +0200 Subject: [PATCH 05/19] Add ResponseFactoryDecorator --- .../Psr/Message/ResponseFactoryDecorator.php | 39 +++++++++++++++++++ .../ResponseFactoryDecoratorInterface.php | 11 ++++++ .../Message/ResponseFactoryDecoratorTest.php | 37 ++++++++++++++++++ .../Psr/Message/UsesResponseFactoryTrait.php | 29 ++++++++++++++ .../Message/UsesTextMapPropagatorTrait.php | 3 ++ 5 files changed, 119 insertions(+) create mode 100644 src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php create mode 100644 src/SDK/Common/Http/Psr/Message/ResponseFactoryDecoratorInterface.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/ResponseFactoryDecoratorTest.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/UsesResponseFactoryTrait.php diff --git a/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php b/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php new file mode 100644 index 000000000..4c54e0200 --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php @@ -0,0 +1,39 @@ +decorated = $decorated; + $this->propagator = $propagator; + } + + public static function create(ResponseFactoryInterface $decorated, TextMapPropagatorInterface $propagator): self + { + return new self($decorated, $propagator); + } + + /** + * @inheritDoc + */ + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface + { + $response = $this->decorated->createResponse($code, $reasonPhrase); + + $headers = $response->getHeaders(); + + $this->propagator->extract($headers); + + return $response; + } +} diff --git a/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecoratorInterface.php b/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecoratorInterface.php new file mode 100644 index 000000000..f5d05e92a --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecoratorInterface.php @@ -0,0 +1,11 @@ +createResponseFactoryMock($code, $reason), + $this->createPropagatorMock() + ); + + $this->assertSame( + $code, + $instance->createResponse($code, $reason)->getStatusCode() + ); + $this->assertSame( + $reason, + $instance->createResponse($code, $reason)->getReasonPhrase() + ); + } +} diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/UsesResponseFactoryTrait.php b/tests/Unit/SDK/Common/Http/Psr/Message/UsesResponseFactoryTrait.php new file mode 100644 index 000000000..47aa263d4 --- /dev/null +++ b/tests/Unit/SDK/Common/Http/Psr/Message/UsesResponseFactoryTrait.php @@ -0,0 +1,29 @@ +createMock(ResponseInterface::class); + $response->method('getHeaders') + ->willReturn(['foo' => 'bar']); + $response->method('getStatusCode') + ->willReturn($code); + $response->method('getReasonPhrase') + ->willReturn($reasonPhrase); + $factory = $this->createMock(ResponseFactoryInterface::class); + $factory->method('createResponse') + ->willReturn($response); + + return $factory; + } +} diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php b/tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php index 94fa5d0a6..5f437083c 100644 --- a/tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php +++ b/tests/Unit/SDK/Common/Http/Psr/Message/UsesTextMapPropagatorTrait.php @@ -4,6 +4,7 @@ namespace OpenTelemetry\Tests\Unit\SDK\Common\Http\Psr\Message; +use OpenTelemetry\Context\Context; use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; trait UsesTextMapPropagatorTrait @@ -17,6 +18,8 @@ private function createPropagatorMock(): TextMapPropagatorInterface ->willReturnCallback(static function (array &$carrier) { $carrier['foo'] = 'bar'; }); + $propagator->method('extract') + ->willReturn(Context::getCurrent()); return $propagator; } From 917a14d6ad951913210c816a2fa4bad1a9618edf Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 12:29:26 +0200 Subject: [PATCH 06/19] Add MessageFactory --- .../Http/Psr/Message/MessageFactory.php | 52 +++++++++++++ .../Psr/Message/MessageFactoryInterface.php | 13 ++++ .../Http/Psr/Message/MessageFactoryTest.php | 78 +++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 src/SDK/Common/Http/Psr/Message/MessageFactory.php create mode 100644 src/SDK/Common/Http/Psr/Message/MessageFactoryInterface.php create mode 100644 tests/Unit/SDK/Common/Http/Psr/Message/MessageFactoryTest.php diff --git a/src/SDK/Common/Http/Psr/Message/MessageFactory.php b/src/SDK/Common/Http/Psr/Message/MessageFactory.php new file mode 100644 index 000000000..fd2010270 --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/MessageFactory.php @@ -0,0 +1,52 @@ +requestFactory = $requestFactory; + $this->responseFactory = $responseFactory; + $this->serverRequestFactory = $serverRequestFactory; + } + + public static function create( + RequestFactoryInterface $requestFactory, + ResponseFactoryInterface $responseFactory, + ServerRequestFactoryInterface $serverRequestFactory + ): self { + return new self($requestFactory, $responseFactory, $serverRequestFactory); + } + + public function createRequest(string $method, $uri): RequestInterface + { + return $this->requestFactory->createRequest($method, $uri); + } + + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface + { + return $this->responseFactory->createResponse($code, $reasonPhrase); + } + + public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface + { + return $this->serverRequestFactory->createServerRequest($method, $uri, $serverParams); + } +} diff --git a/src/SDK/Common/Http/Psr/Message/MessageFactoryInterface.php b/src/SDK/Common/Http/Psr/Message/MessageFactoryInterface.php new file mode 100644 index 000000000..97258491f --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/MessageFactoryInterface.php @@ -0,0 +1,13 @@ +createResponseFactoryMock(); + $serverRequestFactory = $this->createServerRequestFactoryMock(); + $requestFactory = $this->createMock(RequestFactoryInterface::class); + $requestFactory->expects($this->once()) + ->method('createRequest') + ->willReturn( + $this->createMock(RequestInterface::class) + ); + + MessageFactory::create( + $requestFactory, + $responseFactory, + $serverRequestFactory + )->createRequest('POST', 'https://example.com/'); + } + + public function test_create_server_request(): void + { + $responseFactory = $this->createResponseFactoryMock(); + $requestFactory = $this->createRequestFactoryMock(); + $serverRequestFactory = $this->createMock(ServerRequestFactoryInterface::class); + $serverRequestFactory->expects($this->once()) + ->method('createServerRequest') + ->willReturn( + $this->createMock(ServerRequestInterface::class) + ); + + MessageFactory::create( + $requestFactory, + $responseFactory, + $serverRequestFactory + )->createServerRequest('POST', 'https://example.com/', []); + } + + public function test_create_response(): void + { + $requestFactory = $this->createRequestFactoryMock(); + $serverRequestFactory = $this->createServerRequestFactoryMock(); + $responseFactory = $this->createMock(ResponseFactoryInterface::class); + $responseFactory->expects($this->once()) + ->method('createResponse') + ->willReturn( + $this->createMock(ResponseInterface::class) + ); + + MessageFactory::create( + $requestFactory, + $responseFactory, + $serverRequestFactory + )->createResponse(201, 'because'); + } +} From 64ec250e5f864fcb48cd6c7e14897a9fd771877b Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 12:54:26 +0200 Subject: [PATCH 07/19] Add FactoryResolverInterface --- .../Psr/Message/FactoryResolverInterface.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/SDK/Common/Http/Psr/Message/FactoryResolverInterface.php diff --git a/src/SDK/Common/Http/Psr/Message/FactoryResolverInterface.php b/src/SDK/Common/Http/Psr/Message/FactoryResolverInterface.php new file mode 100644 index 000000000..4582e19ce --- /dev/null +++ b/src/SDK/Common/Http/Psr/Message/FactoryResolverInterface.php @@ -0,0 +1,22 @@ + Date: Mon, 27 Jun 2022 12:55:40 +0200 Subject: [PATCH 08/19] Add PsrClientResolverInterface --- src/SDK/Common/Http/Psr/Client/ResolverInterface.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/SDK/Common/Http/Psr/Client/ResolverInterface.php diff --git a/src/SDK/Common/Http/Psr/Client/ResolverInterface.php b/src/SDK/Common/Http/Psr/Client/ResolverInterface.php new file mode 100644 index 000000000..ba028e38a --- /dev/null +++ b/src/SDK/Common/Http/Psr/Client/ResolverInterface.php @@ -0,0 +1,12 @@ + Date: Mon, 27 Jun 2022 12:56:20 +0200 Subject: [PATCH 09/19] Add HttpPlugClientResolverInterface --- .../Http/HttpPlug/Client/ResolverInterface.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/SDK/Common/Http/HttpPlug/Client/ResolverInterface.php diff --git a/src/SDK/Common/Http/HttpPlug/Client/ResolverInterface.php b/src/SDK/Common/Http/HttpPlug/Client/ResolverInterface.php new file mode 100644 index 000000000..2854b4c91 --- /dev/null +++ b/src/SDK/Common/Http/HttpPlug/Client/ResolverInterface.php @@ -0,0 +1,15 @@ + Date: Mon, 27 Jun 2022 12:57:25 +0200 Subject: [PATCH 10/19] add DependencyResolverInterface --- src/SDK/Common/Http/DependencyResolverInterface.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/SDK/Common/Http/DependencyResolverInterface.php diff --git a/src/SDK/Common/Http/DependencyResolverInterface.php b/src/SDK/Common/Http/DependencyResolverInterface.php new file mode 100644 index 000000000..824335213 --- /dev/null +++ b/src/SDK/Common/Http/DependencyResolverInterface.php @@ -0,0 +1,13 @@ + Date: Mon, 27 Jun 2022 14:07:28 +0200 Subject: [PATCH 11/19] Add HttpDiscovery\MessageFactoryResolver --- .../HttpDiscovery/MessageFactoryResolver.php | 88 +++++++++++++++++++ .../MessageFactoryResolverTest.php | 76 ++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php create mode 100644 tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php diff --git a/src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php new file mode 100644 index 000000000..b2b77f99f --- /dev/null +++ b/src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php @@ -0,0 +1,88 @@ +requestFactory = $requestFactory; + $this->responseFactory = $responseFactory; + $this->serverRequestFactory = $serverRequestFactory; + $this->streamFactory = $streamFactory; + $this->uploadedFileFactory = $uploadedFileFactory; + $this->uriFactory = $uriFactory; + } + + public static function create( + ?RequestFactoryInterface $requestFactory = null, + ?ResponseFactoryInterface $responseFactory = null, + ?ServerRequestFactoryInterface $serverRequestFactory = null, + ?StreamFactoryInterface $streamFactory = null, + ?UploadedFileFactoryInterface $uploadedFileFactory = null, + ?UriFactoryInterface $uriFactory = null + ): self { + return new self( + $requestFactory, + $responseFactory, + $serverRequestFactory, + $streamFactory, + $uploadedFileFactory, + $uriFactory + ); + } + + public function resolveRequestFactory(): RequestFactoryInterface + { + return $this->requestFactory ??= Psr17FactoryDiscovery::findRequestFactory(); + } + + public function resolveResponseFactory(): ResponseFactoryInterface + { + return $this->responseFactory ??= Psr17FactoryDiscovery::findResponseFactory(); + } + + public function resolveServerRequestFactory(): ServerRequestFactoryInterface + { + return $this->serverRequestFactory ??= Psr17FactoryDiscovery::findServerRequestFactory(); + } + + public function resolveStreamFactory(): StreamFactoryInterface + { + return $this->streamFactory ??= Psr17FactoryDiscovery::findStreamFactory(); + } + + public function resolveUploadedFileFactory(): UploadedFileFactoryInterface + { + return $this->uploadedFileFactory ??= Psr17FactoryDiscovery::findUploadedFileFactory(); + } + + public function resolveUriFactory(): UriFactoryInterface + { + return $this->uriFactory ??= Psr17FactoryDiscovery::findUriFactory(); + } +} diff --git a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php new file mode 100644 index 000000000..6c256767c --- /dev/null +++ b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php @@ -0,0 +1,76 @@ +assertSame( + $dependency, + $instance->{$method}() + ); + } + + public function provideDependencies(): Generator + { + $dependencies = []; + + foreach (self::DEPENDENCIES as $interface) { + $dependencies[$this->resolveMethodName($interface)] = $this->createMock($interface); + } + + foreach ($dependencies as $method => $dependency) { + yield [$method, $dependency, array_values($dependencies)]; + } + } + + private function resolveMethodName(string $interface): string + { + return sprintf( + 'resolve%s', + str_replace( + 'Interface', + '', + (new ReflectionClass($interface))->getShortName() + ) + ); + } +} From 1bb7bd971071e08de9dad328454a7eff54762afa Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 14:17:03 +0200 Subject: [PATCH 12/19] Add HttpDiscovery\PsrClientResolver --- .../HttpDiscovery/PsrClientResolver.php | 29 ++++++++++++++++ .../HttpDiscovery/PsrClientResolverTest.php | 33 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php create mode 100644 tests/Unit/SDK/Common/Adapter/HttpDiscovery/PsrClientResolverTest.php diff --git a/src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php new file mode 100644 index 000000000..011861b0e --- /dev/null +++ b/src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php @@ -0,0 +1,29 @@ +client = $client; + } + + public static function create(?ClientInterface $client = null): self + { + return new self($client); + } + + public function resolvePsrClient(): ClientInterface + { + return $this->client ??= Psr18ClientDiscovery::find(); + } +} diff --git a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/PsrClientResolverTest.php b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/PsrClientResolverTest.php new file mode 100644 index 000000000..48c5c0ba5 --- /dev/null +++ b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/PsrClientResolverTest.php @@ -0,0 +1,33 @@ +createMock(ClientInterface::class); + $instance = PsrClientResolver::create($dependency); + + $this->assertSame( + $dependency, + $instance->resolvePsrClient() + ); + } +} From f8f518a66704a2831e2f15d447aea3f1616f6b55 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 14:22:50 +0200 Subject: [PATCH 13/19] Add HttpDiscovery\HttpPlugClientResolver --- .../HttpDiscovery/HttpPlugClientResolver.php | 38 +++++++++++++++++++ .../HttpPlugClientResolverTest.php | 38 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php create mode 100644 tests/Unit/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolverTest.php diff --git a/src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php new file mode 100644 index 000000000..edc7d497d --- /dev/null +++ b/src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php @@ -0,0 +1,38 @@ +httpClient = $httpClient; + $this->httpAsyncClient = $httpAsyncClient; + } + + public static function create(?HttpClient $httpClient = null, ?HttpAsyncClient $httpAsyncClient = null): self + { + return new self($httpClient, $httpAsyncClient); + } + + public function resolveHttpPlugClient(): HttpClient + { + return $this->httpClient ??= HttpClientDiscovery::find(); + } + + public function resolveHttpPlugAsyncClient(): HttpAsyncClient + { + return $this->httpAsyncClient ??= HttpAsyncClientDiscovery::find(); + } +} diff --git a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolverTest.php b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolverTest.php new file mode 100644 index 000000000..253d14231 --- /dev/null +++ b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolverTest.php @@ -0,0 +1,38 @@ +createMock(HttpClient::class); + $instance = HttpPlugClientResolver::create($dependency); + + $this->assertSame( + $dependency, + $instance->resolveHttpPlugClient() + ); + } + + public function test_resolve_http_plug_async_client(): void + { + $dependency = $this->createMock(HttpAsyncClient::class); + $instance = HttpPlugClientResolver::create(null, $dependency); + + $this->assertSame( + $dependency, + $instance->resolveHttpPlugAsyncClient() + ); + } +} From 3d3bc83fd542a52bdae2275b497d592c0a81dc5b Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 15:08:49 +0200 Subject: [PATCH 14/19] Add HttpDiscovery\DependencyResolver --- .../HttpDiscovery/DependencyResolver.php | 89 +++++++++++++++ .../HttpDiscovery/DependencyResolverTest.php | 105 ++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php create mode 100644 tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php diff --git a/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php new file mode 100644 index 000000000..b24562804 --- /dev/null +++ b/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php @@ -0,0 +1,89 @@ +messageFactoryResolver = $messageFactoryResolver ?? MessageFactoryResolver::create(); + $this->psrClientResolver = $psrClientResolver ?? PsrClientResolver::create(); + $this->httpPlugClientResolver = $httpPlugClientResolver ?? HttpPlugClientResolver::create(); + } + + public static function create( + ?MessageFactoryResolverInterface $messageFactoryResolver, + ?PsrClientResolverInterface $psrClientResolver, + ?HttpPlugClientResolverInterface $httpPlugClientResolver + ): self { + return new self($messageFactoryResolver, $psrClientResolver, $httpPlugClientResolver); + } + + public function resolveRequestFactory(): RequestFactoryInterface + { + return $this->messageFactoryResolver->resolveRequestFactory(); + } + + public function resolveResponseFactory(): ResponseFactoryInterface + { + return $this->messageFactoryResolver->resolveResponseFactory(); + } + + public function resolveServerRequestFactory(): ServerRequestFactoryInterface + { + return $this->messageFactoryResolver->resolveServerRequestFactory(); + } + + public function resolveStreamFactory(): StreamFactoryInterface + { + return $this->messageFactoryResolver->resolveStreamFactory(); + } + + public function resolveUploadedFileFactory(): UploadedFileFactoryInterface + { + return $this->messageFactoryResolver->resolveUploadedFileFactory(); + } + + public function resolveUriFactory(): UriFactoryInterface + { + return $this->messageFactoryResolver->resolveUriFactory(); + } + + public function resolveHttpPlugClient(): HttpClient + { + return $this->httpPlugClientResolver->resolveHttpPlugClient(); + } + + public function resolveHttpPlugAsyncClient(): HttpAsyncClient + { + return $this->httpPlugClientResolver->resolveHttpPlugAsyncClient(); + } + + public function resolvePsrClient(): ClientInterface + { + return $this->psrClientResolver->resolvePsrClient(); + } +} diff --git a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php new file mode 100644 index 000000000..91223417c --- /dev/null +++ b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php @@ -0,0 +1,105 @@ + [ + RequestFactoryInterface::class, + ResponseFactoryInterface::class, + ServerRequestFactoryInterface::class, + StreamFactoryInterface::class, + UploadedFileFactoryInterface::class, + UriFactoryInterface::class, + ], + PsrClientResolverInterface::class => [ + PsrClientInterface::class, + ], + HttpPlugClientResolverInterface::class => [ + HttpClient::class, + HttpAsyncClient::class, + ], + ]; + private const METHOD_NAME_REPLACEMENTS = [ + MessageFactoryResolverInterface::class => [], + HttpPlugClientResolverInterface::class => ['Http', 'HttpPlug'], + PsrClientResolverInterface::class => ['Client', 'PsrClient'], + ]; + + /** + * @dataProvider provideDependencies + */ + public function test_resolve(string $method, object $dependency, array $arguments): void + { + $instance = DependencyResolver::create(...$arguments); + + $this->assertSame( + $dependency, + $instance->{$method}() + ); + } + + public function provideDependencies(): Generator + { + $arguments = []; + $dependencies = []; + + foreach (self::DEPENDENCIES as $resolverInterface => $interfaces) { + $resolver = $this->createMock($resolverInterface); + + foreach ($interfaces as $interface) { + $method = $this->resolveMethodName($interface, self::METHOD_NAME_REPLACEMENTS[$resolverInterface]); + $dependency = $this->createMock($interface); + $resolver->method($method)->willReturn($dependency); + $dependencies[$method] = $dependency; + } + + $arguments[] = $resolver; + } + + foreach ($dependencies as $method => $dependency) { + yield [$method, $dependency, $arguments]; + } + } + + private function resolveMethodName(string $interface, array $replacements = []): string + { + $interface = (new ReflectionClass($interface))->getShortName(); + + if (count($replacements) >= 2) { + $interface = str_replace($replacements[0], $replacements[1], $interface); + } + + return sprintf( + 'resolve%s', + str_replace( + 'Interface', + '', + $interface + ) + ); + } +} From c250661a00dbddcc3f0bd34b82d09b6bf44fc7f1 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 15:19:12 +0200 Subject: [PATCH 15/19] Finalize --- src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php | 2 +- src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php | 2 +- src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php | 2 +- src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php | 2 +- src/SDK/Common/Http/Psr/Message/MessageFactory.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php index b24562804..5b5285a09 100644 --- a/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php +++ b/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php @@ -18,7 +18,7 @@ use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UriFactoryInterface; -class DependencyResolver implements DependencyResolverInterface +final class DependencyResolver implements DependencyResolverInterface { private MessageFactoryResolverInterface $messageFactoryResolver; private PsrClientResolverInterface $psrClientResolver; diff --git a/src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php index edc7d497d..81b6943fa 100644 --- a/src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php +++ b/src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php @@ -10,7 +10,7 @@ use Http\Discovery\HttpClientDiscovery; use OpenTelemetry\SDK\Common\Http\HttpPlug\Client\ResolverInterface; -class HttpPlugClientResolver implements ResolverInterface +final class HttpPlugClientResolver implements ResolverInterface { private ?HttpClient $httpClient; private ?HttpAsyncClient $httpAsyncClient; diff --git a/src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php index b2b77f99f..6ed0895ff 100644 --- a/src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php +++ b/src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php @@ -13,7 +13,7 @@ use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UriFactoryInterface; -class MessageFactoryResolver implements FactoryResolverInterface +final class MessageFactoryResolver implements FactoryResolverInterface { private ?RequestFactoryInterface $requestFactory; private ?ResponseFactoryInterface $responseFactory; diff --git a/src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php index 011861b0e..46fb36312 100644 --- a/src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php +++ b/src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php @@ -8,7 +8,7 @@ use OpenTelemetry\SDK\Common\Http\Psr\Client\ResolverInterface; use Psr\Http\Client\ClientInterface; -class PsrClientResolver implements ResolverInterface +final class PsrClientResolver implements ResolverInterface { private ?ClientInterface $client; diff --git a/src/SDK/Common/Http/Psr/Message/MessageFactory.php b/src/SDK/Common/Http/Psr/Message/MessageFactory.php index fd2010270..8e99d64c0 100644 --- a/src/SDK/Common/Http/Psr/Message/MessageFactory.php +++ b/src/SDK/Common/Http/Psr/Message/MessageFactory.php @@ -11,7 +11,7 @@ use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestInterface; -class MessageFactory implements MessageFactoryInterface +final class MessageFactory implements MessageFactoryInterface { private RequestFactoryInterface $requestFactory; private ResponseFactoryInterface $responseFactory; From 2bb2597f34b26e839702cf117f340a6086d15422 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 15:34:52 +0200 Subject: [PATCH 16/19] Make Phan happy --- src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php index f8d792987..b5602d275 100644 --- a/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php +++ b/src/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTrait.php @@ -11,6 +11,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; +/** @phan-file-suppress PhanUnreferencedUseNormal */ trait RequestFactoryDecoratorTrait { use FactoryDecoratorTrait; From 918067f4aa71cb68ffb239b00e903537833ed301 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 15:48:20 +0200 Subject: [PATCH 17/19] Make Psalm happy --- src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php | 1 + .../Common/Http/Psr/Message/ServerRequestFactoryDecorator.php | 4 ++++ .../Psr/Message/ServerRequestFactoryDecoratorInterface.php | 4 +++- .../Common/Adapter/HttpDiscovery/DependencyResolverTest.php | 3 +++ .../Adapter/HttpDiscovery/MessageFactoryResolverTest.php | 3 +++ 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php b/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php index 4c54e0200..783b2f45b 100644 --- a/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php +++ b/src/SDK/Common/Http/Psr/Message/ResponseFactoryDecorator.php @@ -28,6 +28,7 @@ public static function create(ResponseFactoryInterface $decorated, TextMapPropag */ public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface { + /** @psalm-suppress PossiblyUndefinedMethod */ $response = $this->decorated->createResponse($code, $reasonPhrase); $headers = $response->getHeaders(); diff --git a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php index 471fb46b8..7fd75ce77 100644 --- a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php +++ b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php @@ -23,6 +23,10 @@ public static function create(ServerRequestFactoryInterface $decorated, TextMapP return new self($decorated, $propagator); } + /** + * @psalm-suppress MoreSpecificReturnType + * @psalm-suppress LessSpecificReturnStatement + */ public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface { return self::doCreateRequest( diff --git a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php index 15dd2af7d..c14a8032d 100644 --- a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php +++ b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorInterface.php @@ -4,6 +4,8 @@ namespace OpenTelemetry\SDK\Common\Http\Psr\Message; -interface ServerRequestFactoryDecoratorInterface +use Psr\Http\Message\ServerRequestFactoryInterface; + +interface ServerRequestFactoryDecoratorInterface extends ServerRequestFactoryInterface, FactoryDecoratorInterface { } diff --git a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php index 91223417c..3f47f77dd 100644 --- a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php +++ b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/DependencyResolverTest.php @@ -85,6 +85,9 @@ public function provideDependencies(): Generator } } + /** + * @psalm-param class-string $interface + */ private function resolveMethodName(string $interface, array $replacements = []): string { $interface = (new ReflectionClass($interface))->getShortName(); diff --git a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php index 6c256767c..e5dee1269 100644 --- a/tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php +++ b/tests/Unit/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolverTest.php @@ -62,6 +62,9 @@ public function provideDependencies(): Generator } } + /** + * @psalm-param class-string $interface + */ private function resolveMethodName(string $interface): string { return sprintf( From 78e270514ae7a76c525df474bd29336862c4daa1 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Mon, 27 Jun 2022 16:06:35 +0200 Subject: [PATCH 18/19] Make Phpstan happy --- .../Common/Http/Psr/Message/ServerRequestFactoryDecorator.php | 1 + .../SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php | 2 +- .../Http/Psr/Message/ServerRequestFactoryDecoratorTest.php | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php index 7fd75ce77..0b2fdf891 100644 --- a/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php +++ b/src/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecorator.php @@ -29,6 +29,7 @@ public static function create(ServerRequestFactoryInterface $decorated, TextMapP */ public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface { + /** @phpstan-ignore-next-line */ return self::doCreateRequest( $this->decorated, $this->propagator, diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php index 5f8f9a47e..7bb2b08c2 100644 --- a/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php +++ b/tests/Unit/SDK/Common/Http/Psr/Message/RequestFactoryDecoratorTest.php @@ -31,7 +31,7 @@ public function test_create_request(): void ); $this->assertSame( $uri, - $instance->createRequest($method, $uri)->getUri() + (string) $instance->createRequest($method, $uri)->getUri() ); } } diff --git a/tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php b/tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php index 895cda197..cb4235764 100644 --- a/tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php +++ b/tests/Unit/SDK/Common/Http/Psr/Message/ServerRequestFactoryDecoratorTest.php @@ -31,7 +31,7 @@ public function test_create_server_request(): void ); $this->assertSame( $uri, - $instance->createServerRequest($method, $uri)->getUri() + (string) $instance->createServerRequest($method, $uri)->getUri() ); } } From 1f514913fbb5a359286975dcc928ca8a0762bea9 Mon Sep 17 00:00:00 2001 From: Timo Michna Date: Tue, 28 Jun 2022 14:27:55 +0200 Subject: [PATCH 19/19] Fix dependency --- .../Adapter/HttpDiscovery/DependencyResolver.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php b/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php index 5b5285a09..b1fd40b79 100644 --- a/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php +++ b/src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php @@ -25,9 +25,9 @@ final class DependencyResolver implements DependencyResolverInterface private HttpPlugClientResolverInterface $httpPlugClientResolver; public function __construct( - ?MessageFactoryResolverInterface $messageFactoryResolver, - ?PsrClientResolverInterface $psrClientResolver, - ?HttpPlugClientResolverInterface $httpPlugClientResolver + ?MessageFactoryResolverInterface $messageFactoryResolver = null, + ?PsrClientResolverInterface $psrClientResolver = null, + ?HttpPlugClientResolverInterface $httpPlugClientResolver = null ) { $this->messageFactoryResolver = $messageFactoryResolver ?? MessageFactoryResolver::create(); $this->psrClientResolver = $psrClientResolver ?? PsrClientResolver::create(); @@ -35,9 +35,9 @@ public function __construct( } public static function create( - ?MessageFactoryResolverInterface $messageFactoryResolver, - ?PsrClientResolverInterface $psrClientResolver, - ?HttpPlugClientResolverInterface $httpPlugClientResolver + ?MessageFactoryResolverInterface $messageFactoryResolver = null, + ?PsrClientResolverInterface $psrClientResolver = null, + ?HttpPlugClientResolverInterface $httpPlugClientResolver = null ): self { return new self($messageFactoryResolver, $psrClientResolver, $httpPlugClientResolver); }