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

ControllerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 34
c 1
b 0
f 0
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testUriGeneration() 0 13 1
A testRouteException() 0 11 1
A testClientException() 0 11 1
A testRoute() 0 19 1
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\Http\Exception\ClientException\NotFoundException;
15
use Spiral\Router\Exception\UndefinedRouteException;
16
use Spiral\Router\Route;
17
use Spiral\Router\Target\Controller;
18
use Spiral\Tests\Router\Fixtures\TestController;
19
use Laminas\Diactoros\ServerRequest;
20
use Laminas\Diactoros\Uri;
21
22
class ControllerTest 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('/<action>/<id>', new Controller(TestController::class))
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('/<action>[/<id>]', new Controller(TestController::class))
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('/echo')));
50
        $this->assertSame(200, $response->getStatusCode());
51
        $this->assertSame('echoed', (string)$response->getBody());
52
53
        $response = $router->handle(new ServerRequest([], [], new Uri('/id/888')));
54
        $this->assertSame(200, $response->getStatusCode());
55
        $this->assertSame('888', (string)$response->getBody());
56
    }
57
58
    public function testUriGeneration(): void
59
    {
60
        $router = $this->makeRouter();
61
        $router->setRoute(
62
            'action',
63
            new Route('/<action>/<id>', new Controller(TestController::class))
64
        );
65
66
        $uri = $router->uri('action/test');
67
        $this->assertSame('/test', $uri->getPath());
68
69
        $uri = $router->uri('action/id', ['id' => 100]);
70
        $this->assertSame('/id/100', $uri->getPath());
71
    }
72
73
    public function testClientException(): void
74
    {
75
        $this->expectException(NotFoundException::class);
76
77
        $router = $this->makeRouter();
78
        $router->setRoute(
79
            'action',
80
            new Route('/<action>[/<id>]', new Controller(TestController::class))
81
        );
82
83
        $router->handle(new ServerRequest([], [], new Uri('/other')));
84
    }
85
}
86