Skip to content

Commit

Permalink
Merge pull request #228 from php-http/fix-tests
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
dbu committed May 17, 2023
2 parents 8805097 + 1467264 commit 676f98f
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 95 deletions.
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
-
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

0 comments on commit 676f98f

Please sign in to comment.