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

testExpectedHeaderValuesPublic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
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
9
class ServiceHttpTransportUnitTest extends TestCase
10
{
11
12
    /**
13
     * Getting mock object.
14
     *
15
     * @return object ServiceLogic mocked object.
16
     */
17
    protected function getServiceLogicMock()
18
    {
19
        return $this->getMockBuilder(TestingServiceLogicForHttpTransport::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

19
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(TestingServiceLogicForHttpTransport::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...
20
            ->disableOriginalConstructor()
21
            ->setMethods([
22
            'connect'
23
        ])
24
            ->getMock();
25
    }
26
27
    /**
28
     * Getting mock object.
29
     *
30
     * @return object ServiceRestTransport mocked object.
31
     */
32
    protected function getTransportMock()
33
    {
34
        $mock = $this->getMockBuilder(ServiceHttpTransport::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

34
        $mock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(ServiceHttpTransport::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...
35
            ->setMethods([
36
            'header',
37
            'createSession'
38
        ])
39
            ->getMock();
40
41
        $mock->expects($this->once())
42
            ->method('header');
43
44
        $mock->setParamsFetcher(
45
            $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

45
            /** @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...
46
                ->setMethods([
47
                'getSessionIdFromHeaders'
48
            ])
49
                ->disableOriginalConstructor()
50
                ->getMock());
51
52
        $mock->getParamsFetcher()
53
            ->method('getSessionIdFromHeaders')
54
            ->willReturn('token');
55
56
        return $mock;
57
    }
58
59
    /**
60
     * Testing connect method.
61
     */
62
    public function testConstructor()
63
    {
64
        new ServiceHttpTransport();
65
66
        $this->addToAssertionCount(1);
67
    }
68
69
    /**
70
     * Testing that security provider was set.
71
     */
72
    public function testSecurityProviderInitDefault()
73
    {
74
        $transport = new ServiceHttpTransport();
75
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
76
    }
77
78
    /**
79
     * Testing that security provider was set.
80
     */
81
    public function testSecurityProviderInitString()
82
    {
83
        $transport = new ServiceHttpTransport(MockProvider::class);
84
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
85
    }
86
87
    /**
88
     * Testing that security provider was set.
89
     */
90
    public function testSecurityProviderInitObject()
91
    {
92
        $transport = new ServiceHttpTransport(new MockProvider());
93
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
94
    }
95
96
    /**
97
     * Testing that header function is called once for each header.
98
     */
99
    public function testSingleHeaderCall()
100
    {
101
        $mock = $this->getTransportMock();
102
103
        $serviceLogic = $this->getServiceLogicMock();
104
105
        $serviceLogic->expects($this->once())
106
            ->method('connect');
107
108
        $mock->callLogic($serviceLogic, 'connect');
109
    }
110
111
    /**
112
     * Testing that header function is called once for each header.
113
     */
114
    public function testSingleHeaderCallPublic()
115
    {
116
        $mock = $this->getTransportMock();
117
118
        $serviceLogic = $this->getServiceLogicMock();
119
120
        $serviceLogic->expects($this->once())
121
            ->method('connect');
122
123
        $mock->callPublicLogic($serviceLogic, 'connect');
124
    }
125
126
    /**
127
     * Testing expected header values.
128
     */
129
    public function testExpectedHeaderValues()
130
    {
131
        $mock = $this->getTransportMock();
132
133
        $mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8'));
134
135
        $serviceLogic = $this->getServiceLogicMock();
136
137
        $mock->callLogic($serviceLogic, 'connect');
138
    }
139
140
    /**
141
     * Testing expected header values.
142
     */
143
    public function testExpectedHeaderValuesPublic()
144
    {
145
        $mock = $this->getTransportMock();
146
147
        $mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8'));
148
149
        $serviceLogic = $this->getServiceLogicMock();
150
151
        $mock->callPublicLogic($serviceLogic, 'connect');
152
    }
153
154
    /**
155
     * Getting tricky mock object
156
     */
157
    protected function getTransportMockEx(string $mode = 'publicCall')
158
    {
159
        $mock = $this->getTransportMock();
160
161
        $mock->setServiceLogic($this->getServiceLogicMock());
162
163
        $mock->method('header')->with($this->equalTo('Content-Type'), $this->equalTo('text/html; charset=utf-8'));
164
165
        $mock->addRoute('connect', 'connect', 'GET', $mode, [
166
            'content_type' => 'text/html; charset=utf-8'
167
        ]);
168
169
        $_SERVER['REQUEST_METHOD'] = 'GET';
170
        $_GET['r'] = 'connect';
171
172
        return $mock;
173
    }
174
175
    /**
176
     * Testing expected header values.
177
     */
178
    public function testExpectedHeaderValuesEx()
179
    {
180
        $mock = $this->getTransportMockEx('callLogic');
181
182
        $mock->getRouter()->callRoute($_GET['r']);
183
    }
184
185
    /**
186
     * Testing expected header values.
187
     */
188
    public function testExpectedHeaderValuesPublicEx()
189
    {
190
        $mock = $this->getTransportMockEx('publicCall');
191
192
        $mock->getRouter()->callRoute($_GET['r']);
193
    }
194
195
    /**
196
     * Testing public call without createSession method.
197
     */
198
    public function testPublicCall()
199
    {
200
        $mock = $this->getTransportMock();
201
202
        $mock->setServiceLogic($this->getServiceLogicMock());
203
204
        $mock->expects($this->never())
205
            ->method('createSession');
206
207
        $mock->addRoute('public-method', 'publicMethod', 'GET', 'public_call');
208
209
        $mock->getRouter()->callRoute('/public-method/');
210
    }
211
212
    /**
213
     * Testing private call with createSession method.
214
     */
215
    public function testPrivateCallNoException()
216
    {
217
        // setup
218
        $mock = $this->getTransportMock();
219
220
        $mock->setServiceLogic($this->getServiceLogicMock());
221
222
        $mock->expects($this->once())
223
            ->method('createSession');
224
225
        $mock->addRoute('private-method', 'privateMethod', 'GET', 'private_call');
226
227
        // test body and assertions
228
        $mock->getRouter()->callRoute('/private-method/');
229
    }
230
231
    /**
232
     * Testing creaetSession method
233
     */
234
    public function testCreateSession(): void
235
    {
236
        // setup and assertions
237
        $securityProvider = $this->getMockBuilder(MockProvider::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

237
        $securityProvider = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(MockProvider::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...
238
            ->setMethods([
239
            'createSession'
240
        ])
241
            ->disableOriginalConstructor()
242
            ->getMock();
243
        $securityProvider->expects($this->once())
244
            ->method('createSession');
245
246
        $transport = new ServiceHttpTransport($securityProvider);
247
248
        // test body
249
        $transport->createSession('some-token');
250
    }
251
252
    /**
253
     * Testing method
254
     */
255
    public function test(): void
256
    {
257
        // setup
258
        $e = [
259
            "message" => "msg",
260
            "code" => - 1
261
        ];
262
        $transport = $this->getTransportMockEx();
263
264
        // test body
265
        ob_start();
266
        $transport->outputException($e);
267
        $content = ob_get_contents();
268
        ob_end_clean();
269
270
        // assertions
271
        $this->assertStringContainsString('"msg"', $content);
272
        $this->assertStringContainsString('-1', $content);
273
        $this->assertTrue(is_array(json_decode($content, true)));
274
    }
275
}
276