Completed
Push — master ( 82f763...87fa0d )
by Joschi
06:38
created

BasicServerTest::tearDownAfterClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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