1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mezon\CustomClient\Tests; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Mezon\CustomClient\CustomClient; |
8
|
|
|
|
9
|
|
|
class CustomClientUnitTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Testing invalid construction |
14
|
|
|
*/ |
15
|
|
|
public function testConstructorInvalid(): void |
16
|
|
|
{ |
17
|
|
|
// assertions |
18
|
|
|
$this->expectException(Exception::class); |
19
|
|
|
|
20
|
|
|
// setup and test body |
21
|
|
|
$client = new CustomClient(''); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Testing valid construction |
26
|
|
|
*/ |
27
|
|
|
public function testConstructorValid(): void |
28
|
|
|
{ |
29
|
|
|
$client = new CustomClient('http://yandex.ru/', [ |
30
|
|
|
'header' |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$this->assertEquals('http://yandex.ru', $client->getUrl(), 'Invalid URL'); |
34
|
|
|
$this->assertEquals(1, count($client->getHeaders()), 'Invalid headers'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Testing getters/setters for the field |
39
|
|
|
*/ |
40
|
|
|
public function testIdempotencyGetSet(): void |
41
|
|
|
{ |
42
|
|
|
// setup |
43
|
|
|
$client = new CustomClient('some url', []); |
44
|
|
|
|
45
|
|
|
// test bodyand assertions |
46
|
|
|
$client->setIdempotencyKey('i-key'); |
47
|
|
|
|
48
|
|
|
$this->assertEquals('i-key', $client->getIdempotencyKey(), 'Invalid idempotency key'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creating mock |
53
|
|
|
*/ |
54
|
|
|
protected function getMock(): object |
55
|
|
|
{ |
56
|
|
|
return $this->getMockBuilder(CustomClient::class) |
|
|
|
|
57
|
|
|
->setMethods([ |
58
|
|
|
'sendRequest' |
59
|
|
|
]) |
60
|
|
|
->disableOriginalConstructor() |
61
|
|
|
->getMock(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Data provider for sending requests tests |
66
|
|
|
* |
67
|
|
|
* @return array test data |
68
|
|
|
*/ |
69
|
|
|
public function sendRequestDataProvider(): array |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
[ |
73
|
|
|
'sendGetRequest' |
74
|
|
|
], |
75
|
|
|
[ |
76
|
|
|
'sendPostRequest' |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
'sendPutRequest' |
80
|
|
|
], |
81
|
|
|
[ |
82
|
|
|
'sendDeleteRequest' |
83
|
|
|
] |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Testing sendGetRequest method |
89
|
|
|
* |
90
|
|
|
* @param string $methodName |
91
|
|
|
* Method name to be tested |
92
|
|
|
* @dataProvider sendRequestDataProvider |
93
|
|
|
*/ |
94
|
|
|
public function testSendRequest(string $methodName): void |
95
|
|
|
{ |
96
|
|
|
// setup |
97
|
|
|
$client = $this->getMock(); |
98
|
|
|
$client->method('sendRequest')->willReturn([ |
99
|
|
|
'return', |
100
|
|
|
1 |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
// test body |
104
|
|
|
$result = $client->$methodName('/end-point/'); |
105
|
|
|
|
106
|
|
|
// assertions |
107
|
|
|
$this->assertEquals('return', $result); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Data provider for the test testResultDispatching |
112
|
|
|
* |
113
|
|
|
* @return array testing data |
114
|
|
|
*/ |
115
|
|
|
public function resultDispatchingDataProvider(): array |
116
|
|
|
{ |
117
|
|
|
$codes = [ |
118
|
|
|
400, |
119
|
|
|
403, |
120
|
|
|
404, |
121
|
|
|
0 |
122
|
|
|
]; |
123
|
|
|
$methods = [ |
124
|
|
|
'sendGetRequest', |
125
|
|
|
'sendPostRequest', |
126
|
|
|
'sendPutRequest', |
127
|
|
|
'sendDeleteRequest' |
128
|
|
|
]; |
129
|
|
|
$return = []; |
130
|
|
|
|
131
|
|
|
foreach ($codes as $code) { |
132
|
|
|
foreach ($methods as $method) { |
133
|
|
|
$return[] = [ |
134
|
|
|
$code, |
135
|
|
|
$method |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Testing result dispatcher |
145
|
|
|
* |
146
|
|
|
* @param int $httpCode |
147
|
|
|
* code of the response |
148
|
|
|
* @param string $methodName |
149
|
|
|
* name of the testing methodd |
150
|
|
|
* @dataProvider resultDispatchingDataProvider |
151
|
|
|
*/ |
152
|
|
|
public function testResultDispatching(int $httpCode, string $methodName): void |
153
|
|
|
{ |
154
|
|
|
// setup |
155
|
|
|
$client = $this->getMock(); |
156
|
|
|
$client->method('sendRequest')->willReturn([ |
157
|
|
|
'result', |
158
|
|
|
$httpCode |
159
|
|
|
]); |
160
|
|
|
|
161
|
|
|
// assertions |
162
|
|
|
$this->expectException(\Exception::class); |
163
|
|
|
|
164
|
|
|
// test body |
165
|
|
|
$client->$methodName('/end-point/'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Testing setting idempotency key |
170
|
|
|
*/ |
171
|
|
|
public function testSetIdempotencyKey(): void |
172
|
|
|
{ |
173
|
|
|
// setup |
174
|
|
|
$client = new TestClient('http://unit.test'); |
175
|
|
|
$client->setIdempotencyKey('iKey'); |
176
|
|
|
|
177
|
|
|
// test body and assertions |
178
|
|
|
$this->assertStringContainsString('iKey', implode('', $client->getCommonHeadersPublic())); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|