Passed
Push — master ( c5a3b1...3a525b )
by Alexander
01:15
created

exceptionShouldBeThrownIfParameterPatternDoesntMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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