1
|
|
|
<?php |
2
|
|
|
namespace Mezon\CustomClient\Tests; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use Mezon\CustomClient\CustomClient; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
10
|
|
|
*/ |
11
|
|
|
class CustomClientUnitTest extends BaseTestUtilities |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Testing invalid construction |
16
|
|
|
*/ |
17
|
|
|
public function testConstructorInvalid(): void |
18
|
|
|
{ |
19
|
|
|
// setup and test body |
20
|
|
|
$client = new CustomClient(''); |
21
|
|
|
|
22
|
|
|
$this->assertEquals('', $client->getUrl()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Testing valid construction |
27
|
|
|
*/ |
28
|
|
|
public function testConstructorValid(): void |
29
|
|
|
{ |
30
|
|
|
$client = new CustomClient('http://yandex.ru/', [ |
31
|
|
|
'header' |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->assertEquals('http://yandex.ru', $client->getUrl(), 'Invalid URL'); |
35
|
|
|
$this->assertEquals(1, count($client->getHeaders()), 'Invalid headers'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Data provider for sending requests tests |
40
|
|
|
* |
41
|
|
|
* @return array test data |
42
|
|
|
*/ |
43
|
|
|
public function sendRequestDataProvider(): array |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
[ |
47
|
|
|
'sendGetRequest' |
48
|
|
|
], |
49
|
|
|
[ |
50
|
|
|
'sendPostRequest' |
51
|
|
|
], |
52
|
|
|
[ |
53
|
|
|
'sendPutRequest' |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'sendDeleteRequest' |
57
|
|
|
] |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Testing sendGetRequest method |
63
|
|
|
* |
64
|
|
|
* @param string $methodName |
65
|
|
|
* Method name to be tested |
66
|
|
|
* @dataProvider sendRequestDataProvider |
67
|
|
|
*/ |
68
|
|
|
public function testSendRequest(string $methodName): void |
69
|
|
|
{ |
70
|
|
|
// setup |
71
|
|
|
$client = $this->getMock(); |
72
|
|
|
$client->method('sendRequest')->willReturn([ |
73
|
|
|
'return', |
74
|
|
|
1 |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
// test body |
78
|
|
|
$result = $client->$methodName('/end-point/'); |
79
|
|
|
|
80
|
|
|
// assertions |
81
|
|
|
$this->assertEquals('return', $result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Testing method assertUrl |
86
|
|
|
*/ |
87
|
|
|
public function testInvalidCall(): void |
88
|
|
|
{ |
89
|
|
|
// assertions |
90
|
|
|
$this->expectException(\Exception::class); |
91
|
|
|
|
92
|
|
|
// setup |
93
|
|
|
$client = new CustomClient(''); |
94
|
|
|
|
95
|
|
|
// test body |
96
|
|
|
$client->sendGetRequest('some-end-point'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Testing method |
101
|
|
|
*/ |
102
|
|
|
public function testSetUrl(): void |
103
|
|
|
{ |
104
|
|
|
// setup |
105
|
|
|
$client = new CustomClient(); |
106
|
|
|
|
107
|
|
|
// test body |
108
|
|
|
$client->setUrl('testing-url'); |
109
|
|
|
|
110
|
|
|
// assertions |
111
|
|
|
$this->assertEquals('testing-url', $client->getUrl()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|