Completed
Push — master ( 6d353a...9ce6e7 )
by Joschi
03:43
created

BasicServerTest::testServerInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
     * @since Method available since Release 3.4.0
69
     */
70
    public static function setUpBeforeClass()
71
    {
72
        TestModule::autorun();
73
74
        // Register a repositoryc
75
        RepositoryFacade::register(
76
            'repo',
77
            [
78
                'type' => FileAdapterStrategy::TYPE,
79
                'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture',
80
            ]
81
        );
82
83
        // Enable the default routes
84
        ServerFacade::enableObjectRoute('repo');
85
    }
86
87
    /**
88
     * Test the server instantiation
89
     */
90
    public function testServerInstance()
91
    {
92
        $server = Kernel::create(Server::class);
93
        $this->assertInstanceOf(Server::class, $server);
94
    }
95
96
    /**
97
     * Test registering and dispatching a route
98
     */
99
    public function testRegisterDispatchRoute()
100
    {
101
        $route = new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class);
102
        $route->setTokens([
103
            'id' => '\d+',
104
            'format' => '(\.[^/]+)?',
105
        ]);
106
        ServerFacade::registerRoute($route);
107
108
        $uri = new Uri('http://apparat/blog/default/1.html');
109
        $request = new ServerRequest();
110
        $request = $request->withUri($uri);
111
        $response = ServerFacade::dispatchRequest($request);
112
        $this->assertInstanceOf(ResponseInterface::class, $response);
113
    }
114
115
    /**
116
     * Test registering and dispatching a route with callable handler
117
     */
118
    public function testRegisterDispatchRouteCallableHandler()
119
    {
120
        $route = new Route(
121
            Route::GET,
122
            'default2',
123
            '/handler/{id}{format}',
124
            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...
125
                return Kernel::create(Response::class, []);
126
            }
127
        );
128
        $route->setTokens([
129
            'id' => '\d+',
130
            'format' => '(\.[^/]+)?',
131
        ]);
132
        ServerFacade::registerRoute($route);
133
134
        $uri = new Uri('http://apparat/blog/handler/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 a route mismatch
143
     */
144
    public function testRouteMismatch()
145
    {
146
        $uri = new Uri('http://apparat/blog/invalid-route');
147
        $request = new ServerRequest();
148
        $request = $request->withUri($uri);
149
150
        /** @var InfrastructureServer $server */
151
        $server = Kernel::create(InfrastructureServer::class);
152
        $route = new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class);
153
        $server->registerRoute($route);
154
155
        $this->assertInstanceOf(AuraErrorRoute::class, $server->dispatchRequestToRoute($request));
156
    }
157
158
    /**
159
     * Test a handler mismatch
160
     *
161
     * @expectedException \Apparat\Server\Ports\Route\InvalidArgumentException
162
     * @expectedExceptionCode 1468918389
163
     */
164
    public function testHandlerMismatch()
165
    {
166
        $uri = new Uri('http://apparat/blog/invalid-handler');
167
        $request = new ServerRequest();
168
        $request = $request->withUri($uri);
169
170
        /** @var InfrastructureServer $server */
171
        $server = Kernel::create(InfrastructureServer::class);
172
        $route = new Route(
173
            Route::GET,
174
            'default',
175
            '/invalid-handler',
176
            [ObjectRoute::OBJECT_STR => TestObjectAction::class]
177
        );
178
        $server->registerRoute($route);
179
180
        $route = $server->dispatchRequestToRoute($request);
181
        $server->getRouteAction($request, $route);
182
    }
183
184
    /**
185
     * Test custom template resources
186
     */
187
    public function testCustomTemplateResources()
188
    {
189
        $noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR;
190
        ServerFacade::setViewResources([
191
            TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR,
192
            TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR,
193
            TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR,
194
        ]);
195
196
        // Enable the default routes for a repository "repo"
197
        $uri = new Uri('http://apparat/blog/repo/2016/06/20/2');
198
        $request = new ServerRequest();
199
        $request = $request->withUri($uri);
200
        $response = ServerFacade::dispatchRequest($request);
201
        $this->assertEquals('[(article)]', trim($response->getBody()));
202
    }
203
}
204