1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Service\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Mezon\Service\Service; |
6
|
|
|
use Mezon\Service\ServiceLogic; |
7
|
|
|
use Mezon\Service\ServiceModel; |
8
|
|
|
use Mezon\Security\MockProvider; |
9
|
|
|
use Mezon\Service\ServiceRestTransport\ServiceRestTransport; |
10
|
|
|
use Mezon\Service\ServiceHttpTransport\ServiceHttpTransport; |
11
|
|
|
use Mezon\Service\ServiceConsoleTransport\ServiceConsoleTransport; |
12
|
|
|
use Mezon\Service\Tests\Mocks\TestingTransport; |
13
|
|
|
use Mezon\Service\ServiceBaseLogic; |
14
|
|
|
use Mezon\Transport\Tests\MockParamsFetcher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ServiceUnitTests |
18
|
|
|
* |
19
|
|
|
* @package Service |
20
|
|
|
* @subpackage ServiceUnitTests |
21
|
|
|
* @author Dodonov A.A. |
22
|
|
|
* @version v.1.0 (2019/08/17) |
23
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
24
|
|
|
*/ |
25
|
|
|
define('AS_STRING', 1); |
26
|
|
|
define('AS_OBJECT', 2); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Common service unit tests |
30
|
|
|
* |
31
|
|
|
* @author Dodonov A.A. |
32
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
33
|
|
|
*/ |
34
|
|
|
class ServiceUnitTests extends TestCase |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Service class name |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $className = Service::class; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Method returns mock |
46
|
|
|
* |
47
|
|
|
* @return object Mock of the testing class |
48
|
|
|
*/ |
49
|
|
|
protected function getMock(): object |
50
|
|
|
{ |
51
|
|
|
return $this->getMockBuilder($this->className) |
52
|
|
|
->disableOriginalConstructor() |
53
|
|
|
->onlyMethods([ |
54
|
|
|
'run' |
55
|
|
|
]) |
56
|
|
|
->getMock(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Method creates logic |
61
|
|
|
* |
62
|
|
|
* @return ServiceLogic|string Service logic object |
63
|
|
|
*/ |
64
|
|
|
protected function getLogic() |
65
|
|
|
{ |
66
|
|
|
$serviceTransport = new ServiceHttpTransport(); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return new ServiceLogic($serviceTransport->getParamsFetcher(), new MockProvider(), new ServiceModel()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Testing launcher with security provider |
73
|
|
|
* |
74
|
|
|
* @see Service::launch |
75
|
|
|
*/ |
76
|
|
|
public function testLaunchWithSecurityProvider() |
77
|
|
|
{ |
78
|
|
|
$localClassName = $this->className; |
79
|
|
|
|
80
|
|
|
$provider = new MockProvider(); |
81
|
|
|
$service = $localClassName::launch( |
82
|
|
|
$this->className, |
83
|
|
|
new ServiceLogic(new MockParamsFetcher(), $provider, new ServiceModel()), |
84
|
|
|
new ServiceModel(), |
85
|
|
|
$provider, |
86
|
|
|
new ServiceRestTransport($provider), |
87
|
|
|
false); |
88
|
|
|
|
89
|
|
|
$this->assertInstanceOf(MockProvider::class, $service->getTransport() |
90
|
|
|
->getSecurityProvider()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Trying to construct service from array of logics |
95
|
|
|
*/ |
96
|
|
|
public function testCreateServiceLogicFromArray() |
97
|
|
|
{ |
98
|
|
|
$localClassName = $this->className; |
99
|
|
|
|
100
|
|
|
$provider = new MockProvider(); |
101
|
|
|
$service = $localClassName::launch( |
102
|
|
|
$this->className, |
103
|
|
|
new ServiceLogic(new MockParamsFetcher(), $provider, new ServiceModel()), |
104
|
|
|
new ServiceModel(), |
105
|
|
|
$provider, |
106
|
|
|
new ServiceRestTransport($provider), |
107
|
|
|
false); |
108
|
|
|
|
109
|
|
|
$this->assertTrue(is_array($service->getLogics()), 'Array of logic objects was not created'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Trying to run logic method from array |
114
|
|
|
*/ |
115
|
|
|
public function testServiceLogicFromArrayCanBeExecuted() |
116
|
|
|
{ |
117
|
|
|
$localClassName = $this->className; |
118
|
|
|
|
119
|
|
|
$_GET['r'] = 'connect'; |
120
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
121
|
|
|
|
122
|
|
|
$provider = new MockProvider(); |
123
|
|
|
$service = $localClassName::launch( |
124
|
|
|
$this->className, |
125
|
|
|
new ServiceLogic(new MockParamsFetcher(), new MockProvider(), new ServiceModel()), |
126
|
|
|
new ServiceModel(), |
127
|
|
|
$provider, |
128
|
|
|
new ServiceConsoleTransport($provider), |
129
|
|
|
false); |
130
|
|
|
|
131
|
|
|
$service->run(); |
132
|
|
|
$this->assertTrue(true); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Testing launcher with transport |
137
|
|
|
* |
138
|
|
|
* @see Service::start |
139
|
|
|
*/ |
140
|
|
|
public function testStartWithTransport() |
141
|
|
|
{ |
142
|
|
|
// setup |
143
|
|
|
$localClassName = $this->className; |
144
|
|
|
$mock = $this->getMock(); |
145
|
|
|
|
146
|
|
|
// test body |
147
|
|
|
$provider = new MockProvider(); |
148
|
|
|
$service = $localClassName::start( |
149
|
|
|
get_class($mock), |
150
|
|
|
new ServiceLogic(new MockParamsFetcher(), $provider, new ServiceModel()), |
151
|
|
|
new ServiceModel(), |
152
|
|
|
$provider, |
153
|
|
|
new ServiceRestTransport($provider)); |
154
|
|
|
|
155
|
|
|
// assertions |
156
|
|
|
$this->assertInstanceOf(ServiceRestTransport::class, $service->getTransport()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Testing service creation without running it |
161
|
|
|
*/ |
162
|
|
|
public function testCreateServiceWithoutRunningIt(): void |
163
|
|
|
{ |
164
|
|
|
// setup |
165
|
|
|
$localClassName = $this->className; |
166
|
|
|
$mock = $this->getMock(); |
167
|
|
|
$mock->expects($this->never()) |
168
|
|
|
->method('run'); |
169
|
|
|
|
170
|
|
|
// test body |
171
|
|
|
$provider = new MockProvider(); |
172
|
|
|
$service = $localClassName::start( |
173
|
|
|
$this->className, |
174
|
|
|
new ServiceLogic(new MockParamsFetcher(), $provider, new ServiceModel()), |
175
|
|
|
new ServiceModel(), |
176
|
|
|
$provider, |
177
|
|
|
new ServiceRestTransport($provider), |
178
|
|
|
false); |
179
|
|
|
|
180
|
|
|
// TODO create logic with StandartSecurityMethods but without model |
181
|
|
|
|
182
|
|
|
// assertions |
183
|
|
|
$this->assertInstanceOf($this->className, $service); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Testing method |
188
|
|
|
*/ |
189
|
|
|
public function testExceptionWhileConstruction(): void |
190
|
|
|
{ |
191
|
|
|
// setup and test body |
192
|
|
|
ob_start(); |
193
|
|
|
$provider = new MockProvider(); |
194
|
|
|
new ExceptionTestingService( |
195
|
|
|
new ServiceBaseLogic(new MockParamsFetcher(), $provider), |
196
|
|
|
new ServiceModel(), |
197
|
|
|
$provider, |
198
|
|
|
new TestingTransport($provider)); |
199
|
|
|
$content = ob_get_contents(); |
200
|
|
|
ob_end_clean(); |
201
|
|
|
|
202
|
|
|
// assertions |
203
|
|
|
$this->assertStringContainsString("message", $content); |
204
|
|
|
$this->assertStringContainsString("code", $content); |
205
|
|
|
$this->assertTrue(is_array(json_decode($content, true))); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.