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

ServiceConsoleTransportUnitTest::testRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 19
rs 9.9666
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Service\Tests;
3
4
use Mezon\Service\ServiceConsoleTransport\ServiceConsoleTransport;
5
use Mezon\Security\MockProvider;
6
7
class ServiceConsoleTransportUnitTest extends \PHPUnit\Framework\TestCase
8
{
9
10
    /**
11
     * Getting mock object.
12
     *
13
     * @return object ServiceConsoleTransport mocked object.
14
     */
15
    protected function getTransportMock(): object
16
    {
17
        return $this->getMockBuilder(ServiceConsoleTransport::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

17
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(ServiceConsoleTransport::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...
18
            ->setMethods([
19
            'createSession'
20
        ])
21
            ->getMock();
22
    }
23
24
    /**
25
     * Getting mock object.
26
     *
27
     * @return object ServiceLogic mocked object.
28
     */
29
    protected function getServiceLogicMock(): object
30
    {
31
        return $this->getMockBuilder(TestingServiceLogicForConsoleTransport::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

31
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(TestingServiceLogicForConsoleTransport::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...
32
            ->disableOriginalConstructor()
33
            ->setMethods([
34
            'connect'
35
        ])
36
            ->getMock();
37
    }
38
39
    /**
40
     * Testing connect method.
41
     */
42
    public function testConstructor(): void
43
    {
44
        $transport = new ServiceConsoleTransport();
45
46
        $this->assertNotEquals(null, $transport->getSecurityProvider());
47
    }
48
49
    /**
50
     * Testing that security provider was set.
51
     */
52
    public function testSecurityProviderInitDefault(): void
53
    {
54
        $transport = new ServiceConsoleTransport();
55
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
56
    }
57
58
    /**
59
     * Testing that security provider was set.
60
     */
61
    public function testSecurityProviderInitString(): void
62
    {
63
        $transport = new ServiceConsoleTransport(MockProvider::class);
64
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
65
    }
66
67
    /**
68
     * Testing that security provider was set.
69
     */
70
    public function testSecurityProviderInitObject(): void
71
    {
72
        $transport = new ServiceConsoleTransport(new MockProvider());
73
        $this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider());
74
    }
75
76
    /**
77
     * Testing that header function is called once for each header.
78
     */
79
    public function testSingleHeaderCall(): void
80
    {
81
        $mock = $this->getTransportMock();
82
83
        $serviceLogic = $this->getServiceLogicMock();
84
85
        $serviceLogic->expects($this->once())
86
            ->method('connect');
87
88
        $mock->callLogic($serviceLogic, 'connect');
89
    }
90
91
    /**
92
     * Testing that header function is called once for each header.
93
     */
94
    public function testSingleHeaderCallPublic(): void
95
    {
96
        $mock = $this->getTransportMock();
97
98
        $serviceLogic = $this->getServiceLogicMock();
99
100
        $serviceLogic->expects($this->once())
101
            ->method('connect');
102
103
        $mock->callPublicLogic($serviceLogic, 'connect');
104
    }
105
106
    /**
107
     * Testing public call without createSession method.
108
     */
109
    public function testPublicCall(): void
110
    {
111
        // setup
112
        $_GET['r'] = '/public-method/';
113
        $_SERVER['REQUEST_METHOD'] = 'GET';
114
        $mock = $this->getTransportMock();
115
116
        $mock->setServiceLogic($this->getServiceLogicMock());
117
118
        $mock->expects($this->never())
119
            ->method('createSession');
120
121
        $mock->addRoute('public-method', 'publicMethod', 'GET', 'public_call');
122
123
        // test body and assertions
124
        $mock->run();
125
    }
126
127
    /**
128
     * Testing private call with createSession method.
129
     */
130
    public function testPrivateCall(): void
131
    {
132
        // setup
133
        $_GET['r'] = '/private-method/';
134
        $_SERVER['REQUEST_METHOD'] = 'GET';
135
        $mock = $this->getTransportMock();
136
137
        $mock->setServiceLogic($this->getServiceLogicMock());
138
139
        $mock->expects($this->once())
140
            ->method('createSession');
141
142
        $mock->addRoute('private-method', 'privateMethod', 'GET', 'private_call');
143
144
        // test body and assertions
145
        $mock->run();
146
    }
147
148
    /**
149
     * Testing 'run' method
150
     */
151
    public function testRun(): void
152
    {
153
        // setup
154
        $_GET['r'] = 'public-method';
155
        $_SERVER['REQUEST_METHOD'] = 'GET';
156
        $mock = $this->getTransportMock();
157
158
        $mock->setServiceLogic($this->getServiceLogicMock());
159
160
        $mock->expects($this->never())
161
            ->method('createSession');
162
163
        $mock->addRoute('public-method', 'publicMethod', 'GET', 'public_call');
164
165
        // test body
166
        $mock->run();
167
168
        // assertions
169
        $this->assertEquals('public', $mock->result);
170
    }
171
}
172