testPublicCallRestException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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