1
|
|
|
<?php |
2
|
|
|
namespace Dokobit\Gateway\Tests; |
3
|
|
|
|
4
|
|
|
use Dokobit\Gateway\Client; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
7
|
|
|
|
8
|
|
|
class ClientTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** @var Dokobit\Gateway\Query\QueryInterface */ |
|
|
|
|
11
|
|
|
private $methodStub; |
12
|
|
|
|
13
|
|
|
/** @var Dokobit\Gateway\Http\ClientInterface */ |
|
|
|
|
14
|
|
|
private $clientStub; |
15
|
|
|
|
16
|
|
|
/** @var Dokobit\Gateway\ResponseMapperInterface */ |
|
|
|
|
17
|
|
|
private $responseMapperStub; |
18
|
|
|
|
19
|
|
|
/** @var Symfony\Component\Validator\Validator */ |
|
|
|
|
20
|
|
|
private $validatorStub; |
21
|
|
|
|
22
|
|
|
/** @var Client */ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
protected function setUp(): void |
26
|
|
|
{ |
27
|
|
|
$this->methodStub = $this->getMockBuilder('Dokobit\Gateway\Query\QueryInterface') |
|
|
|
|
28
|
|
|
->setMethods(['getAction', 'getMethod', 'getFields', 'createResult', 'getValidationConstraints']) |
29
|
|
|
->disableOriginalConstructor() |
30
|
|
|
->getMock(); |
31
|
|
|
$this->methodStub->method('getAction') |
32
|
|
|
->willReturn('login'); |
33
|
|
|
$this->methodStub->method('getMethod') |
34
|
|
|
->willReturn('post'); |
35
|
|
|
$this->methodStub->method('getFields') |
36
|
|
|
->willReturn(['phone' => '+3706xxxxxxx', 'code' => 'xxxxxxxxxxx']) |
37
|
|
|
; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
$this->clientStub = $this->getMockBuilder('Dokobit\Gateway\Http\ClientInterface') |
|
|
|
|
41
|
|
|
->setMethods(['requestJson', 'requestBody', 'sendRequest']) |
42
|
|
|
->disableOriginalConstructor() |
43
|
|
|
->getMock(); |
44
|
|
|
|
45
|
|
|
$this->responseMapperStub = $this->getMockBuilder('Dokobit\Gateway\ResponseMapperInterface') |
|
|
|
|
46
|
|
|
->setMethods(['map']) |
47
|
|
|
->disableOriginalConstructor() |
48
|
|
|
->getMock(); |
49
|
|
|
|
50
|
|
|
$this->validatorStub = $this->getMockBuilder('Symfony\Component\Validator\Validator\RecursiveValidator') |
|
|
|
|
51
|
|
|
->disableOriginalConstructor() |
52
|
|
|
// ->setMethods(['validateValue']) |
53
|
|
|
->getMock(); |
54
|
|
|
|
55
|
|
|
$this->client = new Client( |
56
|
|
|
$this->clientStub, |
57
|
|
|
$this->responseMapperStub, |
58
|
|
|
$this->validatorStub, |
59
|
|
|
['apiKey' => 'xxx', 'sandbox' => true] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testFactoryCreate() |
64
|
|
|
{ |
65
|
|
|
$client = Client::create(['sandbox' => true, 'apiKey' => 'xxx']); |
66
|
|
|
$this->assertInstanceOf('Dokobit\Gateway\Client', $client); |
67
|
|
|
$this->assertTrue($client->isSandbox()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testFactoryCreateWithLogger() |
71
|
|
|
{ |
72
|
|
|
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface') |
73
|
|
|
->getMock() |
74
|
|
|
; |
75
|
|
|
$client = Client::create( |
76
|
|
|
['sandbox' => true, 'apiKey' => 'xxx'], |
77
|
|
|
$logger |
78
|
|
|
); |
79
|
|
|
$this->assertInstanceOf('Dokobit\Gateway\Client', $client); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testDefaultClientConfiguration() |
83
|
|
|
{ |
84
|
|
|
$client = new Client( |
85
|
|
|
$this->clientStub, |
86
|
|
|
$this->responseMapperStub, |
87
|
|
|
$this->validatorStub, |
88
|
|
|
['apiKey' => 'xxx'] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$this->assertSame(false, $client->isSandbox()); |
92
|
|
|
$this->assertSame('https://gateway.dokobit.com', $client->getUrl()); |
93
|
|
|
$this->assertSame('https://gateway-sandbox.dokobit.com', $client->getSandboxUrl()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testCustomClientConfiguration() |
97
|
|
|
{ |
98
|
|
|
$client = new Client( |
99
|
|
|
$this->clientStub, |
100
|
|
|
$this->responseMapperStub, |
101
|
|
|
$this->validatorStub, |
102
|
|
|
[ |
103
|
|
|
'sandbox' => true, |
104
|
|
|
'apiKey' => 'l33t', |
105
|
|
|
'url' => 'https://custom-api.dokobit.com', |
106
|
|
|
'sandboxUrl' => 'https://custom-sandbox.dokobit.com', |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
$this->assertSame(true, $client->isSandbox()); |
110
|
|
|
$this->assertSame('l33t', $client->getApiKey()); |
111
|
|
|
$this->assertSame('https://custom-api.dokobit.com', $client->getUrl()); |
112
|
|
|
$this->assertSame('https://custom-sandbox.dokobit.com', $client->getSandboxUrl()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testApiKeyRequired() |
116
|
|
|
{ |
117
|
|
|
$this->expectException(\Dokobit\Gateway\Exception\InvalidApiKey::class); |
118
|
|
|
$client = new Client( |
|
|
|
|
119
|
|
|
$this->clientStub, |
120
|
|
|
$this->responseMapperStub, |
121
|
|
|
$this->validatorStub |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testGetFullMethodUrlForProduction() |
126
|
|
|
{ |
127
|
|
|
$client = new Client( |
128
|
|
|
$this->clientStub, |
129
|
|
|
$this->responseMapperStub, |
130
|
|
|
$this->validatorStub, |
131
|
|
|
['apiKey' => 'xxxxxx'] |
132
|
|
|
); |
133
|
|
|
$this->assertEquals( |
134
|
|
|
'https://gateway.dokobit.com/api/archive.json', |
135
|
|
|
$client->getFullMethodUrl('archive') |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testGetFullMethodUrlForSandbox() |
140
|
|
|
{ |
141
|
|
|
$this->assertEquals( |
142
|
|
|
'https://gateway-sandbox.dokobit.com/api/archive.json', |
143
|
|
|
$this->client->getFullMethodUrl('archive') |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testGet() |
148
|
|
|
{ |
149
|
|
|
$this->methodStub |
150
|
|
|
->expects($this->once()) |
151
|
|
|
->method('createResult') |
152
|
|
|
->willReturn( |
153
|
|
|
$this->getMockBuilder('Dokobit\Gateway\Result\ResultInterface') |
154
|
|
|
->disableOriginalConstructor() |
155
|
|
|
->getMock() |
156
|
|
|
) |
157
|
|
|
; |
158
|
|
|
$this->methodStub |
159
|
|
|
->expects($this->once()) |
160
|
|
|
->method('getMethod') |
161
|
|
|
; |
162
|
|
|
$this->methodStub |
163
|
|
|
->expects($this->once()) |
164
|
|
|
->method('getAction') |
165
|
|
|
; |
166
|
|
|
$this->methodStub |
167
|
|
|
->expects($this->once()) |
168
|
|
|
->method('getValidationConstraints') |
169
|
|
|
; |
170
|
|
|
|
171
|
|
|
$this->responseMapperStub |
172
|
|
|
->expects($this->once()) |
173
|
|
|
->method('map') |
174
|
|
|
; |
175
|
|
|
|
176
|
|
|
$this->clientStub |
177
|
|
|
->expects($this->once()) |
178
|
|
|
->method('requestJson') |
179
|
|
|
->willReturn([]) |
180
|
|
|
; |
181
|
|
|
|
182
|
|
|
$this->validatorStub |
183
|
|
|
->expects($this->once()) |
184
|
|
|
->method('validate') |
185
|
|
|
->willReturn([]) |
186
|
|
|
; |
187
|
|
|
|
188
|
|
|
$this->client->get($this->methodStub); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* The client has a few methods to generate view URLs. This tests them. |
193
|
|
|
*/ |
194
|
|
|
public function testGetViewUrls() |
195
|
|
|
{ |
196
|
|
|
$client = Client::create(['sandbox' => true, 'apiKey' => 'xxx']); |
197
|
|
|
|
198
|
|
|
$signingToken = 'MyToken123'; |
199
|
|
|
$accessToken = 'MyAccessToken'; |
200
|
|
|
$this->assertSame( |
201
|
|
|
'https://gateway-sandbox.dokobit.com/open/'.$signingToken, |
202
|
|
|
$client->getOpenUrl($signingToken) |
203
|
|
|
); |
204
|
|
|
$this->assertSame( |
205
|
|
|
'https://gateway-sandbox.dokobit.com/signing/'.$signingToken.'?access_token='.$accessToken, |
206
|
|
|
$client->getSigningUrl($signingToken, $accessToken) |
207
|
|
|
); |
208
|
|
|
$this->assertSame( |
209
|
|
|
'https://gateway-sandbox.dokobit.com/signing/batch/'.$signingToken, |
210
|
|
|
$client->getBatchSigningUrl($signingToken) |
211
|
|
|
); |
212
|
|
|
$this->assertSame( |
213
|
|
|
'https://gateway-sandbox.dokobit.com/signing/sequence/'.$signingToken, |
214
|
|
|
$client->getSequenceSigningUrl($signingToken) |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testGetValidationFailed() |
219
|
|
|
{ |
220
|
|
|
$this->expectException(\Dokobit\Gateway\Exception\QueryValidator::class); |
221
|
|
|
$this->expectExceptionMessage('Query parameters validation failed'); |
222
|
|
|
$violations = $this->getMockBuilder('Symfony\Component\Validator\ConstraintViolationList') |
223
|
|
|
->disableOriginalConstructor() |
224
|
|
|
->getMock(); |
225
|
|
|
$violations |
226
|
|
|
->method('count') |
227
|
|
|
->willReturn(1) |
228
|
|
|
; |
229
|
|
|
|
230
|
|
|
$this->validatorStub |
231
|
|
|
->expects($this->once()) |
232
|
|
|
->method('validate') |
233
|
|
|
->willReturn($violations) |
234
|
|
|
; |
235
|
|
|
|
236
|
|
|
$this->client->get($this->methodStub); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: