1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bunq\Tests; |
4
|
|
|
|
5
|
|
|
use Bunq\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
10
|
|
|
|
11
|
|
|
final class ClientTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ClientInterface|ObjectProphecy |
15
|
|
|
*/ |
16
|
|
|
private $httpClient; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class); |
26
|
|
|
$this->client = new Client($this->httpClient->reveal()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
public function itExecutesAGetRequest() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
/** @var Response|ObjectProphecy $response */ |
35
|
|
|
$response = $this->prophesize(Response::class); |
36
|
|
|
$response->getBody()->willReturn( |
|
|
|
|
37
|
|
|
json_encode([ |
38
|
|
|
'Response' => [ |
39
|
|
|
'status' => 'OK' |
40
|
|
|
] |
41
|
|
|
]) |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$this->httpClient->request('GET', 'uri', [])->willReturn($response->reveal()); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$result = $this->client->get('uri'); |
47
|
|
|
|
48
|
|
|
$expectedResult = ['Response' => ['status' => 'OK']]; |
49
|
|
|
|
50
|
|
|
$this->assertEquals($expectedResult, $result); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function itExecutesAPostRequest() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
/** @var Response|ObjectProphecy $response */ |
59
|
|
|
$response = $this->prophesize(Response::class); |
60
|
|
|
$response->getBody()->willReturn( |
|
|
|
|
61
|
|
|
json_encode([ |
62
|
|
|
'Response' => [ |
63
|
|
|
'status' => 'OK' |
64
|
|
|
] |
65
|
|
|
]) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->httpClient->request('POST', 'uri', [])->willReturn($response->reveal()); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$result = $this->client->post('uri'); |
71
|
|
|
|
72
|
|
|
$expectedResult = ['Response' => ['status' => 'OK']]; |
73
|
|
|
|
74
|
|
|
$this->assertEquals($expectedResult, $result); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @test |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function itExecutesAPutRequest() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
/** @var Response|ObjectProphecy $response */ |
83
|
|
|
$response = $this->prophesize(Response::class); |
84
|
|
|
$response->getBody()->willReturn( |
|
|
|
|
85
|
|
|
json_encode([ |
86
|
|
|
'Response' => [ |
87
|
|
|
'status' => 'OK' |
88
|
|
|
] |
89
|
|
|
]) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->httpClient->request('PUT', 'uri', [])->willReturn($response->reveal()); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$result = $this->client->put('uri'); |
95
|
|
|
|
96
|
|
|
$expectedResult = ['Response' => ['status' => 'OK']]; |
97
|
|
|
|
98
|
|
|
$this->assertEquals($expectedResult, $result); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
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.