1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bunq\Tests\Service; |
4
|
|
|
|
5
|
|
|
use Bunq\Certificate\CertificateType; |
6
|
|
|
use Bunq\Certificate\DefaultCertificate; |
7
|
|
|
use Bunq\Certificate\Storage\CertificateStorage; |
8
|
|
|
use Bunq\Service\DefaultInstallationService; |
9
|
|
|
use Bunq\Token\DefaultToken; |
10
|
|
|
use GuzzleHttp\ClientInterface; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
final class InstallationServiceTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ClientInterface|ObjectProphecy |
19
|
|
|
*/ |
20
|
|
|
private $httpClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var CertificateStorage|ObjectProphecy |
24
|
|
|
*/ |
25
|
|
|
private $certificateStorage; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var DefaultInstallationService |
29
|
|
|
*/ |
30
|
|
|
private $service; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class); |
35
|
|
|
$this->certificateStorage = $this->prophesize(CertificateStorage::class); |
36
|
|
|
|
37
|
|
|
$this->service = new DefaultInstallationService( |
38
|
|
|
$this->httpClient->reveal(), |
39
|
|
|
$this->certificateStorage->reveal(), |
40
|
|
|
'apiKey', |
41
|
|
|
[ |
42
|
|
|
'ip1', |
43
|
|
|
'ip2', |
44
|
|
|
] |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
*/ |
51
|
|
|
public function itCallsTheInstallationCallWithPublicKey() |
52
|
|
|
{ |
53
|
|
|
$publicKey = DefaultCertificate::fromString('-----BEGIN PUBLIC KEY-----KEY-----END PUBLIC KEY-----'); |
54
|
|
|
|
55
|
|
|
$this->certificateStorage->load(CertificateType::PUBLIC_KEY())->willReturn($publicKey); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$postOptions = [ |
58
|
|
|
'json' => [ |
59
|
|
|
'client_public_key' => $publicKey->toString(), |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$expectedToken = 'installation-token'; |
64
|
|
|
$expectedKey = '-----BEGIN PUBLIC KEY-----NEW-KEY-----END PUBLIC KEY-----'; |
65
|
|
|
|
66
|
|
|
/** @var ResponseInterface|ObjectProphecy $response */ |
67
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
68
|
|
|
$response->getBody()->willReturn( |
|
|
|
|
69
|
|
|
\json_encode( |
70
|
|
|
[ |
71
|
|
|
'Response' => [ |
72
|
|
|
[ |
73
|
|
|
'Id' => [ |
74
|
|
|
'id' => 1, |
75
|
|
|
], |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
'Token' => [ |
79
|
|
|
'token' => $expectedToken, |
80
|
|
|
], |
81
|
|
|
], |
82
|
|
|
[ |
83
|
|
|
'ServerPublicKey' => [ |
84
|
|
|
'server_public_key' => $expectedKey, |
85
|
|
|
], |
86
|
|
|
], |
87
|
|
|
], |
88
|
|
|
] |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->httpClient->request('POST', '/v1/installation', $postOptions)->willReturn( |
|
|
|
|
93
|
|
|
$response->reveal() |
|
|
|
|
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$result = $this->service->install(); |
97
|
|
|
|
98
|
|
|
$this->assertEquals( |
99
|
|
|
[ |
100
|
|
|
'token' => $expectedToken, |
101
|
|
|
'public_key' => $expectedKey, |
102
|
|
|
], |
103
|
|
|
$result |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
*/ |
110
|
|
|
public function itRegistersADevice() |
111
|
|
|
{ |
112
|
|
|
$installationToken = new DefaultToken('installation-token'); |
113
|
|
|
|
114
|
|
|
$postOptions = [ |
115
|
|
|
'headers' => [ |
116
|
|
|
'X-Bunq-Client-Authentication' => 'installation-token', |
117
|
|
|
], |
118
|
|
|
'json' => [ |
119
|
|
|
'description' => 'Bunq PHP API Client', |
120
|
|
|
'secret' => 'apiKey', |
121
|
|
|
'permitted_ips' => ['ip1', 'ip2'], |
122
|
|
|
], |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
$this->httpClient->request('POST', '/v1/device-server', $postOptions)->shouldBeCalled(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
$this->service->registerDevice($installationToken); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
*/ |
133
|
|
|
public function itCreatesASession() |
134
|
|
|
{ |
135
|
|
|
$installationToken = new DefaultToken('installation-token'); |
136
|
|
|
|
137
|
|
|
$postOptions = [ |
138
|
|
|
'headers' => [ |
139
|
|
|
'X-Bunq-Client-Authentication' => 'installation-token', |
140
|
|
|
], |
141
|
|
|
'json' => [ |
142
|
|
|
'secret' => 'apiKey', |
143
|
|
|
] |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
$expectedToken = 'session-token'; |
147
|
|
|
|
148
|
|
|
/** @var ResponseInterface|ObjectProphecy $response */ |
149
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
150
|
|
|
$response->getBody()->willReturn( |
|
|
|
|
151
|
|
|
\json_encode( |
152
|
|
|
[ |
153
|
|
|
'Response' => [ |
154
|
|
|
[ |
155
|
|
|
'Id' => [ |
156
|
|
|
'id' => 1, |
157
|
|
|
], |
158
|
|
|
], |
159
|
|
|
[ |
160
|
|
|
'Token' => [ |
161
|
|
|
'token' => $expectedToken, |
162
|
|
|
], |
163
|
|
|
] |
164
|
|
|
], |
165
|
|
|
] |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$this->httpClient->request('POST', '/v1/session-server', $postOptions)->willReturn( |
|
|
|
|
170
|
|
|
$response->reveal() |
|
|
|
|
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$result = $this->service->createSession($installationToken); |
174
|
|
|
|
175
|
|
|
$this->assertEquals($expectedToken, $result); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: