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
|
|
|
// Register custom view resources |
86
|
|
|
$noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR; |
87
|
|
|
ServerFacade::setViewResources([ |
88
|
|
|
TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR, |
89
|
|
|
TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR, |
90
|
|
|
TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR, |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* This method is called after the last test of this test class is run. |
96
|
|
|
*/ |
97
|
|
|
public static function tearDownAfterClass() |
98
|
|
|
{ |
99
|
|
|
parent::tearDownAfterClass(); |
100
|
|
|
|
101
|
|
|
// Reset the server |
102
|
|
|
ServerFacade::reset(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test the server instantiation |
107
|
|
|
*/ |
108
|
|
|
public function testServerInstance() |
109
|
|
|
{ |
110
|
|
|
$server = Kernel::create(Server::class); |
111
|
|
|
$this->assertInstanceOf(Server::class, $server); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Test registering and dispatching a route |
116
|
|
|
*/ |
117
|
|
|
public function testRegisterDispatchRoute() |
118
|
|
|
{ |
119
|
|
|
$route = new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class); |
120
|
|
|
$route->setTokens([ |
121
|
|
|
'id' => '\d+', |
122
|
|
|
'format' => '(\.[^/]+)?', |
123
|
|
|
]); |
124
|
|
|
ServerFacade::registerRoute($route); |
125
|
|
|
|
126
|
|
|
$uri = new Uri('http://apparat/blog/default/1.html'); |
127
|
|
|
$request = new ServerRequest(); |
128
|
|
|
$request = $request->withUri($uri); |
129
|
|
|
$response = ServerFacade::dispatchRequest($request); |
130
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Test registering and dispatching a route with callable handler |
135
|
|
|
*/ |
136
|
|
|
public function testRegisterDispatchRouteCallableHandler() |
137
|
|
|
{ |
138
|
|
|
$route = new Route( |
139
|
|
|
Route::GET, |
140
|
|
|
'handler', |
141
|
|
|
'/handler/{id}{format}', |
142
|
|
|
function () { |
143
|
|
|
return Kernel::create(Response::class, []); |
144
|
|
|
} |
145
|
|
|
); |
146
|
|
|
$route->setTokens([ |
147
|
|
|
'id' => '\d+', |
148
|
|
|
'format' => '(\.[^/]+)?', |
149
|
|
|
]); |
150
|
|
|
ServerFacade::registerRoute($route); |
151
|
|
|
|
152
|
|
|
$uri = new Uri('http://apparat/blog/handler/1.html'); |
153
|
|
|
$request = new ServerRequest(); |
154
|
|
|
$request = $request->withUri($uri); |
155
|
|
|
$response = ServerFacade::dispatchRequest($request); |
156
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Test adding and matching a static route |
161
|
|
|
*/ |
162
|
|
|
public function testStaticRoute() { |
163
|
|
|
ServerFacade::registerRoute(RouteFactory::createStaticRoute('/about', 'Test/About')); |
164
|
|
|
|
165
|
|
|
$uri = new Uri('http://apparat/blog/about'); |
166
|
|
|
$request = new ServerRequest(); |
167
|
|
|
$request = $request->withUri($uri); |
168
|
|
|
$response = ServerFacade::dispatchRequest($request); |
169
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
170
|
|
|
$this->assertEquals('[(about)]', trim($response->getBody())); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Test a handler mismatch |
175
|
|
|
* |
176
|
|
|
* @expectedException \Apparat\Server\Ports\Route\InvalidArgumentException |
177
|
|
|
* @expectedExceptionCode 1468918389 |
178
|
|
|
*/ |
179
|
|
|
public function testHandlerMismatch() |
180
|
|
|
{ |
181
|
|
|
$uri = new Uri('http://apparat/blog/invalid-handler'); |
182
|
|
|
$request = new ServerRequest(); |
183
|
|
|
$request = $request->withUri($uri); |
184
|
|
|
|
185
|
|
|
/** @var InfrastructureServer $server */ |
186
|
|
|
$server = Kernel::create(InfrastructureServer::class); |
187
|
|
|
$route = new Route( |
188
|
|
|
Route::GET, |
189
|
|
|
'default', |
190
|
|
|
'/invalid-handler', |
191
|
|
|
[ObjectRoute::OBJECT_STR => TestObjectAction::class] |
192
|
|
|
); |
193
|
|
|
$route->setObject(true); |
194
|
|
|
$server->registerRoute($route); |
195
|
|
|
|
196
|
|
|
$route = $server->dispatchRequestToRoute($request); |
197
|
|
|
$server->getRouteAction($request, $route); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Test custom template resources |
202
|
|
|
*/ |
203
|
|
|
public function testCustomTemplateResources() |
204
|
|
|
{ |
205
|
|
|
// Enable the default routes for a repository "repo" |
206
|
|
|
$uri = new Uri('http://apparat/blog/repo/2016/06/20/2'); |
207
|
|
|
$request = new ServerRequest(); |
208
|
|
|
$request = $request->withUri($uri); |
209
|
|
|
$response = ServerFacade::dispatchRequest($request); |
210
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
211
|
|
|
$this->assertEquals('[(article)]', trim($response->getBody())); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Test an object list response |
216
|
|
|
*/ |
217
|
|
|
public function testListResponse() |
218
|
|
|
{ |
219
|
|
|
$uri = new Uri('http://apparat/blog/repo/*/*/*/*'); |
220
|
|
|
$request = new ServerRequest(); |
221
|
|
|
$request = $request->withUri($uri); |
222
|
|
|
$response = ServerFacade::dispatchRequest($request); |
223
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|