Issues (1)

Mezon/Service/Tests/ServiceClientUnitTests.php (1 issue)

Severity
1
<?php
2
namespace Mezon\Service\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Service\ServiceClient;
6
use Mezon\DnsClient\DnsClient;
7
8
/**
9
 * Basic tests for service client
10
 *
11
 * @author Dodonov A.
12
 * @group baseTests
13
 * @codeCoverageIgnore
14
 */
15
class ServiceClientUnitTests extends TestCase
16
{
17
18
    /**
19
     * Client class name
20
     */
21
    protected $clientClassName = ServiceClient::class;
22
23
    /**
24
     * Common setup for all tests
25
     */
26
    public function setUp(): void
27
    {
28
        DnsClient::clear();
29
        DnsClient::setService('existing-service', 'https://existing-service.com');
30
    }
31
32
    /**
33
     * Method creates mock for the service client
34
     *
35
     * @param array $methods
36
     *            mocking methods
37
     * @return object Mock
38
     */
39
    protected function getServiceClientRawMock(
40
        array $methods = [
41
            'sendPostRequest',
42
            'sendGetRequest',
43
            'sendPutRequest',
44
            'sendDeleteRequest'
45
        ]): object
46
    {
47
        return $this->getMockBuilder($this->clientClassName)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder($this->clientClassName)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
48
            ->setMethods($methods)
49
            ->setConstructorArgs([
50
            'http://some-service-url'
51
        ])
52
            ->getMock();
53
    }
54
55
    /**
56
     * Method creates mock with setup
57
     *
58
     * @param string $dataFile
59
     *            File name with testing data
60
     * @return object Mock object
61
     */
62
    protected function getServiceClientMock(string $dataFile = 'login-with-invalid-session-id'): object
63
    {
64
        $mock = $this->getServiceClientRawMock([
65
            'sendRequest'
66
        ]);
67
68
        $mock->method('sendRequest')->will(
69
            $this->returnValue(json_decode(file_get_contents(__DIR__ . '/Conf/' . $dataFile . '.json'), true)));
70
71
        return $mock;
72
    }
73
74
    /**
75
     * Testing construction with login and password
76
     */
77
    public function testConstructWithLogin(): void
78
    {
79
        // setup
80
        $mock = $this->getServiceClientMock('construct-with-login');
81
82
        // test body
83
        $mock->__construct('http://example.com/', 'login', 'password');
84
85
        // assertions
86
        $this->assertEquals('login', $mock->getStoredLogin(), 'Login was not set');
87
        $this->assertEquals('session id', $mock->getToken(), 'SessionId was not set');
88
    }
89
90
    /**
91
     * Testing constructor
92
     */
93
    public function testSetHeader(): void
94
    {
95
        // setup
96
        $client = new $this->clientClassName('http://example.com/');
97
98
        // test body and assertions
99
        $this->assertEquals('', $client->getService(), 'Field was init but it must not');
100
    }
101
102
    /**
103
     * Checking exception throwing if the service was not found
104
     */
105
    public function testNoServiceFound(): void
106
    {
107
        $this->expectException(\Exception::class);
108
109
        new $this->clientClassName('auth');
110
    }
111
112
    /**
113
     * Testing that service was found.
114
     */
115
    public function testServiceFound(): void
116
    {
117
        $client = new $this->clientClassName('existing-service');
118
119
        $this->assertEquals('existing-service', $client->getService(), 'Field was init but it must not');
120
    }
121
122
    /**
123
     * Data provider for the test testSendRequest
124
     *
125
     * @return array test data
126
     */
127
    public function sendRequestDataProvider(): array
128
    {
129
        return [
130
            [
131
                'sendGetRequest'
132
            ],
133
            [
134
                'sendPostRequest'
135
            ],
136
            [
137
                'sendPutRequest'
138
            ],
139
            [
140
                'sendDeleteRequest'
141
            ]
142
        ];
143
    }
144
145
    /**
146
     * Testing send[Post|Get|Put|Delete]Request
147
     *
148
     * @param string $methodName
149
     *            testing method name
150
     * @dataProvider sendRequestDataProvider
151
     */
152
    public function testSendRequest(string $methodName): void
153
    {
154
        $mock = $this->getServiceClientMock('test-request');
155
156
        $result = $mock->$methodName('http://ya.ru', []);
157
158
        $this->assertEquals(1, $result->result);
159
    }
160
161
    /**
162
     * Testing setToken method
163
     */
164
    public function testSetToken(): void
165
    {
166
        // setup
167
        $mock = $this->getServiceClientRawMock(); // we need this function, as we need mock without any extra setup
168
169
        // test body
170
        $mock->setToken('token', 'login');
171
172
        // assertions
173
        $this->assertEquals('token', $mock->getToken(), 'SessionId was not set');
174
        $this->assertEquals('login', $mock->getStoredLogin(), 'Login was not set');
175
    }
176
177
    /**
178
     * Testing getToken method
179
     */
180
    public function testGetToken(): void
181
    {
182
        // setup
183
        $mock = $this->getServiceClientRawMock(); // we need this function, as we need mock without any extra setup
184
185
        // test body
186
        $sessionId = $mock->getToken();
187
188
        // assertions
189
        $this->assertEquals('', $sessionId, 'Invalid session id');
190
    }
191
192
    /**
193
     * Testing setToken method
194
     */
195
    public function testSetTokenException(): void
196
    {
197
        // setup
198
        $mock = $this->getServiceClientRawMock(); // we need this function, as we need mock without any extra setup
199
200
        // test body and assertions
201
        $this->expectException(\Exception::class);
202
        $mock->setToken('');
203
    }
204
205
    /**
206
     * Testing getSelfId method
207
     */
208
    public function testGetSelfId(): void
209
    {
210
        // setup
211
        $mock = $this->getServiceClientMock('self-id');
212
213
        // test body
214
        $selfId = $mock->getSelfId();
215
216
        // assertions
217
        $this->assertEquals('123', $selfId, 'Invalid self id');
218
    }
219
220
    /**
221
     * Testing getSelfLogin method
222
     */
223
    public function testGetSelfLogin(): void
224
    {
225
        // setup
226
        $mock = $this->getServiceClientMock('self-login');
227
228
        // test body
229
        $selfLogin = $mock->getSelfLogin();
230
231
        // assertions
232
        $this->assertEquals('admin', $selfLogin, 'Invalid self login');
233
    }
234
235
    /**
236
     * Testing loginAs method
237
     */
238
    public function testLoginAsWithInvalidSessionId(): void
239
    {
240
        // setup
241
        $mock = $this->getServiceClientMock();
242
243
        // test body and assertions
244
        $this->expectException(\Exception::class);
245
246
        $mock->loginAs('registered-user', 'login');
247
    }
248
249
    /**
250
     * Testing loginAs method
251
     */
252
    public function testLoginAsWithInvalidSessionId2(): void
253
    {
254
        // setup
255
        $mock = $this->getServiceClientMock();
256
257
        // test body
258
        $mock->loginAs('registered', 'id');
259
260
        // assertions
261
        $this->assertEquals('', $mock->getStoredLogin());
262
    }
263
264
    /**
265
     * Testing loginAs method
266
     */
267
    public function testLoginAs(): void
268
    {
269
        // setup
270
        $mock = $this->getServiceClientMock('login-as');
271
272
        // test body
273
        $mock->loginAs('registered', 'login');
274
275
        // assertions
276
        $this->assertEquals('session-id', $mock->getToken(), 'Invalid self login');
277
    }
278
279
    /**
280
     * Testing construction with login and password and invalid session_id
281
     */
282
    public function testConstructWithLoginAndInvalidSessionId(): void
283
    {
284
        // setup
285
        $mock = $this->getServiceClientMock();
286
287
        // test body and assertions
288
        $this->expectException(\Exception::class);
289
        $mock->__construct('http://example.com/', 'login', 'password');
290
    }
291
292
    /**
293
     * Testing setting and getting rewrite mode
294
     */
295
    public function testRewriteMode(): void
296
    {
297
        // setup
298
        $mock = $this->getServiceClientMock();
299
300
        // test body and assertions
301
        $mock->setRewriteMode(true);
302
        $this->assertTrue($mock->getRewriteMode());
303
304
        $mock->setRewriteMode(false);
305
        $this->assertFalse($mock->getRewriteMode());
306
    }
307
308
    /**
309
     * Testing authentication headers
310
     */
311
    public function testAuthenticationHeaders(): void
312
    {
313
        // setup and assertions
314
        $client = $this->getServiceClientRawMock([
315
            'sendRequest'
316
        ]);
317
        $client->method('sendRequest')
318
            ->with(
319
            $this->callback(function () {
320
                return true;
321
            }),
322
            $this->callback(
323
                function ($headers) {
324
                    $this->assertContains('Authentication: Basic some-token', $headers);
325
                    $this->assertContains('Cgi-Authorization: Basic some-token', $headers);
326
                    return true;
327
                }))
328
            ->willReturn([
329
            "{\"session_id\":\"some-password\"}",
330
            200
331
        ]);
332
        $client->setToken('some-token');
333
334
        // test body
335
        $client->connect('some-login', 'some-password');
336
    }
337
338
    /**
339
     * Testing method
340
     */
341
    public function testGetRequestUrlException(): void
342
    {
343
        // setup and assertions
344
        $this->expectException(\Exception::class);
345
        $client = new TestingServiceClient('https://some-service');
346
347
        // test body
348
        $client->getRequestUriPublic('unexistingUri');
349
    }
350
351
    /**
352
     * Testing exception throwing if error response was got
353
     */
354
    public function testGetReuqetsUrlWithHandlingError(): void
355
    {
356
        // setup and assertions
357
        $this->expectException(\Exception::class);
358
        $client = new TestingServiceClient('https://some-service');
359
        $client->sendRequestResult = [
360
            '{"message":"", "code": 1}',
361
            200
362
        ];
363
364
        // test body
365
        $client->sendGetRequest('some endpoint');
366
    }
367
368
    /**
369
     * Mtrhod tests case when sendRequest method have returned invalid json
370
     */
371
    public function testInvalidJsonReturnedFromSendRequest(): void
372
    {
373
        // setup and assertions
374
        $this->expectException(\Mezon\Rest\Exception::class);
375
        $client = new TestingServiceClient('https://some-service');
376
        $client->sendRequestResult = [
377
            'some crap',
378
            200
379
        ];
380
381
        // test body
382
        $client->sendGetRequest('some endpoint');
383
    }
384
}
385