1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-server |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Server\Tests |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Server\Tests; |
38
|
|
|
|
39
|
|
|
use Apparat\Dev\Tests\AbstractTest; |
40
|
|
|
use Apparat\Kernel\Ports\Kernel; |
41
|
|
|
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy; |
42
|
|
|
use Apparat\Object\Ports\Facades\RepositoryFacade; |
43
|
|
|
use Apparat\Server\Infrastructure\Model\Server; |
44
|
|
|
use Apparat\Server\Infrastructure\Model\Server as InfrastructureServer; |
45
|
|
|
use Apparat\Server\Ports\Facade\ServerFacade; |
46
|
|
|
use Apparat\Server\Ports\Route\Route; |
47
|
|
|
use Apparat\Server\Ports\Route\RouteFactory; |
48
|
|
|
use Apparat\Server\Ports\Types\ObjectRoute; |
49
|
|
|
use Apparat\Server\Ports\View\TYPO3FluidView; |
50
|
|
|
use Apparat\Server\Tests\Adr\TestAction; |
51
|
|
|
use Apparat\Server\Tests\Adr\TestModule; |
52
|
|
|
use Apparat\Server\Tests\Adr\TestObjectAction; |
53
|
|
|
use Psr\Http\Message\ResponseInterface; |
54
|
|
|
use Zend\Diactoros\Response; |
55
|
|
|
use Zend\Diactoros\ServerRequest; |
56
|
|
|
use Zend\Diactoros\Uri; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Basic server test |
60
|
|
|
* |
61
|
|
|
* @package Apparat\Server |
62
|
|
|
* @subpackage Apparat\Server\Tests |
63
|
|
|
*/ |
64
|
|
|
class BasicServerTest extends AbstractTest |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* This method is called before the first test of this test class is run. |
68
|
|
|
*/ |
69
|
|
|
public static function setUpBeforeClass() |
70
|
|
|
{ |
71
|
|
|
TestModule::autorun(); |
72
|
|
|
|
73
|
|
|
// Register a repository |
74
|
|
|
RepositoryFacade::register( |
75
|
|
|
'repo', |
76
|
|
|
[ |
77
|
|
|
'type' => FileAdapterStrategy::TYPE, |
78
|
|
|
'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture', |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
// Enable the default routes |
83
|
|
|
ServerFacade::enableObjectRoute('repo'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* This method is called after the last test of this test class is run. |
88
|
|
|
*/ |
89
|
|
|
public static function tearDownAfterClass() |
90
|
|
|
{ |
91
|
|
|
parent::tearDownAfterClass(); |
92
|
|
|
|
93
|
|
|
// Reset the server |
94
|
|
|
ServerFacade::reset(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Enable custom view resources |
99
|
|
|
*/ |
100
|
|
|
protected static function enableCustomViewResources() |
101
|
|
|
{ |
102
|
|
|
// Register custom view resources |
103
|
|
|
$noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR; |
104
|
|
|
ServerFacade::setViewResources([ |
105
|
|
|
TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR, |
106
|
|
|
TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR, |
107
|
|
|
TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Test the server instantiation |
113
|
|
|
*/ |
114
|
|
|
public function testServerInstance() |
115
|
|
|
{ |
116
|
|
|
$server = Kernel::create(Server::class); |
117
|
|
|
$this->assertInstanceOf(Server::class, $server); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Test registering and dispatching a route |
122
|
|
|
*/ |
123
|
|
|
public function testRegisterDispatchRoute() |
124
|
|
|
{ |
125
|
|
|
self::enableCustomViewResources(); |
126
|
|
|
|
127
|
|
|
$route = new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class); |
128
|
|
|
$route->setTokens([ |
129
|
|
|
'id' => '\d+', |
130
|
|
|
'format' => '(\.[^/]+)?', |
131
|
|
|
]); |
132
|
|
|
ServerFacade::registerRoute($route); |
133
|
|
|
|
134
|
|
|
$uri = new Uri('http://apparat/blog/default/1.html'); |
135
|
|
|
$request = new ServerRequest(); |
136
|
|
|
$request = $request->withUri($uri); |
137
|
|
|
$response = ServerFacade::dispatchRequest($request); |
138
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Test registering and dispatching a route with callable handler |
143
|
|
|
*/ |
144
|
|
|
public function testRegisterDispatchRouteCallableHandler() |
145
|
|
|
{ |
146
|
|
|
$route = new Route( |
147
|
|
|
Route::GET, |
148
|
|
|
'handler', |
149
|
|
|
'/handler/{id}{format}', |
150
|
|
|
function () { |
151
|
|
|
return Kernel::create(Response::class, []); |
152
|
|
|
} |
153
|
|
|
); |
154
|
|
|
$route->setTokens([ |
155
|
|
|
'id' => '\d+', |
156
|
|
|
'format' => '(\.[^/]+)?', |
157
|
|
|
]); |
158
|
|
|
ServerFacade::registerRoute($route); |
159
|
|
|
|
160
|
|
|
$uri = new Uri('http://apparat/blog/handler/1.html'); |
161
|
|
|
$request = new ServerRequest(); |
162
|
|
|
$request = $request->withUri($uri); |
163
|
|
|
$response = ServerFacade::dispatchRequest($request); |
164
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Test adding and matching a static route |
169
|
|
|
*/ |
170
|
|
|
public function testStaticRoute() |
171
|
|
|
{ |
172
|
|
|
self::enableCustomViewResources(); |
173
|
|
|
|
174
|
|
|
// Register a static route |
175
|
|
|
ServerFacade::registerRoute(RouteFactory::createStaticRoute('/about', 'Test/About')); |
176
|
|
|
|
177
|
|
|
$uri = new Uri('http://apparat/blog/about'); |
178
|
|
|
$request = new ServerRequest(); |
179
|
|
|
$request = $request->withUri($uri); |
180
|
|
|
$response = ServerFacade::dispatchRequest($request); |
181
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
182
|
|
|
$this->assertEquals('[(about)]', trim($response->getBody())); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Test a handler mismatch |
187
|
|
|
* |
188
|
|
|
* @expectedException \Apparat\Server\Ports\Route\InvalidArgumentException |
189
|
|
|
* @expectedExceptionCode 1468918389 |
190
|
|
|
*/ |
191
|
|
|
public function testHandlerMismatch() |
192
|
|
|
{ |
193
|
|
|
$uri = new Uri('http://apparat/blog/invalid-handler'); |
194
|
|
|
$request = new ServerRequest(); |
195
|
|
|
$request = $request->withUri($uri); |
196
|
|
|
|
197
|
|
|
/** @var InfrastructureServer $server */ |
198
|
|
|
$server = Kernel::create(InfrastructureServer::class); |
199
|
|
|
$route = new Route( |
200
|
|
|
Route::GET, |
201
|
|
|
'default', |
202
|
|
|
'/invalid-handler', |
203
|
|
|
[ObjectRoute::OBJECT_STR => TestObjectAction::class] |
204
|
|
|
); |
205
|
|
|
$route->setObject(true); |
206
|
|
|
$server->registerRoute($route); |
207
|
|
|
|
208
|
|
|
$route = $server->dispatchRequestToRoute($request); |
209
|
|
|
$server->getRouteAction($request, $route); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Test custom template resources |
214
|
|
|
*/ |
215
|
|
|
public function testCustomTemplateResources() |
216
|
|
|
{ |
217
|
|
|
// Enable the default routes for a repository "repo" |
218
|
|
|
$uri = new Uri('http://apparat/blog/repo/2016/06/20/2'); |
219
|
|
|
$request = new ServerRequest(); |
220
|
|
|
$request = $request->withUri($uri); |
221
|
|
|
$response = ServerFacade::dispatchRequest($request); |
222
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
223
|
|
|
$this->assertEquals('[(article)]', trim($response->getBody())); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Test an object list response |
228
|
|
|
*/ |
229
|
|
|
public function testListResponse() |
230
|
|
|
{ |
231
|
|
|
$uri = new Uri('http://apparat/blog/repo/*/*/*/*'); |
232
|
|
|
$request = new ServerRequest(); |
233
|
|
|
$request = $request->withUri($uri); |
234
|
|
|
$response = ServerFacade::dispatchRequest($request); |
235
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|