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
|
|
|
// assertions |
16
|
|
|
$this->expectException(\Exception::class); |
17
|
|
|
|
18
|
|
|
// setup and test body |
19
|
|
|
$client = new CustomClient(''); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Testing valid construction |
24
|
|
|
*/ |
25
|
|
|
public function testConstructorValid(): void |
26
|
|
|
{ |
27
|
|
|
$client = new CustomClient('http://yandex.ru/', [ |
28
|
|
|
'header' |
29
|
|
|
]); |
30
|
|
|
|
31
|
|
|
$this->assertEquals('http://yandex.ru', $client->getUrl(), 'Invalid URL'); |
32
|
|
|
$this->assertEquals(1, count($client->getHeaders()), 'Invalid headers'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Data provider for sending requests tests |
37
|
|
|
* |
38
|
|
|
* @return array test data |
39
|
|
|
*/ |
40
|
|
|
public function sendRequestDataProvider(): array |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
[ |
44
|
|
|
'sendGetRequest' |
45
|
|
|
], |
46
|
|
|
[ |
47
|
|
|
'sendPostRequest' |
48
|
|
|
], |
49
|
|
|
[ |
50
|
|
|
'sendPutRequest' |
51
|
|
|
], |
52
|
|
|
[ |
53
|
|
|
'sendDeleteRequest' |
54
|
|
|
] |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Testing sendGetRequest method |
60
|
|
|
* |
61
|
|
|
* @param string $methodName |
62
|
|
|
* Method name to be tested |
63
|
|
|
* @dataProvider sendRequestDataProvider |
64
|
|
|
*/ |
65
|
|
|
public function testSendRequest(string $methodName): void |
66
|
|
|
{ |
67
|
|
|
// setup |
68
|
|
|
$client = $this->getMock(); |
69
|
|
|
$client->method('sendRequest')->willReturn([ |
70
|
|
|
'return', |
71
|
|
|
1 |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
// test body |
75
|
|
|
$result = $client->$methodName('/end-point/'); |
76
|
|
|
|
77
|
|
|
// assertions |
78
|
|
|
$this->assertEquals('return', $result); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Data provider for the test testResultDispatching |
83
|
|
|
* |
84
|
|
|
* @return array testing data |
85
|
|
|
*/ |
86
|
|
|
public function resultDispatchingDataProvider(): array |
87
|
|
|
{ |
88
|
|
|
$codes = [ |
89
|
|
|
400, |
90
|
|
|
403, |
91
|
|
|
404, |
92
|
|
|
0 |
93
|
|
|
]; |
94
|
|
|
$methods = [ |
95
|
|
|
'sendGetRequest', |
96
|
|
|
'sendPostRequest', |
97
|
|
|
'sendPutRequest', |
98
|
|
|
'sendDeleteRequest' |
99
|
|
|
]; |
100
|
|
|
$return = []; |
101
|
|
|
|
102
|
|
|
foreach ($codes as $code) { |
103
|
|
|
foreach ($methods as $method) { |
104
|
|
|
$return[] = [ |
105
|
|
|
$code, |
106
|
|
|
$method |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Testing result dispatcher |
116
|
|
|
* |
117
|
|
|
* @param int $httpCode |
118
|
|
|
* code of the response |
119
|
|
|
* @param string $methodName |
120
|
|
|
* name of the testing methodd |
121
|
|
|
* @dataProvider resultDispatchingDataProvider |
122
|
|
|
*/ |
123
|
|
|
public function testResultDispatching(int $httpCode, string $methodName): void |
124
|
|
|
{ |
125
|
|
|
// setup |
126
|
|
|
$client = $this->getMock(); |
127
|
|
|
$client->method('sendRequest')->willReturn([ |
128
|
|
|
'result', |
129
|
|
|
$httpCode |
130
|
|
|
]); |
131
|
|
|
|
132
|
|
|
// assertions |
133
|
|
|
$this->expectException(\Exception::class); |
134
|
|
|
|
135
|
|
|
// test body |
136
|
|
|
$client->$methodName('/end-point/'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|