Skip to content

fix tests #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
},
"autoload-dev": {
"psr-4": {
"spec\\Http\\Client\\Common\\": "spec/"
"spec\\Http\\Client\\Common\\": "spec/",
"Tests\\Http\\Client\\Common\\": "tests/"
}
},
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is something with stubs one can do in phpstan but did not get it to work.

we will get rid of all this when we do a new major version anyways, so did not want to spend more time on it.

-
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
Expand Down
89 changes: 0 additions & 89 deletions spec/HttpMethodsClientSpec.php

This file was deleted.

100 changes: 100 additions & 0 deletions tests/HttpMethodsClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Tests\Http\Client\Common;

use Http\Client\Common\HttpMethodsClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

class HttpMethodsClientTest extends TestCase
{
private const URI = '/uri';
private const HEADER_NAME = 'Content-Type';
private const HEADER_VALUE = 'text/plain';
private const BODY = 'body';

/**
* @var ClientInterface
*/
private $httpClient;

/**
* @var HttpMethodsClient
*/
private $httpMethodsClient;

protected function setUp(): void
{
$this->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);
}
}
2 changes: 1 addition & 1 deletion tests/Plugin/AddPathPluginTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace tests\Http\Client\Common\Plugin;
namespace Tests\Http\Client\Common\Plugin;

use Http\Client\Common\Plugin;
use Http\Client\Common\Plugin\AddPathPlugin;
Expand Down
2 changes: 1 addition & 1 deletion tests/Plugin/RedirectPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Plugin;
namespace Tests\Http\Cient\Common\Plugin;

use Http\Client\Common\Exception\CircularRedirectionException;
use Http\Client\Common\Plugin\RedirectPlugin;
Expand Down
2 changes: 1 addition & 1 deletion tests/PluginChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace tests\Http\Client\Common;
namespace Tests\Http\Client\Common;

use Http\Client\Common\Exception\LoopException;
use Http\Client\Common\Plugin;
Expand Down
2 changes: 1 addition & 1 deletion tests/PluginClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace tests\Http\Client\Common;
namespace Tests\Http\Client\Common;

use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
Expand Down
2 changes: 1 addition & 1 deletion tests/PluginClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace tests\Http\Client\Common;
namespace Tests\Http\Client\Common;

use Http\Client\Common\Plugin;
use Http\Client\Common\Plugin\HeaderAppendPlugin;
Expand Down