Passed
Push — master ( 24047f...143e9b )
by Alexander
01:52
created

testDefaultShouldNotBeUsedForRequiredParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Router\FastRoute\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Yiisoft\Router\Route;
7
use Yiisoft\Router\Group;
8
use Yiisoft\Router\RouteCollectorInterface;
9
use Yiisoft\Router\RouteNotFoundException;
10
use Yiisoft\Router\UrlGeneratorInterface;
11
12
class UrlGeneratorTest extends TestCase
13
{
14
    private function createUrlGenerator(array $routes): UrlGeneratorInterface
15
    {
16
        $container = new DummyContainer();
17
        $factory = new RouteFactory();
18
19
        return $factory($routes, $container);
20
    }
21
22
    public function testSimpleRouteShouldBeGenerated(): void
23
    {
24
        $routes = [
25
            Route::get('/home/index')->name('index'),
26
        ];
27
        $url = $this->createUrlGenerator($routes)->generate('index');
28
29
        $this->assertEquals('/home/index', $url);
30
    }
31
32
    public function testRouteWithoutNameShouldNotBeFound(): void
33
    {
34
        $routes = [
35
            Route::get('/home/index'),
36
            Route::get('/index'),
37
            Route::get('index'),
38
        ];
39
        $urlGenerator = $this->createUrlGenerator($routes);
40
41
        $this->expectException(RouteNotFoundException::class);
42
        $urlGenerator->generate('index');
43
    }
44
45
    public function testParametersShouldBeSubstituted(): void
46
    {
47
        $routes = [
48
            Route::get('/view/{id:\d+}/{text:~[\w]+}#{tag:\w+}')->name('view'),
49
        ];
50
        $url = $this->createUrlGenerator($routes)->generate('view', ['id' => 100, 'tag' => 'yii', 'text' => '~test']);
51
52
        $this->assertEquals('/view/100/~test#yii', $url);
53
    }
54
55
    public function testExceptionShouldBeThrownIfParameterPatternDoesntMatch(): void
56
    {
57
        $routes = [
58
            Route::get('/view/{id:\w+}')->name('view'),
59
        ];
60
        $urlGenerator = $this->createUrlGenerator($routes);
61
62
        $this->expectExceptionMessage('Parameter value for [id] did not match the regex `\w+`');
63
        $urlGenerator->generate('view', ['id' => null]);
64
    }
65
66
    public function testExceptionShouldBeThrownIfAnyParameterIsMissing(): void
67
    {
68
        $routes = [
69
            Route::get('/view/{id:\d+}/{value}')->name('view'),
70
        ];
71
        $urlGenerator = $this->createUrlGenerator($routes);
72
73
        $this->expectExceptionMessage('Route `view` expects at least parameter values for [id,value], but received [id]');
74
        $urlGenerator->generate('view', ['id' => 123]);
75
    }
76
77
    public function testGroupPrefixShouldBeAppended(): void
78
    {
79
        $routes = [
80
            ['/api', static function (RouteCollectorInterface $r) {
81
                $r->addRoute(Route::get('/post')->name('post/index'));
82
                $r->addRoute(Route::get('/post/{id}')->name('post/view'));
83
            }],
84
        ];
85
        $urlGenerator = $this->createUrlGenerator($routes);
86
87
        $url = $urlGenerator->generate('post/index');
88
        $this->assertEquals('/api/post', $url);
89
90
        $url = $urlGenerator->generate('post/view', ['id' => 42]);
91
        $this->assertEquals('/api/post/42', $url);
92
    }
93
94
    public function testNestedGroupsPrefixShouldBeAppended(): void
95
    {
96
        $routes = [
97
            Group::create('/api', [
98
                Group::create('/v1', [
99
                    Group::create('/blog', [
100
                        Route::get('/post')->name('api-v1-post/index'),
101
                        Route::get('/post/{id}')->name('api-v1-post/view'),
102
                    ]),
103
                ])
104
            ])
105
        ];
106
        $urlGenerator = $this->createUrlGenerator($routes);
107
108
        $url = $urlGenerator->generate('api-v1-post/index');
109
        $this->assertEquals('/api/v1/blog/post', $url);
110
111
        $url = $urlGenerator->generate('api-v1-post/view', ['id' => 42]);
112
        $this->assertEquals('/api/v1/blog/post/42', $url);
113
    }
114
115
    public function testDefaultSholdNotBeUsedForOptionalParameter(): void
116
    {
117
        $routes = [
118
            Route::get('/[{name}]')
119
                ->name('defaults')
120
                ->defaults(['name' => 'default'])
121
        ];
122
123
        $url = $this->createUrlGenerator($routes)->generate('defaults');
124
        $this->assertEquals('/', $url);
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function testValueShouldBeUsedForOptionalParameter(): void
131
    {
132
        $routes = [
133
            Route::get('/[{name}]')
134
                ->name('defaults')
135
                ->defaults(['name' => 'default'])
136
        ];
137
138
        $url = $this->createUrlGenerator($routes)->generate('defaults', ['name' => 'test']);
139
        $this->assertEquals('/test', $url);
140
    }
141
142
    public function testDefaultShouldNotBeUsedForRequiredParameter(): void
143
    {
144
        $routes = [
145
            Route::get('/{name}')
146
                ->name('defaults')
147
                ->defaults(['name' => 'default'])
148
        ];
149
150
        $this->expectExceptionMessage('Route `defaults` expects at least parameter values for [name], but received []');
151
        $this->createUrlGenerator($routes)->generate('defaults');
152
    }
153
}
154