From e8e215529fbcaee73769985dca8d911144c75096 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 17 May 2023 08:54:28 +0200 Subject: [PATCH 1/2] fix tests --- composer.json | 3 +- spec/HttpMethodsClientSpec.php | 89 ---------------------------- tests/HttpMethodsClientTest.php | 100 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 90 deletions(-) delete mode 100644 spec/HttpMethodsClientSpec.php create mode 100644 tests/HttpMethodsClientTest.php diff --git a/composer.json b/composer.json index cc5cda8..a15838e 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ }, "autoload-dev": { "psr-4": { - "spec\\Http\\Client\\Common\\": "spec/" + "spec\\Http\\Client\\Common\\": "spec/", + "Tests\\Http\\Client\\Common\\": "tests/" } }, "scripts": { diff --git a/spec/HttpMethodsClientSpec.php b/spec/HttpMethodsClientSpec.php deleted file mode 100644 index 68e124d..0000000 --- a/spec/HttpMethodsClientSpec.php +++ /dev/null @@ -1,89 +0,0 @@ - '/uri', - 'headers' => [ - 'Content-Type' => 'text/plain', - ], - 'body' => 'body', - ]; - - public function let(HttpClient $client, RequestFactory $requestFactory) - { - $this->beAnInstanceOf( - HttpMethodsClient::class, [ - $client, - $requestFactory, - ] - ); - } - - public function it_sends_a_get_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'get'); - } - - public function it_sends_a_head_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'head'); - } - - public function it_sends_a_trace_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'trace'); - } - - public function it_sends_a_post_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'post', self::$requestData['body']); - } - - public function it_sends_a_put_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'put', self::$requestData['body']); - } - - public function it_sends_a_patch_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'patch', self::$requestData['body']); - } - - public function it_sends_a_delete_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'delete', self::$requestData['body']); - } - - public function it_sends_an_options_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) - { - $this->assert($client, $requestFactory, $request, $response, 'options', self::$requestData['body']); - } - - /** - * Run the actual test. - * - * As there is no data provider in phpspec, we keep separate methods to get new mocks for each test. - */ - private function assert(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response, string $method, string $body = null) - { - $client->sendRequest($request)->shouldBeCalled()->willReturn($response); - $this->mockFactory($requestFactory, $request, strtoupper($method), $body); - - $this->$method(self::$requestData['uri'], self::$requestData['headers'], self::$requestData['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); - } - - private function mockFactory(RequestFactory $requestFactory, RequestInterface $request, string $method, string $body = null) - { - $requestFactory->createRequest($method, self::$requestData['uri'], self::$requestData['headers'], $body)->willReturn($request); - } -} diff --git a/tests/HttpMethodsClientTest.php b/tests/HttpMethodsClientTest.php new file mode 100644 index 0000000..4ec9f69 --- /dev/null +++ b/tests/HttpMethodsClientTest.php @@ -0,0 +1,100 @@ +httpClient = $this->createMock(ClientInterface::class); + $streamFactory = $requestFactory = new Psr17Factory(); + $this->httpMethodsClient = new HttpMethodsClient($this->httpClient, $requestFactory, $streamFactory); + } + + public function testGet(): void + { + $this->expectSendRequest('get'); + } + + public function testHead(): void + { + $this->expectSendRequest('head'); + } + + public function testTrace(): void + { + $this->expectSendRequest('trace'); + } + + public function testPost(): void + { + $this->expectSendRequest('post', self::BODY); + } + + public function testPut(): void + { + $this->expectSendRequest('put', self::BODY); + } + + public function testPatch(): void + { + $this->expectSendRequest('patch', self::BODY); + } + + public function testDelete(): void + { + $this->expectSendRequest('delete', self::BODY); + } + + public function testOptions(): void + { + $this->expectSendRequest('options', self::BODY); + } + + /** + * Run the actual test. + * + * As there is no data provider in phpspec, we keep separate methods to get new mocks for each test. + */ + private function expectSendRequest(string $method, string $body = null): void + { + $response = new Response(); + $this->httpClient->expects($this->once()) + ->method('sendRequest') + ->with(self::callback(static function (RequestInterface $request) use ($body, $method): bool { + self::assertSame(strtoupper($method), $request->getMethod()); + self::assertSame(self::URI, (string) $request->getUri()); + self::assertSame([self::HEADER_NAME => [self::HEADER_VALUE]], $request->getHeaders()); + self::assertSame((string) $body, (string) $request->getBody()); + + return true; + })) + ->willReturn($response) + ; + + $actualResponse = $this->httpMethodsClient->$method(self::URI, [self::HEADER_NAME => self::HEADER_VALUE], self::BODY); + $this->assertSame($response, $actualResponse); + } +} From 1467264599ae6dc47bd1cce0466bc7f57e79624e Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 17 May 2023 10:04:23 +0200 Subject: [PATCH 2/2] add factory stub for phpstan --- phpstan.neon.dist | 21 +++++++++++++++++++++ tests/Plugin/AddPathPluginTest.php | 2 +- tests/Plugin/RedirectPluginTest.php | 2 +- tests/PluginChainTest.php | 2 +- tests/PluginClientBuilderTest.php | 2 +- tests/PluginClientTest.php | 2 +- 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 236087f..8886dea 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -31,6 +31,27 @@ parameters: count: 1 path: src/EmulatedHttpClient.php + # we still support the obsolete RequestFactory for BC but do not require the package anymore + - + message: "#^Call to method createRequest\\(\\) on an unknown class Http\\\\Message\\\\RequestFactory\\.$#" + count: 1 + path: src/HttpMethodsClient.php + + - + message: "#^Class Http\\\\Message\\\\RequestFactory not found\\.$#" + count: 4 + path: src/HttpMethodsClient.php + + - + message: "#^Parameter \\$requestFactory of method Http\\\\Client\\\\Common\\\\HttpMethodsClient\\:\\:__construct\\(\\) has invalid type Http\\\\Message\\\\RequestFactory\\.$#" + count: 1 + path: src/HttpMethodsClient.php + + - + message: "#^Property Http\\\\Client\\\\Common\\\\HttpMethodsClient\\:\\:\\$requestFactory has unknown class Http\\\\Message\\\\RequestFactory as its type\\.$#" + count: 1 + path: src/HttpMethodsClient.php + - message: "#^Anonymous function should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#" count: 1 diff --git a/tests/Plugin/AddPathPluginTest.php b/tests/Plugin/AddPathPluginTest.php index 55081a8..9457535 100644 --- a/tests/Plugin/AddPathPluginTest.php +++ b/tests/Plugin/AddPathPluginTest.php @@ -1,6 +1,6 @@