1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\LayerContracts\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use EasyHttp\LayerContracts\Tests\Unit\Example\SomeClient; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class AbstractClientTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @test |
12
|
|
|
*/ |
13
|
|
|
public function itExecutesARequest() |
14
|
|
|
{ |
15
|
|
|
$client = new SomeClient(); |
16
|
|
|
|
17
|
|
|
$response = $client->call('GET', 'http://example.com/api'); |
18
|
|
|
|
19
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
20
|
|
|
$this->assertSame(['key' => 'value'], $response->toJson()); |
21
|
|
|
$this->assertSame(['Server' => 'Apache/2.4.38 (Debian)'], $response->getHeaders()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @test |
26
|
|
|
*/ |
27
|
|
|
public function itPreparesARequestForExecution() |
28
|
|
|
{ |
29
|
|
|
$client = new SomeClient(); |
30
|
|
|
$client->prepareRequest('GET', 'http://example.com/api'); |
31
|
|
|
|
32
|
|
|
$this->assertSame('GET', $client->getRequest()->getMethod()); |
33
|
|
|
$this->assertSame('http://example.com/api', $client->getRequest()->getUri()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @test |
38
|
|
|
*/ |
39
|
|
|
public function itExecutesAPreparedRequest() |
40
|
|
|
{ |
41
|
|
|
$client = new SomeClient(); |
42
|
|
|
$client->prepareRequest('GET', 'http://example.com/api'); |
43
|
|
|
|
44
|
|
|
$response = $client->execute(); |
45
|
|
|
|
46
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
47
|
|
|
$this->assertSame(['key' => 'value'], $response->toJson()); |
48
|
|
|
$this->assertSame(['Server' => 'Apache/2.4.38 (Debian)'], $response->getHeaders()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function itReuseTheAdapterForEachRequest() |
55
|
|
|
{ |
56
|
|
|
$client = new SomeClient(); |
57
|
|
|
|
58
|
|
|
$client->call('GET', 'http://example.com/api'); |
59
|
|
|
$client->call('GET', 'http://example.com/api'); |
60
|
|
|
$client->call('GET', 'http://example.com/api'); |
61
|
|
|
|
62
|
|
|
$this->assertSame(1, $client->getAdapterCounter()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function itSetsHandlers() |
69
|
|
|
{ |
70
|
|
|
$client = new SomeClient(); |
71
|
|
|
|
72
|
|
|
$client->withHandler( |
73
|
|
|
function () { |
74
|
|
|
return [ |
75
|
|
|
'status' => 500, |
76
|
|
|
'headers' => ['Server' => 'Apache/2.4.38 (Ubuntu)'], |
77
|
|
|
'body' => '{"message":"Server Error"}', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$response = $client->call('GET', 'some uri'); |
83
|
|
|
|
84
|
|
|
$this->assertSame(500, $response->getStatusCode()); |
85
|
|
|
$this->assertSame(['message' => 'Server Error'], $response->toJson()); |
86
|
|
|
$this->assertSame(['Server' => 'Apache/2.4.38 (Ubuntu)'], $response->getHeaders()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function itFlushTheAdapterAfterSetsHandler() |
93
|
|
|
{ |
94
|
|
|
$client = new SomeClient(); |
95
|
|
|
|
96
|
|
|
$liveResponse = $client->call('GET', 'http://example.com/api'); |
97
|
|
|
|
98
|
|
|
$client->withHandler( |
99
|
|
|
function () { |
100
|
|
|
return [ |
101
|
|
|
'status' => 500, |
102
|
|
|
'headers' => ['Server' => 'Apache/2.4 (Ubuntu)'], |
103
|
|
|
'body' => 'Server Error', |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$mockedResponse = $client->call('GET', 'some uri'); |
109
|
|
|
|
110
|
|
|
$this->assertSame(200, $liveResponse->getStatusCode()); |
111
|
|
|
$this->assertSame(['key' => 'value'], $liveResponse->toJson()); |
112
|
|
|
$this->assertSame(['Server' => 'Apache/2.4.38 (Debian)'], $liveResponse->getHeaders()); |
113
|
|
|
$this->assertSame(500, $mockedResponse->getStatusCode()); |
114
|
|
|
$this->assertSame(['Server' => 'Apache/2.4 (Ubuntu)'], $mockedResponse->getHeaders()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|