Passed
Pull Request — master (#18)
by Alexander
01:16
created

UrlGeneratorTest::testParametersSubstituted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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