|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\Service\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Mezon\Service\ServiceConsoleTransport\ServiceConsoleTransport; |
|
5
|
|
|
use Mezon\Security\MockProvider; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
|
10
|
|
|
*/ |
|
11
|
|
|
class ServiceConsoleTransportUnitTest extends \PHPUnit\Framework\TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Getting mock object. |
|
16
|
|
|
* |
|
17
|
|
|
* @return object ServiceConsoleTransport mocked object. |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function getTransportMock(): object |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->getMockBuilder(ServiceConsoleTransport::class) |
|
22
|
|
|
->setConstructorArgs([new MockProvider()]) |
|
23
|
|
|
->onlyMethods([ |
|
24
|
|
|
'createSession' |
|
25
|
|
|
]) |
|
26
|
|
|
->getMock(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Getting mock object. |
|
31
|
|
|
* |
|
32
|
|
|
* @return object ServiceLogic mocked object. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function getServiceLogicMock(): object |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getMockBuilder(TestingServiceLogicForConsoleTransport::class) |
|
37
|
|
|
->disableOriginalConstructor() |
|
38
|
|
|
->onlyMethods([ |
|
39
|
|
|
'connect' |
|
40
|
|
|
]) |
|
41
|
|
|
->getMock(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Testing that security provider was set. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testSecurityProviderInitObject(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$transport = new ServiceConsoleTransport(new MockProvider()); |
|
50
|
|
|
$this->assertInstanceOf(MockProvider::class, $transport->getSecurityProvider()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Testing that header function is called once for each header. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testSingleHeaderCall(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$mock = $this->getTransportMock(); |
|
59
|
|
|
|
|
60
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
|
61
|
|
|
|
|
62
|
|
|
$serviceLogic->expects($this->once()) |
|
63
|
|
|
->method('connect'); |
|
64
|
|
|
|
|
65
|
|
|
$mock->callLogic($serviceLogic, 'connect'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Testing that header function is called once for each header. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testSingleHeaderCallPublic(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$mock = $this->getTransportMock(); |
|
74
|
|
|
|
|
75
|
|
|
$serviceLogic = $this->getServiceLogicMock(); |
|
76
|
|
|
|
|
77
|
|
|
$serviceLogic->expects($this->once()) |
|
78
|
|
|
->method('connect'); |
|
79
|
|
|
|
|
80
|
|
|
$mock->callPublicLogic($serviceLogic, 'connect'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Testing public call without createSession method. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testPublicCall(): void |
|
87
|
|
|
{ |
|
88
|
|
|
// setup |
|
89
|
|
|
$_GET['r'] = '/public-method/'; |
|
90
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
91
|
|
|
$mock = $this->getTransportMock(); |
|
92
|
|
|
|
|
93
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
|
94
|
|
|
|
|
95
|
|
|
$mock->expects($this->never()) |
|
96
|
|
|
->method('createSession'); |
|
97
|
|
|
|
|
98
|
|
|
$mock->addRoute('public-method', 'publicMethod', 'GET', 'public_call'); |
|
99
|
|
|
|
|
100
|
|
|
// test body and assertions |
|
101
|
|
|
$mock->run(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Testing private call with createSession method. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testPrivateCall(): void |
|
108
|
|
|
{ |
|
109
|
|
|
// setup |
|
110
|
|
|
$_GET['r'] = '/private-method/'; |
|
111
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
112
|
|
|
$mock = $this->getTransportMock(); |
|
113
|
|
|
|
|
114
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
|
115
|
|
|
|
|
116
|
|
|
$mock->expects($this->once()) |
|
117
|
|
|
->method('createSession'); |
|
118
|
|
|
|
|
119
|
|
|
$mock->addRoute('private-method', 'privateMethod', 'GET', 'private_call'); |
|
120
|
|
|
|
|
121
|
|
|
// test body and assertions |
|
122
|
|
|
$mock->run(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Testing 'run' method |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testRun(): void |
|
129
|
|
|
{ |
|
130
|
|
|
// setup |
|
131
|
|
|
$_GET['r'] = 'public-method'; |
|
132
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
133
|
|
|
$mock = $this->getTransportMock(); |
|
134
|
|
|
|
|
135
|
|
|
$mock->setServiceLogic($this->getServiceLogicMock()); |
|
136
|
|
|
|
|
137
|
|
|
$mock->expects($this->never()) |
|
138
|
|
|
->method('createSession'); |
|
139
|
|
|
|
|
140
|
|
|
$mock->addRoute('public-method', 'publicMethod', 'GET', 'public_call'); |
|
141
|
|
|
|
|
142
|
|
|
// test body |
|
143
|
|
|
$mock->run(); |
|
144
|
|
|
|
|
145
|
|
|
// assertions |
|
146
|
|
|
$this->assertEquals('public', $mock->result); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|