1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Gvera\Helpers\annotations\AnnotationUtil; |
4
|
|
|
use Gvera\Helpers\config\Config; |
5
|
|
|
use Gvera\Helpers\dependencyInjection\DIContainer; |
6
|
|
|
use Gvera\Helpers\fileSystem\FileManager; |
7
|
|
|
use Gvera\Helpers\http\HttpRequest; |
|
|
|
|
8
|
|
|
use Gvera\Helpers\http\HttpResponse; |
|
|
|
|
9
|
|
|
use Gvera\Services\ControllerService; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class ControllerServiceTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private ControllerService $controllerService; |
15
|
|
|
|
16
|
|
|
public function setUp():void |
17
|
|
|
{ |
18
|
|
|
$this->controllerService = new ControllerService(); |
19
|
|
|
$this->controllerService->setControllerAutoloadingNames($this->getAutoloadingNames()); |
20
|
|
|
$this->controllerService->setDiContainer($this->getDiContainer()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
*/ |
26
|
|
|
public function testControllerLifeCycle() |
27
|
|
|
{ |
28
|
|
|
$this->controllerService->startControllerLifecycle( |
29
|
|
|
$this->getDiContainer(), |
30
|
|
|
"/" |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$this->assertTrue( |
34
|
|
|
$this->controllerService->getControllerName() === "Index" |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$this->assertTrue( |
38
|
|
|
$this->controllerService->getMethodName() === "index" |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
|
public function testStartControllerLifeCycle() |
46
|
|
|
{ |
47
|
|
|
$this->controllerService->startControllerLifecycle( |
48
|
|
|
$this->getDiContainer(), |
49
|
|
|
"/examples/asd" |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->assertTrue( |
53
|
|
|
$this->controllerService->getControllerName() === "Examples" |
54
|
|
|
); |
55
|
|
|
$this->assertTrue( |
56
|
|
|
$this->controllerService->getMethodName() === 'asd' |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
*/ |
63
|
|
|
public function testServiceExceptions() |
64
|
|
|
{ |
65
|
|
|
$this->expectException(Exception::class); |
66
|
|
|
$this->controllerService->startControllerLifecycle( |
67
|
|
|
$this->getDiContainer(), |
68
|
|
|
"/gvcontroller" |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @test |
74
|
|
|
*/ |
75
|
|
|
public function testVersionException() |
76
|
|
|
{ |
77
|
|
|
$this->expectException(\Gvera\Exceptions\NotFoundException::class); |
78
|
|
|
$this->controllerService->startControllerLifecycle( |
79
|
|
|
$this->getDiContainer(), |
80
|
|
|
"/v5/index/asd" |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
*/ |
87
|
|
|
public function testSubControllers() |
88
|
|
|
{ |
89
|
|
|
$this->controllerService->startControllerLifecycle( |
90
|
|
|
$this->getDiContainer(), |
91
|
|
|
"/v0/moreexamples/other" |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->assertTrue( |
95
|
|
|
$this->controllerService->getControllerName() === 'MoreExamples' |
96
|
|
|
); |
97
|
|
|
$this->assertTrue( |
98
|
|
|
$this->controllerService->getMethodName() === 'other' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @test |
104
|
|
|
*/ |
105
|
|
|
public function testSpecificControllerLifeCycle() |
106
|
|
|
{ |
107
|
|
|
$this->controllerService->generateSpecificControllerLifeCycle( |
108
|
|
|
'index', |
109
|
|
|
'index' |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->assertTrue( |
113
|
|
|
$this->controllerService->getControllerName() === "Index" |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->assertTrue( |
117
|
|
|
$this->controllerService->getMethodName() === "index" |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function getDiContainer() |
122
|
|
|
{ |
123
|
|
|
$diContainer = $this->createMock(DIContainer::class); |
124
|
|
|
$diContainer->expects($this->any()) |
125
|
|
|
->method("get") |
126
|
|
|
->with($this->logicalOr( |
127
|
|
|
$this->equalTo('httpRequest'), |
128
|
|
|
$this->equalTo('httpResponse'), |
129
|
|
|
$this->equalTo('annotationUtil'), |
130
|
|
|
$this->equalTo('twigService'), |
131
|
|
|
$this->equalTo('config') |
132
|
|
|
)) |
133
|
|
|
->will( |
134
|
|
|
$this->returnCallback(array($this, 'httpCallBack')) |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $diContainer; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function httpCallBack($param) |
141
|
|
|
{ |
142
|
|
|
return $this->getMockedHttp($param); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function getAutoloadingNames() |
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
'index' => 'Index', |
149
|
|
|
'examples' => 'Examples', |
150
|
|
|
'gvcontroller' => 'GvController', |
151
|
|
|
'v0' => ['moreexamples' => 'MoreExamples'] |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function getMockedHttp($type = '') |
156
|
|
|
{ |
157
|
|
|
$validator = new \Gvera\Helpers\http\HttpRequestValidator(new \Gvera\Helpers\validation\ValidationService()); |
158
|
|
|
if ($type === 'httpRequest') { |
159
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
160
|
|
|
return new HttpRequest( |
161
|
|
|
new FileManager($this->getMockedConfig()), |
162
|
|
|
$validator |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($type === 'annotationUtil') { |
167
|
|
|
return $this->getMockedannotationUtil(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ($type === 'twigService') { |
171
|
|
|
return $this->getMockedTwigService(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($type === 'config') { |
175
|
|
|
return $this->getMockedConfig(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$httpResponse = $this->createMock(HttpResponse::class); |
179
|
|
|
$httpResponse->expects($this->any()) |
180
|
|
|
->method('response') |
181
|
|
|
->willReturn(true); |
182
|
|
|
|
183
|
|
|
$httpResponse->expects($this->any()) |
184
|
|
|
->method('redirect'); |
185
|
|
|
|
186
|
|
|
return $httpResponse; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function getMockedConfig() |
190
|
|
|
{ |
191
|
|
|
return $this->createMock(Config::class); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
private function getMockedTwigService() |
195
|
|
|
{ |
196
|
|
|
$config = $this->getMockedConfig(); |
197
|
|
|
return new \Gvera\Services\TwigService($config); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function getMockedannotationUtil() |
201
|
|
|
{ |
202
|
|
|
$annotationUtilMock = $this->createMock(AnnotationUtil::class); |
203
|
|
|
$annotationUtilMock->expects($this->any()) |
204
|
|
|
->method('validateMethods') |
205
|
|
|
->willReturn(true); |
206
|
|
|
$annotationUtilMock->expects($this->any()) |
207
|
|
|
->method('getAnnotationContentFromMethod') |
208
|
|
|
->willReturn([]); |
209
|
|
|
return $annotationUtilMock; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: