1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Postpay\Tests\HttpClients; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Postpay\Exceptions\PostpayException; |
7
|
|
|
use Postpay\Http\Request; |
8
|
|
|
use Postpay\HttpClients\Curl; |
9
|
|
|
use Postpay\HttpClients\CurlClient; |
10
|
|
|
|
11
|
|
|
class CurlClientTest extends TestCase |
12
|
|
|
{ |
13
|
|
View Code Duplication |
public function testSend() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$curl = $this->createMock(Curl::class); |
16
|
|
|
$curl->method('getInfo')->willReturn(200); |
17
|
|
|
$client = new CurlClient($curl); |
18
|
|
|
$response = $client->send(new Request('GET')); |
19
|
|
|
|
20
|
|
|
self::assertSame(200, $response->getStatusCode()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testSendRequest() |
24
|
|
|
{ |
25
|
|
|
$request = $this->createMock(Request::class); |
26
|
|
|
$url = 'https://postman-echo.com/basic-auth'; |
27
|
|
|
$request->method('getUrl')->willReturn($url); |
28
|
|
|
$request->method('getMethod')->willReturn('GET'); |
29
|
|
|
$request->method('getHeaders')->willReturn([]); |
30
|
|
|
$request->method('getAuth')->willReturn(['postman', 'password']); |
31
|
|
|
|
32
|
|
|
$client = new CurlClient(); |
33
|
|
|
$response = $client->send($request); |
34
|
|
|
|
35
|
|
|
self::assertSame(200, $response->getStatusCode()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testSendError() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$curl = $this->createMock(Curl::class); |
41
|
|
|
$curl->method('errno')->willReturn(1); |
42
|
|
|
$client = new CurlClient($curl); |
43
|
|
|
|
44
|
|
|
$this->expectException(PostpayException::class); |
45
|
|
|
$client->send(new Request('POST')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testGetRequestHeaders() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$curl = new CurlClient(); |
51
|
|
|
$request = new Request('GET'); |
52
|
|
|
$request->setHeaders(['test' => true]); |
53
|
|
|
$headers = $curl->getRequestHeaders($request); |
54
|
|
|
|
55
|
|
|
self::assertSame('test: 1', $headers[0]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.