Passed
Push — master ( a58ef6...fc3cb3 )
by Alex
10:58 queued 01:08
created

ServiceRestTransportUnitTest::setupMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Service\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Service\ServiceRestTransport\ServiceRestTransport;
6
use Mezon\Transport\HttpRequestParams;
7
use Mezon\Security\MockProvider;
8
if (defined('MEZON_DEBUG') === false) {
9
    define('MEZON_DEBUG', true);
10
}
11
12
class ServiceRestTransportUnitTest extends TestCase
13
{
14
15
    /**
16
     * Getting mock object.
17
     *
18
     * @return object ServiceRestTransport mocked object.
19
     */
20
    protected function getTransportMock()
21
    {
22
        $mock = $this->getMockBuilder(ServiceRestTransport::class)
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

22
        $mock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(ServiceRestTransport::class)

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...
23
            ->setMethods([
24
            'header',
25
            'createSession',
26
            'errorResponse',
27
            'parentErrorResponse'
28
        ])
29
            ->getMock();
30
31
        $mock->expects($this->once())
32
            ->method('header');
33
        $mock->method('errorResponse')->willThrowException(new \Mezon\Rest\Exception('Msg', 0, 1, 1));
34
        $mock->method('parentErrorResponse')->willThrowException(new \Exception('Msg', 0));
35
36
        $mock->setParamsFetcher(
37
            $this->getMockBuilder(HttpRequestParams::class)
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

37
            /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(HttpRequestParams::class)

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...
38
                ->setMethods([
39
                'getSessionIdFromHeaders'
40
            ])
41
                ->disableOriginalConstructor()
42
                ->getMock());
43
44
        $mock->getParamsFetcher()
45
            ->method('getSessionIdFromHeaders')
46
            ->willReturn('token');
47
48
        return $mock;
49
    }
50
51
    /**
52
     * Getting mock object.
53
     *
54
     * @return object ServiceLogic mocked object.
55
     */
56
    protected function getServiceLogicMock()
57
    {
58
        return $this->getMockBuilder(TestingServiceLogicForRestTransport::class)
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

58
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(TestingServiceLogicForRestTransport::class)

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...
59
            ->disableOriginalConstructor()
60
            ->setMethods([
61
            'connect'
62
        ])
63
            ->getMock();
64
    }
65
66
    /**
67
     * Testing connect method.
68
     */
69
    public function testConstructor()
70
    {
71
        $transport = new ServiceRestTransport();
72
73
        $this->assertNotEquals(null, $transport->getSecurityProvider());
74
    }
75
76
    /**
77
     * Testing that security provider was set.
78
     */
79
    public function testSecurityProviderInitDefault()
80
    {
81
        $transport = new ServiceRestTransport();
82
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
83
    }
84
85
    /**
86
     * Testing that security provider was set.
87
     */
88
    public function testSecurityProviderInitString()
89
    {
90
        $transport = new ServiceRestTransport(MockProvider::class);
91
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
92
    }
93
94
    /**
95
     * Testing that security provider was set.
96
     */
97
    public function testSecurityProviderInitObject()
98
    {
99
        $transport = new ServiceRestTransport(new MockProvider());
100
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
101
    }
102
103
    /**
104
     * Testing that header function is called once for each header.
105
     */
106
    public function testSingleHeaderCall()
107
    {
108
        $mock = $this->getTransportMock();
109
110
        $serviceLogic = $this->getServiceLogicMock();
111
112
        $serviceLogic->expects($this->once())
113
            ->method('connect');
114
115
        $mock->callLogic($serviceLogic, 'connect');
116
    }
117
118
    /**
119
     * Testing that header function is called once for each header.
120
     */
121
    public function testSingleHeaderCallPublic()
122
    {
123
        $mock = $this->getTransportMock();
124
125
        $serviceLogic = $this->getServiceLogicMock();
126
127
        $serviceLogic->expects($this->once())
128
            ->method('connect');
129
130
        $mock->callPublicLogic($serviceLogic, 'connect');
131
    }
132
133
    /**
134
     * Setup method call
135
     *
136
     * @param string $methodName
137
     *            Method name
138
     * @return object Mock object
139
     */
140
    protected function setupMethod(string $methodName): object
141
    {
142
        $mock = $this->getTransportMock();
143
144
        $mock->setServiceLogic($this->getServiceLogicMock());
145
146
        $mock->expects($this->never())
147
            ->method('createSession');
148
149
        $_SERVER['REQUEST_METHOD'] = 'GET';
150
        $mock->addRoute('public-method', $methodName, 'GET', 'public_call');
151
152
        return $mock;
153
    }
154
155
    /**
156
     * Testing public call without createSession method.
157
     */
158
    public function testPublicCall()
159
    {
160
        // setup
161
        $mock = $this->setupMethod('publicMethod');
162
163
        // test body and assertions
164
        $mock->getRouter()->callRoute('/public-method/');
165
    }
166
167
    /**
168
     * Setup method call
169
     *
170
     * @param string $methodName
171
     *            Method name
172
     * @return object Mock object
173
     */
174
    protected function setupPrivateMethod(string $methodName): object
175
    {
176
        $mock = $this->getTransportMock();
177
178
        $mock->setServiceLogic($this->getServiceLogicMock());
179
180
        $mock->expects($this->once())
181
            ->method('createSession');
182
183
        $mock->addRoute('private-method', $methodName, 'GET', 'private_call');
184
185
        return $mock;
186
    }
187
188
    /**
189
     * Testing private call with createSession method.
190
     */
191
    public function testPrivateCall()
192
    {
193
        // setup
194
        $mock = $this->setupPrivateMethod('privateMethod');
195
196
        // test body and assertions
197
        $mock->getRouter()->callRoute('/private-method/');
198
    }
199
200
    /**
201
     * Testing public call with exception throwing
202
     */
203
    public function testPublicCallException()
204
    {
205
        // setup
206
        $mock = $this->setupMethod('methodException');
207
208
        $this->expectException(\Exception::class);
209
210
        // test body and assertions
211
        $mock->getRouter()->callRoute('/public-method/');
212
    }
213
214
    /**
215
     * Testing public call with exception throwing
216
     */
217
    public function testPublicCallRestException()
218
    {
219
        // setup
220
        $mock = $this->setupMethod('methodRestException');
221
222
        $this->expectException(\Exception::class);
223
        // test body and assertions
224
        $mock->getRouter()->callRoute('/public-method/');
225
    }
226
227
    /**
228
     * Testing private call with exception throwing
229
     */
230
    public function testPrivateCallException()
231
    {
232
        // setup
233
        $mock = $this->setupPrivateMethod('methodException');
234
235
        $this->expectException(\Exception::class);
236
237
        // test body and assertions
238
        $mock->getRouter()->callRoute('/private-method/');
239
    }
240
241
    /**
242
     * Testing private call with exception throwing
243
     */
244
    public function testPrivateCallRestException()
245
    {
246
        // setup
247
        $mock = $this->setupPrivateMethod('methodRestException');
248
249
        $this->expectException(\Exception::class);
250
251
        // test body and assertions
252
        $mock->getRouter()->callRoute('/private-method/');
253
    }
254
255
    /**
256
     * Testing errorResponse method
257
     */
258
    public function testErrorResponseException(): void
259
    {
260
        // setup
261
        $_SERVER['HTTP_HOST'] = 'http://service';
262
        $e = new \Exception('msg', 1);
263
        $Transport = new ServiceRestTransport();
264
265
        // test body
266
        $result = $Transport->errorResponse($e);
267
268
        // assertions
269
        $this->assertEquals('msg', $result['message']);
270
        $this->assertEquals(1, $result['code']);
271
        $this->assertEquals('http://service', $result['service']);
272
    }
273
274
    /**
275
     * Testing errorResponse method
276
     */
277
    public function testErrorResponseRestException(): void
278
    {
279
        // setup
280
        $_SERVER['HTTP_HOST'] = 'http://rest-service';
281
        $e = new \Mezon\Rest\Exception('msg', 1, 200, 'body');
282
        $Transport = new ServiceRestTransport();
283
284
        // test body
285
        $result = $Transport->errorResponse($e);
286
287
        // assertions
288
        $this->assertEquals('msg', $result['message']);
289
        $this->assertEquals(1, $result['code']);
290
        $this->assertEquals('http://rest-service', $result['service']);
291
        $this->assertEquals(200, $result['http_code']);
292
        $this->assertEquals('body', $result['http_body']);
293
    }
294
295
    /**
296
     * Testing parentErrorResponse method
297
     */
298
    public function testParentErrorResponseRestException(): void
299
    {
300
        // setup
301
        $e = new \Exception('msg', 1);
302
        $Transport = new ServiceRestTransport();
303
304
        // test body
305
        $result = $Transport->parentErrorResponse($e);
306
307
        // assertions
308
        $this->assertEquals('msg', $result['message']);
309
        $this->assertEquals(1, $result['code']);
310
    }
311
312
    /**
313
     * Getting tricky mock object
314
     */
315
    protected function getTransportMockEx(string $mode = 'publicCall')
316
    {
317
        $mock = $this->getTransportMock();
318
319
        $mock->setServiceLogic($this->getServiceLogicMock());
320
321
        $mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('application/json'));
322
323
        $mock->addRoute('connect', 'connect', 'GET', $mode, [
324
            'content_type' => 'application/json'
325
        ]);
326
327
        $_SERVER['REQUEST_METHOD'] = 'GET';
328
        $_GET['r'] = 'connect';
329
330
        return $mock;
331
    }
332
333
    /**
334
     * Testing method outputException
335
     */
336
    public function testOutputException(): void
337
    {
338
        // setup
339
        $e = [
340
            "message" => "msg",
341
            "code" => - 1
342
        ];
343
        $transport = $this->getTransportMockEx();
344
345
        // test body
346
        ob_start();
347
        $transport->outputException($e);
348
        $content = ob_get_contents();
349
        ob_end_clean();
350
351
        // assertions
352
        $this->assertStringContainsString('"msg"', $content);
353
        $this->assertStringContainsString('-1', $content);
354
        $this->assertTrue(is_array(json_decode($content, true)));
355
    }
356
357
    /**
358
     * Testing that route will be called
359
     */
360
    public function testCallRoute(): void
361
    {
362
        // setup
363
        $transport = new ServiceRestTransport();
364
        $transport->setServiceLogic(new TestingServiceLogicForRestTransport());
365
        $transport->addRoute('/ok/', 'ok', 'GET', 'public_call');
366
        $_GET['r'] = 'ok';
367
368
        // test body
369
        ob_start();
370
        $transport->run();
371
        $content = ob_get_contents();
372
        ob_end_clean();
373
374
        // assertions
375
        $this->assertEquals('"ok"', $content);
376
    }
377
}
378