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

UriTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 40
c 1
b 0
f 0
dl 0
loc 80
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testQuery() 0 13 1
A testSlugNoDefault() 0 8 1
A testDirect() 0 12 1
A testCastRoute() 0 12 1
A testSlugDefault() 0 11 1
A testSlug() 0 12 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\Router\Exception\UndefinedRouteException;
15
use Spiral\Router\Route;
16
use Spiral\Router\Target\Group;
17
use Spiral\Tests\Router\Fixtures\TestController;
18
19
class UriTest extends BaseTest
20
{
21
    public function testCastRoute(): void
22
    {
23
        $router = $this->makeRouter();
24
        $router->setRoute(
25
            'group',
26
            new Route('/<controller>[/<action>[/<id>]]', new Group([
27
                'test' => TestController::class,
28
            ]))
29
        );
30
31
        $uri = $router->uri('group/test:test');
32
        $this->assertSame('/test/test', $uri->getPath());
33
    }
34
35
    public function testQuery(): void
36
    {
37
        $router = $this->makeRouter();
38
        $router->setRoute(
39
            'group',
40
            new Route('/<controller>[/<action>[/<id>]]', new Group([
41
                'test' => TestController::class,
42
            ]))
43
        );
44
45
        $uri = $router->uri('group/test:id', ['id' => 100, 'data' => 'hello']);
46
        $this->assertSame('/test/id/100', $uri->getPath());
47
        $this->assertSame('data=hello', $uri->getQuery());
48
    }
49
50
    public function testDirect(): void
51
    {
52
        $router = $this->makeRouter();
53
        $router->setRoute(
54
            'group',
55
            new Route('/<controller>[/<action>[/<id>]]', new Group([
56
                'test' => TestController::class,
57
            ]))
58
        );
59
60
        $uri = $router->getRoute('group')->uri(['test', 'id', 100]);
61
        $this->assertSame('/test/id/100', $uri->getPath());
62
    }
63
64
    public function testSlug(): void
65
    {
66
        $router = $this->makeRouter();
67
        $router->setRoute(
68
            'group',
69
            new Route('/<controller>[/<action>[/<id>[-<title>]]]', new Group([
70
                'test' => TestController::class,
71
            ]))
72
        );
73
74
        $uri = $router->getRoute('group')->uri(['test', 'id', 100, 'Hello World']);
75
        $this->assertSame('/test/id/100-hello-world', $uri->getPath());
76
    }
77
78
    public function testSlugDefault(): void
79
    {
80
        $router = $this->makeRouter();
81
        $router->setDefault(
82
            new Route('/<controller>[/<action>[/<id>[-<title>]]]', new Group([
83
                'test' => TestController::class,
84
            ]))
85
        );
86
87
        $uri = $router->uri('test:id', ['id' => 100, 'title' => 'Hello World']);
88
        $this->assertSame('/test/id/100-hello-world', $uri->getPath());
89
    }
90
91
    public function testSlugNoDefault(): void
92
    {
93
        $this->expectException(UndefinedRouteException::class);
94
95
        $router = $this->makeRouter();
96
97
        $uri = $router->uri('test:id', ['id' => 100, 'title' => 'Hello World']);
98
        $this->assertSame('/test/id/100-hello-world', $uri->getPath());
99
    }
100
}
101