Completed
Push — master ( 1db20d...871671 )
by Joschi
03:02
created

ErrorTest::testNoRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 12
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\Server\Infrastructure\Action\ErrorAction;
41
use Apparat\Server\Infrastructure\Model\Server;
42
use Apparat\Server\Infrastructure\Route\AuraErrorRoute;
43
use Apparat\Server\Ports\Route\Route;
44
use Apparat\Server\Ports\Types\ObjectRoute;
45
use Apparat\Server\Tests\Adr\TestAction;
46
use Psr\Http\Message\ResponseInterface;
47
use Zend\Diactoros\ServerRequest;
48
use Zend\Diactoros\Uri;
49
50
/**
51
 * Error test
52
 *
53
 * @package Apparat\Server
54
 * @subpackage Apparat\Server\Tests
55
 */
56
class ErrorTest extends AbstractServerTest
57
{
58
    /**
59
     * Test an object route mismatch / bad object request
60
     */
61
    public function testObjectRouteMismatch()
62
    {
63
        $uri = new Uri('http://apparat/blog/');
64
        $request = new ServerRequest();
65
        $request = $request->withUri($uri);
66
67
        // Dispatch the route
68
        $route = self::$server->dispatchRequestToRoute($request);
69
        $this->assertInstanceOf(AuraErrorRoute::class, $route);
70
71
        $action = self::$server->getRouteAction($request, $route);
72
        $this->assertInstanceOf(ErrorAction::class, $action);
73
74
        /** @var ResponseInterface $response */
75
        $response = $action();
76
        $this->assertInstanceOf(ResponseInterface::class, $response);
77
        $this->assertEquals(400, $response->getStatusCode());
78
    }
79
80
    /**
81
     * Test a disallowed HTTP method
82
     */
83
    public function testDisallowedMethod()
84
    {
85
        $uri = new Uri('http://apparat/blog/*');
86
        $request = new ServerRequest();
87
        $request = $request->withUri($uri)->withMethod(Route::POST);
88
89
        // Dispatch the route
90
        $route = self::$server->dispatchRequestToRoute($request);
91
        $this->assertInstanceOf(AuraErrorRoute::class, $route);
92
93
        $action = self::$server->getRouteAction($request, $route);
94
        $this->assertInstanceOf(ErrorAction::class, $action);
95
96
        /** @var ResponseInterface $response */
97
        $response = $action();
98
        $this->assertInstanceOf(ResponseInterface::class, $response);
99
        $this->assertEquals(405, $response->getStatusCode());
100
101
        $allowHeader = $response->getHeader('allow');
102
        $this->assertArrayEquals([Route::GET], $allowHeader);
103
    }
104
105
    /**
106
     * Test an inacceptable response
107
     */
108
    public function testInacceptableResponse()
109
    {
110
        $uri = new Uri('http://apparat/blog/*');
111
        $request = new ServerRequest();
112
        $request = $request->withUri($uri)->withAddedHeader('Accept', 'application/object');
113
114
        // Dispatch the route
115
        $route = self::$server->dispatchRequestToRoute($request);
116
        $this->assertInstanceOf(AuraErrorRoute::class, $route);
117
118
        $action = self::$server->getRouteAction($request, $route);
119
        $this->assertInstanceOf(ErrorAction::class, $action);
120
121
        /** @var ResponseInterface $response */
122
        $response = $action();
123
        $this->assertInstanceOf(ResponseInterface::class, $response);
124
        $this->assertEquals(406, $response->getStatusCode());
125
126
        $acceptHeader = $response->getHeader('accept');
127
        $this->assertArrayEquals(ObjectRoute::$accept, $acceptHeader);
128
    }
129
130
    /**
131
     * Test a route mismatch
132
     */
133
    public function testRouteMismatch()
134
    {
135
        /** @var Server $server */
136
        $server = Kernel::create(Server::class);
137
        $server->registerRoute(new Route(Route::GET, 'default', '/default/{id}{format}', TestAction::class));
138
139
        $uri = new Uri('http://apparat/blog/invalid-request');
140
        $request = new ServerRequest();
141
        $request = $request->withUri($uri);
142
143
        // Dispatch the route
144
        $route = $server->dispatchRequestToRoute($request);
145
        $this->assertInstanceOf(AuraErrorRoute::class, $route);
146
147
        $action = $server->getRouteAction($request, $route);
148
        $this->assertInstanceOf(ErrorAction::class, $action);
149
150
        /** @var ResponseInterface $response */
151
        $response = $action();
152
        $this->assertInstanceOf(ResponseInterface::class, $response);
153
        $this->assertEquals(404, $response->getStatusCode());
154
    }
155
156
    /**
157
     * Test dispatching with no route registered
158
     */
159
    public function testNoRoutes()
160
    {
161
        /** @var Server $server */
162
        $server = Kernel::create(Server::class);
163
164
        $uri = new Uri('http://apparat/blog/no-route');
165
        $request = new ServerRequest();
166
        $request = $request->withUri($uri);
167
168
        // Dispatch the route
169
        $route = $server->dispatchRequestToRoute($request);
170
        $this->assertInstanceOf(AuraErrorRoute::class, $route);
171
172
        $action = $server->getRouteAction($request, $route);
173
        $this->assertInstanceOf(ErrorAction::class, $action);
174
175
        /** @var ResponseInterface $response */
176
        $response = $action();
177
        $this->assertInstanceOf(ResponseInterface::class, $response);
178
        $this->assertEquals(501, $response->getStatusCode());
179
    }
180
}
181