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

SingleActionTest::testWrongActionRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
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 Spiral\Router\Exception\RouteException;
15
use Spiral\Router\Exception\UndefinedRouteException;
16
use Spiral\Router\Route;
17
use Spiral\Router\Target\Action;
18
use Spiral\Tests\Router\Fixtures\TestController;
19
use Laminas\Diactoros\ServerRequest;
20
use Laminas\Diactoros\Uri;
21
22
class SingleActionTest extends BaseTest
23
{
24
    public function testRouteException(): void
25
    {
26
        $this->expectException(UndefinedRouteException::class);
27
28
        $router = $this->makeRouter();
29
        $router->setRoute(
30
            'action',
31
            new Route('/test', new Action(TestController::class, 'test'))
32
        );
33
34
        $router->handle(new ServerRequest());
35
    }
36
37
    public function testRoute(): void
38
    {
39
        $router = $this->makeRouter();
40
        $router->setRoute(
41
            'action',
42
            new Route('/test', new Action(TestController::class, 'test'))
43
        );
44
45
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
46
        $this->assertSame(200, $response->getStatusCode());
47
        $this->assertSame('hello world', (string)$response->getBody());
48
49
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
50
        $this->assertSame(200, $response->getStatusCode());
51
        $this->assertSame('hello world', (string)$response->getBody());
52
    }
53
54
    public function testVerbRoute(): void
55
    {
56
        $this->expectException(UndefinedRouteException::class);
57
58
        $router = $this->makeRouter();
59
        $router->setRoute(
60
            'action',
61
            (new Route('/test', new Action(TestController::class, 'test')))->withVerbs('POST')
62
        );
63
64
        $router->handle(new ServerRequest([], [], new Uri('/test')));
65
    }
66
67
    public function testVerbRouteValid(): void
68
    {
69
        $router = $this->makeRouter();
70
        $router->setRoute(
71
            'action',
72
            (new Route('/test', new Action(TestController::class, 'test')))->withVerbs('POST')
73
        );
74
75
        $response = $router->handle(new ServerRequest([], [], new Uri('/test'), 'POST'));
76
        $this->assertSame(200, $response->getStatusCode());
77
        $this->assertSame('hello world', (string)$response->getBody());
78
    }
79
80
    public function testEchoed(): void
81
    {
82
        $router = $this->makeRouter();
83
        $router->setRoute(
84
            'action',
85
            new Route('/test', new Action(TestController::class, 'echo'))
86
        );
87
88
        $response = $router->handle(new ServerRequest([], [], new Uri('/test')));
89
        $this->assertSame(200, $response->getStatusCode());
90
        $this->assertSame('echoed', (string)$response->getBody());
91
    }
92
93
    public function testAutoFill(): void
94
    {
95
        $router = $this->makeRouter();
96
        $router->setRoute(
97
            'action',
98
            new Route('/<action>', new Action(TestController::class, 'echo'))
99
        );
100
101
        $response = $router->handle(new ServerRequest([], [], new Uri('/echo')));
102
        $this->assertSame(200, $response->getStatusCode());
103
        $this->assertSame('echoed', (string)$response->getBody());
104
105
        $e = null;
106
        try {
107
            $router->handle(new ServerRequest([], [], new Uri('/test')));
108
        } catch (UndefinedRouteException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
109
        }
110
111
        $this->assertNotNull($e, 'Autofill not fired');
112
    }
113
114
    public function testVerbException(): void
115
    {
116
        $this->expectException(RouteException::class);
117
118
        $router = $this->makeRouter();
119
        $router->setRoute(
120
            'action',
121
            (new Route('/test', new Action(TestController::class, 'test')))->withVerbs('other')
122
        );
123
    }
124
125
    public function testParametrizedActionRoute(): void
126
    {
127
        $router = $this->makeRouter();
128
        $router->setRoute(
129
            'action',
130
            new Route('/test/<id:\d+>', new Action(TestController::class, 'id'))
131
        );
132
133
        $response = $router->handle(new ServerRequest([], [], new Uri('/test/100')));
134
        $this->assertSame(200, $response->getStatusCode());
135
        $this->assertSame('100', (string)$response->getBody());
136
    }
137
138
    public function testParametrizedActionRouteNotFound(): void
139
    {
140
        $this->expectException(UndefinedRouteException::class);
141
142
        $router = $this->makeRouter();
143
        $router->setRoute(
144
            'action',
145
            new Route('/test/<id:\d+>', new Action(TestController::class, 'id'))
146
        );
147
148
        $router->handle(new ServerRequest([], [], new Uri('/test/abc')));
149
    }
150
151
    public function testUriGeneration(): void
152
    {
153
        $router = $this->makeRouter();
154
        $router->setRoute(
155
            'action',
156
            new Route('/test/<id>', new Action(TestController::class, 'id'))
157
        );
158
159
        $uri = $router->uri('action');
160
        $this->assertSame('/test', $uri->getPath());
161
162
        $uri = $router->uri('action', ['id' => 100]);
163
        $this->assertSame('/test/100', $uri->getPath());
164
    }
165
166
    public function testWrongActionRoute(): void
167
    {
168
        $this->expectException(UndefinedRouteException::class);
169
170
        $router = $this->makeRouter();
171
        $router->setRoute(
172
            'action',
173
            new Route('/test', new Action(TestController::class, 'test'))
174
        );
175
176
        $router->handle(new ServerRequest([], [], new Uri('/other')));
177
    }
178
}
179