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