testPrivateCallNoException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 14
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\ServiceHttpTransport\ServiceHttpTransport;
6
use Mezon\Transport\HttpRequestParams;
7
use Mezon\Security\MockProvider;
8
use Mezon\Transport\Tests\MockParamsFetcher;
9
10
/**
11
 *
12
 * @psalm-suppress PropertyNotSetInConstructor
13
 */
14
class ServiceHttpTransportUnitTest extends TestCase
15
{
16
17
    /**
18
     *
19
     * {@inheritdoc}
20
     * @see TestCase::setUp()
21
     */
22
    protected function setUp(): void
23
    {
24
        $_SERVER['REQUEST_METHOD'] = 'GET';
25
    }
26
27
    /**
28
     * Getting mock object.
29
     *
30
     * @return object ServiceLogic mocked object
31
     */
32
    protected function getServiceLogicMock(): object
33
    {
34
        return $this->getMockBuilder(TestingServiceLogicForHttpTransport::class)
35
            ->disableOriginalConstructor()
36
            ->onlyMethods([
37
            'connect'
38
        ])
39
            ->getMock();
40
    }
41
42
    /**
43
     * Getting mock object
44
     *
45
     * @return object ServiceRestTransport mocked object
46
     */
47
    protected function getTransportMock(): object
48
    {
49
        $mock = $this->getMockBuilder(ServiceHttpTransport::class)
50
            ->setConstructorArgs([
51
            new MockProvider()
52
        ])
53
            ->onlyMethods([
54
            'header',
55
            'createSession'
56
        ])
57
            ->getMock();
58
59
        $mock->expects($this->once())
60
            ->method('header');
61
62
        $paramFetcher = new MockParamsFetcher('token');
63
64
        $mock->setParamsFetcher($paramFetcher);
65
66
        return $mock;
67
    }
68
69
    /**
70
     * Testing that security provider was set
71
     */
72
    public function testSecurityProviderInitObject(): void
73
    {
74
        $transport = new ServiceHttpTransport(new MockProvider());
75
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
76
    }
77
78
    /**
79
     * Testing that header function is called once for each header
80
     */
81
    public function testSingleHeaderCall(): void
82
    {
83
        $mock = $this->getTransportMock();
84
85
        $serviceLogic = $this->getServiceLogicMock();
86
87
        $serviceLogic->expects($this->once())
88
            ->method('connect');
89
90
        $mock->callLogic($serviceLogic, 'connect');
91
    }
92
93
    /**
94
     * Testing that header function is called once for each header
95
     */
96
    public function testSingleHeaderCallPublic(): void
97
    {
98
        $mock = $this->getTransportMock();
99
100
        $serviceLogic = $this->getServiceLogicMock();
101
102
        $serviceLogic->expects($this->once())
103
            ->method('connect');
104
105
        $mock->callPublicLogic($serviceLogic, 'connect');
106
    }
107
108
    /**
109
     * Testing expected header values
110
     */
111
    public function testExpectedHeaderValues(): void
112
    {
113
        $mock = $this->getTransportMock();
114
115
        $mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8'));
116
117
        $serviceLogic = $this->getServiceLogicMock();
118
119
        $mock->callLogic($serviceLogic, 'connect');
120
    }
121
122
    /**
123
     * Testing expected header values
124
     */
125
    public function testExpectedHeaderValuesPublic(): void
126
    {
127
        $mock = $this->getTransportMock();
128
129
        $mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8'));
130
131
        $serviceLogic = $this->getServiceLogicMock();
132
133
        $mock->callPublicLogic($serviceLogic, 'connect');
134
    }
135
136
    /**
137
     * Getting tricky mock object
138
     *
139
     * @return object mock object
140
     */
141
    protected function getTransportMockEx(string $mode = 'publicCall'): object
142
    {
143
        $mock = $this->getTransportMock();
144
145
        $mock->setServiceLogic($this->getServiceLogicMock());
146
147
        $mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8'));
148
149
        $mock->addRoute('connect', 'connect', 'GET', $mode, [
150
            'content_type' => 'text/html; charset=utf-8'
151
        ]);
152
153
        $_SERVER['REQUEST_METHOD'] = 'GET';
154
        $_GET['r'] = 'connect';
155
156
        return $mock;
157
    }
158
159
    /**
160
     * Testing expected header values
161
     */
162
    public function testExpectedHeaderValuesEx(): void
163
    {
164
        $mock = $this->getTransportMockEx('callLogic');
165
166
        $mock->getRouter()->callRoute($_GET['r']);
167
    }
168
169
    /**
170
     * Testing expected header values
171
     */
172
    public function testExpectedHeaderValuesPublicEx(): void
173
    {
174
        $mock = $this->getTransportMockEx('publicCall');
175
176
        $mock->getRouter()->callRoute($_GET['r']);
177
    }
178
179
    /**
180
     * Testing public call without createSession method
181
     */
182
    public function testPublicCall(): void
183
    {
184
        $mock = $this->getTransportMock();
185
186
        $mock->setServiceLogic($this->getServiceLogicMock());
187
188
        $mock->expects($this->never())
189
            ->method('createSession');
190
191
        $mock->addRoute('public-method', 'publicMethod', 'GET', 'public_call');
192
193
        $mock->getRouter()->callRoute('/public-method/');
194
    }
195
196
    /**
197
     * Testing private call with createSession method
198
     */
199
    public function testPrivateCallNoException(): void
200
    {
201
        // setup
202
        $mock = $this->getTransportMock();
203
204
        $mock->setServiceLogic($this->getServiceLogicMock());
205
206
        $mock->expects($this->once())
207
            ->method('createSession');
208
209
        $mock->addRoute('private-method', 'privateMethod', 'GET', 'private_call');
210
211
        // test body and assertions
212
        $mock->getRouter()->callRoute('/private-method/');
213
    }
214
215
    /**
216
     * Testing creaetSession method
217
     */
218
    public function testCreateSession(): void
219
    {
220
        // setup and assertions
221
        $securityProvider = $this->getMockBuilder(MockProvider::class)
222
            ->onlyMethods([
223
            'createSession'
224
        ])
225
            ->disableOriginalConstructor()
226
            ->getMock();
227
        $securityProvider->expects($this->once())
228
            ->method('createSession');
229
230
        $transport = new ServiceHttpTransport($securityProvider);
231
232
        // test body
233
        $transport->createSession('some-token');
234
    }
235
236
    /**
237
     * Testing trace outputting
238
     */
239
    public function testTraceOutput(): void
240
    {
241
        // setup
242
        $e = [
243
            "message" => "msg",
244
            "code" => - 1
245
        ];
246
        $transport = $this->getTransportMockEx();
247
248
        // test body
249
        ob_start();
250
        $transport->outputException($e);
251
        $content = ob_get_contents();
252
        ob_end_clean();
253
254
        // assertions
255
        $this->assertStringContainsString('"msg"', $content);
256
        $this->assertStringContainsString('-1', $content);
257
        $this->assertTrue(is_array(json_decode($content, true)));
258
    }
259
}
260