-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathHttpMethodsClientTest.php
More file actions
95 lines (78 loc) · 2.74 KB
/
HttpMethodsClientTest.php
File metadata and controls
95 lines (78 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Tests\Http\Client\Common;
use Http\Client\Common\HttpMethodsClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
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';
private ClientInterface|MockObject $httpClient;
private HttpMethodsClient $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);
}
}