Passed
Push — master ( 814c96...c5a3b1 )
by Alexander
13:20
created

FastRouteTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 53
c 4
b 0
f 0
dl 0
loc 114
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testSimpleNamedRoute() 0 10 1
A testRouteWithoutNameNotFound() 0 11 1
A testRouteWithParameters() 0 10 1
A testMissedParam() 0 9 1
A testRouteWithoutParameters() 0 9 1
A testGroup() 0 19 1
A testParamPattern() 0 9 1
A testMissedParams() 0 9 1
A createRouterCollector() 0 6 1
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\RouterInterface;
10
11
class FastRouteTest extends TestCase
12
{
13
    public function testSimpleNamedRoute(): void
14
    {
15
        $routes = [
16
            Route::get('/home/index')->name('index'),
17
        ];
18
        $routerCollector = $this->createRouterCollector($routes);
19
20
        $url = $routerCollector->generate('index');
21
22
        $this->assertEquals('/home/index', $url);
23
    }
24
25
    public function testRouteWithoutNameNotFound(): void
26
    {
27
        $routes = [
28
            Route::get('/home/index'),
29
            Route::get('/index'),
30
            Route::get('index'),
31
        ];
32
        $routerCollector = $this->createRouterCollector($routes);
33
34
        $this->expectException(RouteNotFoundException::class);
35
        $routerCollector->generate('index');
36
    }
37
38
    public function testRouteWithParameters(): void
39
    {
40
        $routes = [
41
            Route::get('/view/{id:\d+}/{text:~[\w]+}#{tag:\w+}')->name('view'),
42
        ];
43
        $routerCollector = $this->createRouterCollector($routes);
44
45
        $url = $routerCollector->generate('view', ['id' => 100, 'tag' => 'yii', 'text' => '~test']);
46
47
        $this->assertEquals('/view/100/~test#yii', $url);
48
    }
49
50
    public function testParamPattern(): void
51
    {
52
        $routes = [
53
            Route::get('/view/{id:\w+}')->name('view'),
54
        ];
55
        $routerCollector = $this->createRouterCollector($routes);
56
57
        $this->expectExceptionMessage('Parameter value for [id] did not match the regex `\w+`');
58
        $routerCollector->generate('view', ['id' => null]);
59
    }
60
61
    public function testMissedParams(): void
62
    {
63
        $routes = [
64
            Route::get('/view/{id:\w+}')->name('view'),
65
        ];
66
        $routerCollector = $this->createRouterCollector($routes);
67
68
        $this->expectExceptionMessage('Route `view` expects at least parameter values for [id], but received []');
69
        $routerCollector->generate('view');
70
    }
71
72
    public function testMissedParam(): void
73
    {
74
        $routes = [
75
            Route::get('/view/{id:\d+}/{value}')->name('view'),
76
        ];
77
        $routerCollector = $this->createRouterCollector($routes);
78
79
        $this->expectExceptionMessage('Route `view` expects at least parameter values for [id,value], but received [id]');
80
        $routerCollector->generate('view', ['id' => 123]);
81
    }
82
83
    public function testRouteWithoutParameters(): void
84
    {
85
        $routes = [
86
            Route::get('/view/{id:\d+}#{tag:\w+}')->name('view'),
87
        ];
88
        $routerCollector = $this->createRouterCollector($routes);
89
90
        $this->expectException(\RuntimeException::class);
91
        $routerCollector->generate('view');
92
    }
93
94
    /**
95
     * @param array $routes
96
     * @return RouterInterface
97
     */
98
    private function createRouterCollector(array $routes): RouterInterface
99
    {
100
        $container = new DummyContainer();
101
        $factory = new RouteFactory();
102
103
        return $factory($routes, $container);
104
    }
105
106
    public function testGroup(): void
107
    {
108
        $routes = [
109
            Route::get('/home/index')->name('index'),
110
            ['/api', static function (RouteCollectorInterface $r) {
111
                $r->addRoute(Route::get('/post')->name('post/index'));
112
                $r->addRoute(Route::get('/post/{id}')->name('post/view'));
113
            }],
114
        ];
115
        $routerCollector = $this->createRouterCollector($routes);
116
117
        $url = $routerCollector->generate('index');
118
        $this->assertEquals('/home/index', $url);
119
120
        $url = $routerCollector->generate('post/index');
121
        $this->assertEquals('/api/post', $url);
122
123
        $url = $routerCollector->generate('post/view', ['id' => 42]);
124
        $this->assertEquals('/api/post/42', $url);
125
    }
126
}
127