Completed
Push — master ( f8782f...c598c9 )
by Alex
02:46
created

TestingServiceLogicForRestTransport::ok()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
class FakeSecurityProviderForRestTransport
4
{
5
}
6
7
class TestingServiceLogicForRestTransport extends \Mezon\Service\ServiceLogic
8
{
9
10
    /**
11
     * Constructor
12
     */
13
    public function __construct()
14
    {}
15
16
    public function ok()
17
    {
18
        return "ok";
19
    }
20
21
    public function privateMethod()
22
    {}
23
24
    public function publicMethod()
25
    {}
26
27
    public function methodException()
28
    {
29
        throw (new \Exception('Msg'));
30
    }
31
32
    public function methodRestException()
33
    {
34
        throw (new \Mezon\Rest\Exception('Msg', 0, 1, 1));
35
    }
36
}
37
38
class ServiceRestTransportUnitTest extends \PHPUnit\Framework\TestCase
39
{
40
41
    /**
42
     * Getting mock object.
43
     *
44
     * @return object ServiceRestTransport mocked object.
45
     */
46
    protected function getTransportMock()
47
    {
48
        $mock = $this->getMockBuilder(\Mezon\Service\ServiceRestTransport\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

48
        $mock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(\Mezon\Service\ServiceRestTransport\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...
49
            ->setMethods([
50
            'header',
51
            'createSession',
52
            'errorResponse',
53
            'parentErrorResponse'
54
        ])
55
            ->getMock();
56
57
        $mock->expects($this->once())
58
            ->method('header');
59
        $mock->method('errorResponse')->willThrowException(new \Mezon\Rest\Exception('Msg', 0, 1, 1));
60
        $mock->method('parentErrorResponse')->willThrowException(new \Exception('Msg', 0));
61
62
        $mock->setParamsFetcher(
63
            $this->getMockBuilder(\Mezon\Service\ServiceHttpTransport\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

63
            /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(\Mezon\Service\ServiceHttpTransport\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...
64
                ->setMethods([
65
                'getSessionIdFromHeaders'
66
            ])
67
                ->disableOriginalConstructor()
68
                ->getMock());
69
70
        $mock->getParamsFetcher()
71
            ->method('getSessionIdFromHeaders')
72
            ->willReturn('token');
73
74
        return $mock;
75
    }
76
77
    /**
78
     * Getting mock object.
79
     *
80
     * @return object ServiceLogic mocked object.
81
     */
82
    protected function getServiceLogicMock()
83
    {
84
        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

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