Completed
Push — master ( 95547f...4d3aea )
by Joschi
03:25
created

BasicServerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testServerInstance() 0 5 1
A testRegisterDispatchRoute() 0 15 1
A setUpBeforeClass() 0 16 1
A testRegisterDispatchRouteCallableHandler() 0 22 1
A testRouteMismatch() 0 13 1
A testHandlerMismatch() 0 19 1
A testCustomTemplateResources() 0 17 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\Kernel\Ports\Kernel;
40
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
41
use Apparat\Object\Ports\Facades\RepositoryFacade;
42
use Apparat\Server\Domain\Model\Server;
43
use Apparat\Server\Infrastructure\Model\Server as InfrastructureServer;
44
use Apparat\Server\Infrastructure\Route\AuraErrorRoute;
45
use Apparat\Server\Ports\Facade\ServerFacade;
46
use Apparat\Server\Ports\Route\Route;
47
use Apparat\Server\Ports\Types\ObjectRoute;
48
use Apparat\Server\Ports\View\TYPO3FluidView;
49
use Apparat\Server\Tests\Adr\TestAction;
50
use Apparat\Server\Tests\Adr\TestModule;
51
use Apparat\Server\Tests\Adr\TestObjectAction;
52
use Psr\Http\Message\ResponseInterface;
53
use Zend\Diactoros\Response;
54
use Zend\Diactoros\ServerRequest;
55
use Zend\Diactoros\Uri;
56
57
/**
58
 * Basic server test
59
 *
60
 * @package Apparat\Server
61
 * @subpackage Apparat\Server\Tests
62
 */
63
class BasicServerTest extends AbstractServerTest
64
{
65
    /**
66
     * This method is called before the first test of this test class is run.
67
     */
68
    public static function setUpBeforeClass()
69
    {
70
        TestModule::autorun();
71
72
        // Register a repositoryc
73
        RepositoryFacade::register(
74
            'repo',
75
            [
76
                'type' => FileAdapterStrategy::TYPE,
77
                'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture',
78
            ]
79
        );
80
81
        // Enable the default routes
82
        ServerFacade::enableObjectRoute('repo');
83
    }
84
85
    /**
86
     * Test the server instantiation
87
     */
88
    public function testServerInstance()
89
    {
90
        $server = Kernel::create(Server::class);
91
        $this->assertInstanceOf(Server::class, $server);
92
    }
93
94
    /**
95
     * Test registering and dispatching a route
96
     */
97
    public function testRegisterDispatchRoute()
98
    {
99
        $route = new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class);
100
        $route->setTokens([
101
            'id' => '\d+',
102
            'format' => '(\.[^/]+)?',
103
        ]);
104
        ServerFacade::registerRoute($route);
105
106
        $uri = new Uri('http://apparat/blog/default/1.html');
107
        $request = new ServerRequest();
108
        $request = $request->withUri($uri);
109
        $response = ServerFacade::dispatchRequest($request);
110
        $this->assertInstanceOf(ResponseInterface::class, $response);
111
    }
112
113
    /**
114
     * Test registering and dispatching a route with callable handler
115
     */
116
    public function testRegisterDispatchRouteCallableHandler()
117
    {
118
        $route = new Route(
119
            Route::GET,
120
            'default2',
121
            '/handler/{id}{format}',
122
            function () {
0 ignored issues
show
Documentation introduced by
function () { return...nse::class, array()); } is of type object<Closure>, but the function expects a string|object<Callable>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
                return Kernel::create(Response::class, []);
124
            }
125
        );
126
        $route->setTokens([
127
            'id' => '\d+',
128
            'format' => '(\.[^/]+)?',
129
        ]);
130
        ServerFacade::registerRoute($route);
131
132
        $uri = new Uri('http://apparat/blog/handler/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 a route mismatch
141
     */
142
    public function testRouteMismatch()
143
    {
144
        $uri = new Uri('http://apparat/blog/invalid-route');
145
        $request = new ServerRequest();
146
        $request = $request->withUri($uri);
147
148
        /** @var InfrastructureServer $server */
149
        $server = Kernel::create(InfrastructureServer::class);
150
        $route = new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class);
151
        $server->registerRoute($route);
152
153
        $this->assertInstanceOf(AuraErrorRoute::class, $server->dispatchRequestToRoute($request));
154
    }
155
156
    /**
157
     * Test a handler mismatch
158
     *
159
     * @expectedException \Apparat\Server\Ports\Route\InvalidArgumentException
160
     * @expectedExceptionCode 1468918389
161
     */
162
    public function testHandlerMismatch()
163
    {
164
        $uri = new Uri('http://apparat/blog/invalid-handler');
165
        $request = new ServerRequest();
166
        $request = $request->withUri($uri);
167
168
        /** @var InfrastructureServer $server */
169
        $server = Kernel::create(InfrastructureServer::class);
170
        $route = new Route(
171
            Route::GET,
172
            'default',
173
            '/invalid-handler',
174
            [ObjectRoute::OBJECT_STR => TestObjectAction::class]
175
        );
176
        $server->registerRoute($route);
177
178
        $route = $server->dispatchRequestToRoute($request);
179
        $server->getRouteAction($request, $route);
180
    }
181
182
    /**
183
     * Test custom template resources
184
     */
185
    public function testCustomTemplateResources()
186
    {
187
        $noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR;
188
        ServerFacade::setViewResources([
189
            TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR,
190
            TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR,
191
            TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR,
192
        ]);
193
194
        // Enable the default routes for a repository "repo"
195
        $uri = new Uri('http://apparat/blog/repo/2016/06/20/2');
196
        $request = new ServerRequest();
197
        $request = $request->withUri($uri);
198
        $response = ServerFacade::dispatchRequest($request);
199
        $this->assertInstanceOf(ResponseInterface::class, $response);
200
        $this->assertEquals('[(article)]', trim($response->getBody()));
201
    }
202
203
    /**
204
     * Test an object list response
205
     */
206
    public function testListResponse()
207
    {
208
        $uri = new Uri('http://apparat/blog/repo/*/*/*/*');
209
        $request = new ServerRequest();
210
        $request = $request->withUri($uri);
211
        $response = ServerFacade::dispatchRequest($request);
212
        $this->assertInstanceOf(ResponseInterface::class, $response);
213
    }
214
}
215