1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeverTest\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use Lever\Api\Client; |
7
|
|
|
use PHPUnit_Framework_TestCase; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* LeverTest\Api\ClientTest |
12
|
|
|
*/ |
13
|
|
|
class ClientTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var Client */ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** @var HttpClient | \Prophecy\Prophecy\ObjectProphecy */ |
19
|
|
|
protected $mockClient; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->client = new Client( |
24
|
|
|
[ |
25
|
|
|
'authToken' => 'myToken', |
26
|
|
|
'httpClient' => $this->mockClient()->reveal(), |
27
|
|
|
] |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @test |
33
|
|
|
*/ |
34
|
|
|
public function hasDefaultEndpoint() |
35
|
|
|
{ |
36
|
|
|
$this->assertEquals('https://api.lever.co/v1', $this->client->getEndpoint()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
*/ |
42
|
|
|
public function canMutateEndpoint() |
43
|
|
|
{ |
44
|
|
|
$this->client = new Client(['endpoint' => 'https://test.lever.co/v2/']); |
45
|
|
|
$this->assertEquals('https://test.lever.co/v2', $this->client->getEndpoint()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function canMakeGetRequests() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$query = ['foo' => 'bar']; |
54
|
|
|
|
55
|
|
|
$this->mockClient()->request( |
56
|
|
|
'GET', |
57
|
|
|
$this->url('/foo'), |
58
|
|
|
[ |
59
|
|
|
'auth' => ['myToken', ''], |
60
|
|
|
'query' => $query, |
61
|
|
|
] |
62
|
|
|
)->willReturn($this->fooBarResponse()); |
63
|
|
|
|
64
|
|
|
$data = $this->client->get('/foo', $query); |
65
|
|
|
|
66
|
|
|
$this->assertFooBarResponse($data); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @test |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function canMakePostRequests() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$postData = ['foo' => 'bar']; |
75
|
|
|
|
76
|
|
|
$this->mockClient()->request( |
77
|
|
|
'POST', |
78
|
|
|
$this->url('/foo'), |
79
|
|
|
[ |
80
|
|
|
'auth' => ['myToken', ''], |
81
|
|
|
'form_params' => $postData, |
82
|
|
|
] |
83
|
|
|
)->willReturn($this->fooBarResponse()); |
84
|
|
|
|
85
|
|
|
$data = $this->client->post('/foo', $postData); |
86
|
|
|
|
87
|
|
|
$this->assertFooBarResponse($data); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function canMakePutRequest() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$putData = ['foo' => 'bar']; |
96
|
|
|
|
97
|
|
|
$this->mockClient()->request( |
98
|
|
|
'PUT', |
99
|
|
|
$this->url('/foo'), |
100
|
|
|
[ |
101
|
|
|
'auth' => ['myToken', ''], |
102
|
|
|
'json' => $putData, |
103
|
|
|
] |
104
|
|
|
)->willReturn($this->fooBarResponse()); |
105
|
|
|
|
106
|
|
|
$data = $this->client->put('/foo', $putData); |
107
|
|
|
|
108
|
|
|
$this->assertFooBarResponse($data); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return \Prophecy\Prophecy\ObjectProphecy |
113
|
|
|
*/ |
114
|
|
|
private function mockClient() |
115
|
|
|
{ |
116
|
|
|
if ($this->mockClient === null) { |
117
|
|
|
$this->mockClient = $this->prophesize(HttpClient::class); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->mockClient; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $path |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
private function url($path = '/') |
129
|
|
|
{ |
130
|
|
|
return 'https://api.lever.co/v1' . $path; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function fooBarResponse() |
134
|
|
|
{ |
135
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
136
|
|
|
$response->getBody()->willReturn('{"foo":"bar"}'); |
137
|
|
|
|
138
|
|
|
return $response->reveal(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param array $data |
143
|
|
|
*/ |
144
|
|
|
private function assertFooBarResponse($data) |
145
|
|
|
{ |
146
|
|
|
$this->assertEquals(['foo' => 'bar'], $data); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.