Skip to content

Commit 676f98f

Browse files
authored
Merge pull request #228 from php-http/fix-tests
fix tests
2 parents 8805097 + 1467264 commit 676f98f

9 files changed

+128
-95
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
},
4343
"autoload-dev": {
4444
"psr-4": {
45-
"spec\\Http\\Client\\Common\\": "spec/"
45+
"spec\\Http\\Client\\Common\\": "spec/",
46+
"Tests\\Http\\Client\\Common\\": "tests/"
4647
}
4748
},
4849
"scripts": {

phpstan.neon.dist

+21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ parameters:
3131
count: 1
3232
path: src/EmulatedHttpClient.php
3333

34+
# we still support the obsolete RequestFactory for BC but do not require the package anymore
35+
-
36+
message: "#^Call to method createRequest\\(\\) on an unknown class Http\\\\Message\\\\RequestFactory\\.$#"
37+
count: 1
38+
path: src/HttpMethodsClient.php
39+
40+
-
41+
message: "#^Class Http\\\\Message\\\\RequestFactory not found\\.$#"
42+
count: 4
43+
path: src/HttpMethodsClient.php
44+
45+
-
46+
message: "#^Parameter \\$requestFactory of method Http\\\\Client\\\\Common\\\\HttpMethodsClient\\:\\:__construct\\(\\) has invalid type Http\\\\Message\\\\RequestFactory\\.$#"
47+
count: 1
48+
path: src/HttpMethodsClient.php
49+
50+
-
51+
message: "#^Property Http\\\\Client\\\\Common\\\\HttpMethodsClient\\:\\:\\$requestFactory has unknown class Http\\\\Message\\\\RequestFactory as its type\\.$#"
52+
count: 1
53+
path: src/HttpMethodsClient.php
54+
3455
-
3556
message: "#^Anonymous function should return Psr\\\\Http\\\\Message\\\\ResponseInterface but returns mixed\\.$#"
3657
count: 1

spec/HttpMethodsClientSpec.php

-89
This file was deleted.

tests/HttpMethodsClientTest.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Tests\Http\Client\Common;
4+
5+
use Http\Client\Common\HttpMethodsClient;
6+
use Nyholm\Psr7\Factory\Psr17Factory;
7+
use Nyholm\Psr7\Response;
8+
use PHPUnit\Framework\TestCase;
9+
use Psr\Http\Client\ClientInterface;
10+
use Psr\Http\Message\RequestInterface;
11+
12+
class HttpMethodsClientTest extends TestCase
13+
{
14+
private const URI = '/uri';
15+
private const HEADER_NAME = 'Content-Type';
16+
private const HEADER_VALUE = 'text/plain';
17+
private const BODY = 'body';
18+
19+
/**
20+
* @var ClientInterface
21+
*/
22+
private $httpClient;
23+
24+
/**
25+
* @var HttpMethodsClient
26+
*/
27+
private $httpMethodsClient;
28+
29+
protected function setUp(): void
30+
{
31+
$this->httpClient = $this->createMock(ClientInterface::class);
32+
$streamFactory = $requestFactory = new Psr17Factory();
33+
$this->httpMethodsClient = new HttpMethodsClient($this->httpClient, $requestFactory, $streamFactory);
34+
}
35+
36+
public function testGet(): void
37+
{
38+
$this->expectSendRequest('get');
39+
}
40+
41+
public function testHead(): void
42+
{
43+
$this->expectSendRequest('head');
44+
}
45+
46+
public function testTrace(): void
47+
{
48+
$this->expectSendRequest('trace');
49+
}
50+
51+
public function testPost(): void
52+
{
53+
$this->expectSendRequest('post', self::BODY);
54+
}
55+
56+
public function testPut(): void
57+
{
58+
$this->expectSendRequest('put', self::BODY);
59+
}
60+
61+
public function testPatch(): void
62+
{
63+
$this->expectSendRequest('patch', self::BODY);
64+
}
65+
66+
public function testDelete(): void
67+
{
68+
$this->expectSendRequest('delete', self::BODY);
69+
}
70+
71+
public function testOptions(): void
72+
{
73+
$this->expectSendRequest('options', self::BODY);
74+
}
75+
76+
/**
77+
* Run the actual test.
78+
*
79+
* As there is no data provider in phpspec, we keep separate methods to get new mocks for each test.
80+
*/
81+
private function expectSendRequest(string $method, string $body = null): void
82+
{
83+
$response = new Response();
84+
$this->httpClient->expects($this->once())
85+
->method('sendRequest')
86+
->with(self::callback(static function (RequestInterface $request) use ($body, $method): bool {
87+
self::assertSame(strtoupper($method), $request->getMethod());
88+
self::assertSame(self::URI, (string) $request->getUri());
89+
self::assertSame([self::HEADER_NAME => [self::HEADER_VALUE]], $request->getHeaders());
90+
self::assertSame((string) $body, (string) $request->getBody());
91+
92+
return true;
93+
}))
94+
->willReturn($response)
95+
;
96+
97+
$actualResponse = $this->httpMethodsClient->$method(self::URI, [self::HEADER_NAME => self::HEADER_VALUE], self::BODY);
98+
$this->assertSame($response, $actualResponse);
99+
}
100+
}

tests/Plugin/AddPathPluginTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace tests\Http\Client\Common\Plugin;
3+
namespace Tests\Http\Client\Common\Plugin;
44

55
use Http\Client\Common\Plugin;
66
use Http\Client\Common\Plugin\AddPathPlugin;

tests/Plugin/RedirectPluginTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Plugin;
5+
namespace Tests\Http\Cient\Common\Plugin;
66

77
use Http\Client\Common\Exception\CircularRedirectionException;
88
use Http\Client\Common\Plugin\RedirectPlugin;

tests/PluginChainTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace tests\Http\Client\Common;
5+
namespace Tests\Http\Client\Common;
66

77
use Http\Client\Common\Exception\LoopException;
88
use Http\Client\Common\Plugin;

tests/PluginClientBuilderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace tests\Http\Client\Common;
5+
namespace Tests\Http\Client\Common;
66

77
use Http\Client\Common\Plugin;
88
use Http\Client\Common\PluginClient;

tests/PluginClientTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace tests\Http\Client\Common;
5+
namespace Tests\Http\Client\Common;
66

77
use Http\Client\Common\Plugin;
88
use Http\Client\Common\Plugin\HeaderAppendPlugin;

0 commit comments

Comments
 (0)