Skip to content

Commit 4452856

Browse files
committed
Add tests for Psr18Client::request()
1 parent f476099 commit 4452856

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Redmine\Tests\Unit\Client\Psr18ClientTest;
4+
5+
use Exception;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Http\Client\ClientExceptionInterface;
8+
use Psr\Http\Client\ClientInterface;
9+
use Psr\Http\Message\RequestFactoryInterface;
10+
use Psr\Http\Message\RequestInterface;
11+
use Psr\Http\Message\ResponseInterface;
12+
use Psr\Http\Message\StreamFactoryInterface;
13+
use Psr\Http\Message\StreamInterface;
14+
use Redmine\Client\Psr18Client;
15+
use Redmine\Exception\ClientException;
16+
use Redmine\Http\Request;
17+
use Redmine\Http\Response;
18+
19+
/**
20+
* @covers \Redmine\Client\Psr18Client::request
21+
* @covers \Redmine\Client\Psr18Client::runRequest
22+
*/
23+
class RequestTest extends TestCase
24+
{
25+
/**
26+
* @dataProvider getRequestReponseData
27+
*/
28+
public function testRequestReturnsCorrectResponse($method, $data, $statusCode, $contentType, $content)
29+
{
30+
$httpClient = $this->createConfiguredMock(ClientInterface::class, [
31+
'sendRequest' => $this->createConfiguredMock(ResponseInterface::class, [
32+
'getStatusCode' => $statusCode,
33+
'getHeaderLine' => $contentType,
34+
'getBody' => $this->createConfiguredMock(StreamInterface::class, [
35+
'__toString' => $content,
36+
]),
37+
])
38+
]);
39+
40+
$requestFactory = $this->createConfiguredMock(RequestFactoryInterface::class, [
41+
'createRequest' => (function() {
42+
$request = $this->createMock(RequestInterface::class);
43+
$request->method('withHeader')->willReturn($request);
44+
$request->method('withBody')->willReturn($request);
45+
46+
return $request;
47+
})(),
48+
]);
49+
50+
$client = new Psr18Client(
51+
$httpClient,
52+
$requestFactory,
53+
$this->createMock(StreamFactoryInterface::class),
54+
'http://test.local',
55+
'access_token'
56+
);
57+
58+
$request = $this->createConfiguredMock(Request::class, [
59+
'getMethod' => $method,
60+
'getPath' => '/path',
61+
'getContentType' => $contentType,
62+
'getContent' => $data,
63+
]);
64+
65+
$response = $client->request($request);
66+
67+
$this->assertInstanceOf(Response::class, $response);
68+
$this->assertSame($statusCode, $response->getStatusCode());
69+
$this->assertSame($contentType, $response->getContentType());
70+
$this->assertSame($content, $response->getContent());
71+
}
72+
73+
public static function getRequestReponseData(): array
74+
{
75+
return [
76+
['GET', '', 101, 'text/plain', ''],
77+
['GET', '', 200, 'application/json', '{"foo_bar": 12345}'],
78+
['GET', '', 301, 'application/json', ''],
79+
['GET', '', 404, 'application/json', '{"title": "404 Not Found"}'],
80+
['GET', '', 500, 'text/plain', 'Internal Server Error'],
81+
['POST', '{"foo":"bar"}', 101, 'text/plain', ''],
82+
['POST', '{"foo":"bar"}', 200, 'application/json', '{"foo_bar": 12345}'],
83+
['POST', '{"foo":"bar"}', 301, 'application/json', ''],
84+
['POST', '{"foo":"bar"}', 404, 'application/json', '{"title": "404 Not Found"}'],
85+
['POST', '{"foo":"bar"}', 500, 'text/plain', 'Internal Server Error'],
86+
['PUT', '{"foo":"bar"}', 101, 'text/plain', ''],
87+
['PUT', '{"foo":"bar"}', 200, 'application/json', '{"foo_bar": 12345}'],
88+
['PUT', '{"foo":"bar"}', 301, 'application/json', ''],
89+
['PUT', '{"foo":"bar"}', 404, 'application/json', '{"title": "404 Not Found"}'],
90+
['PUT', '{"foo":"bar"}', 500, 'text/plain', 'Internal Server Error'],
91+
['DELETE', '', 101, 'text/plain', ''],
92+
['DELETE', '', 200, 'application/json', '{"foo_bar": 12345}'],
93+
['DELETE', '', 301, 'application/json', ''],
94+
['DELETE', '', 404, 'application/json', '{"title": "404 Not Found"}'],
95+
['DELETE', '', 500, 'text/plain', 'Internal Server Error'],
96+
];
97+
}
98+
99+
public function testRequestThrowsClientException()
100+
{
101+
$httpClient = $this->createMock(ClientInterface::class);
102+
$httpClient->expects($this->exactly(1))->method('sendRequest')->willThrowException(
103+
new class('error message') extends Exception implements ClientExceptionInterface {}
104+
);
105+
106+
$requestFactory = $this->createConfiguredMock(RequestFactoryInterface::class, [
107+
'createRequest' => (function() {
108+
$request = $this->createMock(RequestInterface::class);
109+
$request->method('withHeader')->willReturn($request);
110+
$request->method('withBody')->willReturn($request);
111+
112+
return $request;
113+
})(),
114+
]);
115+
116+
$client = new Psr18Client(
117+
$httpClient,
118+
$requestFactory,
119+
$this->createMock(StreamFactoryInterface::class),
120+
'http://test.local',
121+
'access_token'
122+
);
123+
124+
$request = $this->createConfiguredMock(Request::class, [
125+
'getMethod' => 'GET',
126+
'getPath' => '/path',
127+
'getContentType' => 'application/json',
128+
'getContent' => '',
129+
]);
130+
131+
$this->expectException(ClientException::class);
132+
$this->expectExceptionMessage('error message');
133+
134+
$client->request($request);
135+
}
136+
}

0 commit comments

Comments
 (0)