Skip to content

Commit 9404453

Browse files
committed
Create test for NativeCurlClient::request()
1 parent 4452856 commit 9404453

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Redmine\Tests\Unit\Client\NativeCurlClientTest;
4+
5+
use phpmock\phpunit\PHPMock;
6+
use PHPUnit\Framework\TestCase;
7+
use Redmine\Client\NativeCurlClient;
8+
use Redmine\Http\Request;
9+
use Redmine\Http\Response;
10+
use stdClass;
11+
12+
/**
13+
* @covers \Redmine\Client\NativeCurlClient::request
14+
* @covers \Redmine\Client\NativeCurlClient::runRequest
15+
*/
16+
class RequestTest extends TestCase
17+
{
18+
use PHPMock;
19+
20+
/**
21+
* @dataProvider getRequestReponseData
22+
*/
23+
public function testRequestReturnsCorrectResponse($method, $data, $statusCode, $contentType, $content)
24+
{
25+
$namespace = 'Redmine\Client';
26+
27+
$curl = $this->createMock(stdClass::class);
28+
29+
$curlInit = $this->getFunctionMock($namespace, 'curl_init');
30+
$curlInit->expects($this->exactly(1))->willReturn($curl);
31+
32+
$curlExec = $this->getFunctionMock($namespace, 'curl_exec');
33+
$curlExec->expects($this->exactly(1))->willReturn($content);
34+
35+
$curlSetoptArray = $this->getFunctionMock($namespace, 'curl_setopt_array');
36+
37+
$curlGetinfo = $this->getFunctionMock($namespace, 'curl_getinfo');
38+
$curlGetinfo->expects($this->exactly(2))->will($this->returnValueMap(([
39+
[$curl, CURLINFO_HTTP_CODE, $statusCode],
40+
[$curl, CURLINFO_CONTENT_TYPE, $contentType],
41+
])));
42+
43+
$curlErrno = $this->getFunctionMock($namespace, 'curl_errno');
44+
$curlErrno->expects($this->exactly(1))->willReturn(CURLE_OK);
45+
46+
$curlClose = $this->getFunctionMock($namespace, 'curl_close');
47+
48+
$client = new NativeCurlClient(
49+
'http://test.local',
50+
'access_token'
51+
);
52+
53+
$request = $this->createConfiguredMock(Request::class, [
54+
'getMethod' => $method,
55+
'getPath' => '/path',
56+
'getContentType' => $contentType,
57+
'getContent' => $data,
58+
]);
59+
60+
$response = $client->request($request);
61+
62+
$this->assertInstanceOf(Response::class, $response);
63+
$this->assertSame($statusCode, $response->getStatusCode());
64+
$this->assertSame($contentType, $response->getContentType());
65+
$this->assertSame($content, $response->getContent());
66+
}
67+
68+
public static function getRequestReponseData(): array
69+
{
70+
return [
71+
['GET', '', 101, 'text/plain', ''],
72+
['GET', '', 200, 'application/json', '{"foo_bar": 12345}'],
73+
['GET', '', 301, 'application/json', ''],
74+
['GET', '', 404, 'application/json', '{"title": "404 Not Found"}'],
75+
['GET', '', 500, 'text/plain', 'Internal Server Error'],
76+
['POST', '{"foo":"bar"}', 101, 'text/plain', ''],
77+
['POST', '{"foo":"bar"}', 200, 'application/json', '{"foo_bar": 12345}'],
78+
['POST', '{"foo":"bar"}', 301, 'application/json', ''],
79+
['POST', '{"foo":"bar"}', 404, 'application/json', '{"title": "404 Not Found"}'],
80+
['POST', '{"foo":"bar"}', 500, 'text/plain', 'Internal Server Error'],
81+
['PUT', '{"foo":"bar"}', 101, 'text/plain', ''],
82+
['PUT', '{"foo":"bar"}', 200, 'application/json', '{"foo_bar": 12345}'],
83+
['PUT', '{"foo":"bar"}', 301, 'application/json', ''],
84+
['PUT', '{"foo":"bar"}', 404, 'application/json', '{"title": "404 Not Found"}'],
85+
['PUT', '{"foo":"bar"}', 500, 'text/plain', 'Internal Server Error'],
86+
['DELETE', '', 101, 'text/plain', ''],
87+
['DELETE', '', 200, 'application/json', '{"foo_bar": 12345}'],
88+
['DELETE', '', 301, 'application/json', ''],
89+
['DELETE', '', 404, 'application/json', '{"title": "404 Not Found"}'],
90+
['DELETE', '', 500, 'text/plain', 'Internal Server Error'],
91+
];
92+
}
93+
}

0 commit comments

Comments
 (0)