Completed
Push — master ( cf54a1...d70e7f )
by Joschi
03:49
created

BasicServerTest::testRouteMismatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 11
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\View\TYPO3FluidView;
48
use Apparat\Server\Tests\Adr\TestAction;
49
use Apparat\Server\Tests\Adr\TestModule;
50
use Psr\Http\Message\ResponseInterface;
51
use Zend\Diactoros\Response;
52
use Zend\Diactoros\ServerRequest;
53
use Zend\Diactoros\Uri;
54
55
/**
56
 * Basic server test
57
 *
58
 * @package Apparat\Server
59
 * @subpackage Apparat\Server\Tests
60
 */
61
class BasicServerTest extends AbstractServerTest
62
{
63
    /**
64
     * This method is called before the first test of this test class is run.
65
     *
66
     * @since Method available since Release 3.4.0
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('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
            'GET',
120
            'default',
121
            '/default/{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/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 a route mismatch
141
     */
142
    public function testRouteMismatch()
143
    {
144
        $uri = new Uri('http://apparat/blog/invalid');
145
        $request = new ServerRequest();
146
        $request = $request->withUri($uri);
147
148
        /** @var InfrastructureServer $server */
149
        $server = Kernel::create(InfrastructureServer::class);
150
        $route = new Route('GET', 'default', '/default/{id}{format}', TestAction::class);
151
        $route->setTokens([
152
            'id' => '\d+',
153
            'format' => '(\.[^/]+)?',
154
        ]);
155
        $server->registerRoute($route);
156
157
        $this->assertInstanceOf(AuraErrorRoute::class, $server->dispatchRequestToRoute($request));
158
    }
159
160
    /**
161
     * Test custom template resources
162
     */
163
    public function testCustomTemplateResources()
164
    {
165
        $noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR;
166
        ServerFacade::setViewResources([
167
            TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR,
168
            TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR,
169
            TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR,
170
        ]);
171
172
        // Enable the default routes for a repository "repo"
173
        $uri = new Uri('http://apparat/blog/repo/2016/06/20/2');
174
        $request = new ServerRequest();
175
        $request = $request->withUri($uri);
176
        $response = ServerFacade::dispatchRequest($request);
177
        $this->assertEquals('[(article)]', trim($response->getBody()));
178
    }
179
}
180