BasicServerTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 10
lcom 0
cbo 9
dl 0
loc 175
rs 10
c 8
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A enableCustomViewResources() 0 10 1
A setUpBeforeClass() 0 16 1
A tearDownAfterClass() 0 7 1
A testServerInstance() 0 5 1
A testRegisterDispatchRoute() 0 15 1
A testRegisterDispatchRouteCallableHandler() 0 22 1
A testStaticRoute() 0 14 1
A testHandlerMismatch() 0 20 1
A testCustomTemplateResources() 0 13 1
A testListResponse() 0 8 1
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
        $route = new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class);
126
        $route->setTokens([
127
            'id' => '\d+',
128
            'format' => '(\.[^/]+)?',
129
        ]);
130
        ServerFacade::registerRoute($route);
131
132
        $uri = new Uri('http://apparat/blog/default/1.html');
133
        $request = new ServerRequest();
134
        $request = $request->withUri($uri);
135
        $response = ServerFacade::dispatchRequest($request);
136
        $this->assertInstanceOf(ResponseInterface::class, $response);
137
    }
138
139
    /**
140
     * Test registering and dispatching a route with callable handler
141
     */
142
    public function testRegisterDispatchRouteCallableHandler()
143
    {
144
        $route = new Route(
145
            Route::GET,
146
            'handler',
147
            '/handler/{id}{format}',
148
            function () {
149
                return Kernel::create(Response::class, []);
150
            }
151
        );
152
        $route->setTokens([
153
            'id' => '\d+',
154
            'format' => '(\.[^/]+)?',
155
        ]);
156
        ServerFacade::registerRoute($route);
157
158
        $uri = new Uri('http://apparat/blog/handler/1.html');
159
        $request = new ServerRequest();
160
        $request = $request->withUri($uri);
161
        $response = ServerFacade::dispatchRequest($request);
162
        $this->assertInstanceOf(ResponseInterface::class, $response);
163
    }
164
165
    /**
166
     * Test adding and matching a static route
167
     */
168
    public function testStaticRoute()
169
    {
170
        self::enableCustomViewResources();
171
172
        //  Register a static route
173
        ServerFacade::registerRoute(RouteFactory::createStaticRoute('/about', 'Test/About'));
174
175
        $uri = new Uri('http://apparat/blog/about');
176
        $request = new ServerRequest();
177
        $request = $request->withUri($uri);
178
        $response = ServerFacade::dispatchRequest($request);
179
        $this->assertInstanceOf(ResponseInterface::class, $response);
180
        $this->assertEquals('[(about)]', trim($response->getBody()));
181
    }
182
183
    /**
184
     * Test a handler mismatch
185
     *
186
     * @expectedException \Apparat\Server\Ports\Route\InvalidArgumentException
187
     * @expectedExceptionCode 1468918389
188
     */
189
    public function testHandlerMismatch()
190
    {
191
        $uri = new Uri('http://apparat/blog/invalid-handler');
192
        $request = new ServerRequest();
193
        $request = $request->withUri($uri);
194
195
        /** @var InfrastructureServer $server */
196
        $server = Kernel::create(InfrastructureServer::class);
197
        $route = new Route(
198
            Route::GET,
199
            'default',
200
            '/invalid-handler',
201
            [ObjectRoute::OBJECT_STR => TestObjectAction::class]
202
        );
203
        $route->setObject(true);
204
        $server->registerRoute($route);
205
206
        $route = $server->dispatchRequestToRoute($request);
207
        $server->getRouteAction($request, $route);
208
    }
209
210
    /**
211
     * Test custom template resources
212
     */
213
    public function testCustomTemplateResources()
214
    {
215
        // Enable a custom set of templates
216
        self::enableCustomViewResources();
217
218
        // Enable the default routes for a repository "repo"
219
        $uri = new Uri('http://apparat/blog/repo/2016/06/20/2');
220
        $request = new ServerRequest();
221
        $request = $request->withUri($uri);
222
        $response = ServerFacade::dispatchRequest($request);
223
        $this->assertInstanceOf(ResponseInterface::class, $response);
224
        $this->assertEquals('[(article)]', trim($response->getBody()));
225
    }
226
227
    /**
228
     * Test an object list response
229
     */
230
    public function testListResponse()
231
    {
232
        $uri = new Uri('http://apparat/blog/repo/*/*/*/*');
233
        $request = new ServerRequest();
234
        $request = $request->withUri($uri);
235
        $response = ServerFacade::dispatchRequest($request);
236
        $this->assertInstanceOf(ResponseInterface::class, $response);
237
    }
238
}
239