Passed
Push — master ( edea3d...0ae37f )
by Alexander
01:22
created

FastRouteTest::testGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.8666
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+}#{tag:\w+}')->name('view'),
42
        ];
43
        $routerCollector = $this->createRouterCollector($routes);
44
45
        $url = $routerCollector->generate('view', ['id' => 100, 'tag' => 'yii']);
46
47
        $this->assertEquals('/view/100#yii', $url);
48
    }
49
50
    public function testRouteWithoutParameters(): void
51
    {
52
        $routes = [
53
            Route::get('/view/{id:\d+}#{tag:\w+}')->name('view'),
54
        ];
55
        $routerCollector = $this->createRouterCollector($routes);
56
57
        $this->expectException(\RuntimeException::class);
58
        $routerCollector->generate('view');
59
    }
60
61
    /**
62
     * @param array $routes
63
     * @return RouterInterface
64
     */
65
    private function createRouterCollector(array $routes): RouterInterface
66
    {
67
        $container = new DummyContainer();
68
        $factory = new RouteFactory();
69
70
        return $factory($routes, $container);
71
    }
72
73
    public function testGroup(): void
74
    {
75
        $routes = [
76
            Route::get('/home/index')->name('index'),
77
            ['/api', static function (RouteCollectorInterface $r) {
78
                $r->addRoute(Route::get('/post')->name('post/index'));
79
                $r->addRoute(Route::get('/post/{id}')->name('post/view'));
80
            }],
81
        ];
82
        $routerCollector = $this->createRouterCollector($routes);
83
84
        $url = $routerCollector->generate('index');
85
        $this->assertEquals('/home/index', $url);
86
87
        $url = $routerCollector->generate('post/index');
88
        $this->assertEquals('/api/post', $url);
89
90
        $url = $routerCollector->generate('post/view', ['id' => 42]);
91
        $this->assertEquals('/api/post/42', $url);
92
    }
93
}
94