Passed
Push — master ( 035a69...ef3bdc )
by Kirill
03:22
created

MiddlewareTest::testPipelineExceptionMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Router;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use Spiral\Router\Exception\RouteException;
19
use Spiral\Router\Route;
20
use Spiral\Router\Target\Group;
21
use Spiral\Tests\Router\Diactoros\UriFactory;
22
use Spiral\Tests\Router\Fixtures\TestController;
23
use Spiral\Router\UriHandler;
24
use Laminas\Diactoros\ServerRequest;
25
use Laminas\Diactoros\Uri;
26
27
class MiddlewareTest extends BaseTest
28
{
29
    public function testRoute(): void
30
    {
31
        $router = $this->makeRouter();
32
33
        $router->setRoute(
34
            'group',
35
            (new Route('/<controller>[/<action>[/<id>]]', new Group([
36
                'test' => TestController::class,
37
            ])))->withMiddleware(HeaderMiddleware::class)
38
        );
39
40
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
41
        $this->assertSame(200, $response->getStatusCode());
42
        $this->assertSame('hello world', (string)$response->getBody());
43
        $this->assertSame('Value*', $response->getHeaderLine('Header'));
44
45
        $r = $router->getRoute('group')->withMiddleware(HeaderMiddleware::class);
0 ignored issues
show
Bug introduced by
The method withMiddleware() does not exist on Spiral\Router\RouteInterface. It seems like you code against a sub-type of Spiral\Router\RouteInterface such as Spiral\Router\Route or Spiral\Router\Route. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        $r = $router->getRoute('group')->/** @scrutinizer ignore-call */ withMiddleware(HeaderMiddleware::class);
Loading history...
46
47
        $r = $r->match(new ServerRequest([], [], new Uri('/test')));
48
        $response = $r->handle(new ServerRequest([], [], new Uri('/test')));
49
        $this->assertSame(200, $response->getStatusCode());
50
        $this->assertSame('hello world', (string)$response->getBody());
51
        $this->assertSame('Value*,Value*', $response->getHeaderLine('Header'));
52
    }
53
54
    public function testRouteRuntime(): void
55
    {
56
        $router = $this->makeRouter();
57
58
        $router->setRoute(
59
            'group',
60
            (new Route('/<controller>[/<action>[/<id>]]', new Group([
61
                'test' => TestController::class,
62
            ])))->withMiddleware(new HeaderMiddleware())
63
        );
64
65
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
66
        $this->assertSame(200, $response->getStatusCode());
67
        $this->assertSame('hello world', (string)$response->getBody());
68
        $this->assertSame('Value*', $response->getHeaderLine('Header'));
69
    }
70
71
    public function testRouteArray(): void
72
    {
73
        $router = $this->makeRouter();
74
75
        $router->setRoute(
76
            'group',
77
            (new Route('/<controller>[/<action>[/<id>]]', new Group([
78
                'test' => TestController::class,
79
            ])))->withMiddleware([new HeaderMiddleware(), HeaderMiddleware::class])
80
        );
81
82
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
83
        $this->assertSame(200, $response->getStatusCode());
84
        $this->assertSame('hello world', (string)$response->getBody());
85
        $this->assertSame('Value*,Value*', $response->getHeaderLine('Header'));
86
87
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
88
        $this->assertSame(200, $response->getStatusCode());
89
        $this->assertSame('hello world', (string)$response->getBody());
90
        $this->assertSame('Value*,Value*', $response->getHeaderLine('Header'));
91
    }
92
93
    public function testInvalid(): void
94
    {
95
        $this->expectException(RouteException::class);
96
97
        $router = $this->makeRouter();
98
99
        $router->setRoute(
100
            'group',
101
            (new Route('/<controller>[/<action>[/<id>]]', new Group([
102
                'test' => TestController::class,
103
            ])))->withMiddleware($this)
0 ignored issues
show
Bug introduced by
$this of type Spiral\Tests\Router\MiddlewareTest is incompatible with the type Psr\Http\Server\MiddlewareInterface|array|string expected by parameter $middleware of Spiral\Router\Route::withMiddleware(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
            ])))->withMiddleware(/** @scrutinizer ignore-type */ $this)
Loading history...
104
        );
105
106
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
107
        $this->assertSame(200, $response->getStatusCode());
108
        $this->assertSame('hello world', (string)$response->getBody());
109
        $this->assertSame('Value*,Value*', $response->getHeaderLine('Header'));
110
    }
111
112
    public function testInvalid2(): void
113
    {
114
        $this->expectException(RouteException::class);
115
116
        $router = $this->makeRouter();
117
118
        $router->setRoute(
119
            'group',
120
            (new Route('/<controller>[/<action>[/<id>]]', new Group([
121
                'test' => TestController::class,
122
            ])))->withMiddleware([[]])
123
        );
124
125
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
126
        $this->assertSame(200, $response->getStatusCode());
127
        $this->assertSame('hello world', (string)$response->getBody());
128
        $this->assertSame('Value*,Value*', $response->getHeaderLine('Header'));
129
    }
130
131
    public function testPipelineException(): void
132
    {
133
        $this->expectException(RouteException::class);
134
135
        $r = (new Route('/<controller>[/<action>[/<id>]]', new Group([
136
            'test' => TestController::class,
137
        ])))->withMiddleware([new HeaderMiddleware(), HeaderMiddleware::class]);
138
        $r = $r->withUriHandler(new UriHandler(new UriFactory()));
139
140
        $r = $r->match(new ServerRequest([], [], new Uri('/test')));
141
        $response = $r->handle(new ServerRequest([], [], new Uri('/test')));
142
        $this->assertSame(200, $response->getStatusCode());
143
        $this->assertSame('hello world', (string)$response->getBody());
144
        $this->assertSame('Value*,Value*', $response->getHeaderLine('Header'));
145
    }
146
147
    public function testPipelineExceptionMiddleware(): void
148
    {
149
        $this->expectException(RouteException::class);
150
151
        $r = (new Route('/<controller>[/<action>[/<id>]]', new Group([
152
            'test' => TestController::class,
153
        ])))->withMiddleware([new HeaderMiddleware(), 'other']);
154
        $r = $r->withUriHandler(new UriHandler(new UriFactory()));
155
156
        $r = $r->withContainer($this->container);
157
158
        $r = $r->match(new ServerRequest([], [], new Uri('/test')));
159
        $r->handle(new ServerRequest([], [], new Uri('/test')));
160
    }
161
}
162