Passed
Pull Request — master (#18)
by Rustam
01:28
created

testSimpleRouteShouldBeGenerated()   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\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
            Group::create('/api', [
81
                Route::get('/post')->name('post/index'),
82
                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 testDefaultShouldNotBeUsedForOptionalParameter(): void
95
    {
96
        $routes = [
97
            Route::get('/[{name}]')
98
                ->name('defaults')
99
                ->defaults(['name' => 'default'])
100
        ];
101
102
        $url = $this->createUrlGenerator($routes)->generate('defaults');
103
        $this->assertEquals('/', $url);
104
    }
105
106
    public function testValueShouldBeUsedForOptionalParameter(): void
107
    {
108
        $routes = [
109
            Route::get('/[{name}]')
110
                ->name('defaults')
111
                ->defaults(['name' => 'default'])
112
        ];
113
114
        $url = $this->createUrlGenerator($routes)->generate('defaults', ['name' => 'test']);
115
        $this->assertEquals('/test', $url);
116
    }
117
118
    public function testDefaultShouldNotBeUsedForRequiredParameter(): void
119
    {
120
        $routes = [
121
            Route::get('/{name}')
122
                ->name('defaults')
123
                ->defaults(['name' => 'default'])
124
        ];
125
126
        $this->expectExceptionMessage('Route `defaults` expects at least parameter values for [name], but received []');
127
        $this->createUrlGenerator($routes)->generate('defaults');
128
    }
129
}
130