Passed
Push — master ( 4cfdc5...37c7d0 )
by Alex
07:00
created

ReverseRouteUnitTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 21
c 1
b 0
f 1
dl 0
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testReverseRouteByName() 0 13 1
A reverseRouteByNameDataProvider() 0 26 1
A testFetchUnexistingRouteByName() 0 10 1
1
<?php
2
namespace Mezon\Router\Tests\Simple;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Router\SimpleRouter;
6
7
/**
8
 * @psalm-suppress PropertyNotSetInConstructor
9
 */
10
class ReverseRouteUnitTest extends TestCase
11
{
12
13
    /**
14
     * Data provider for the test testReverseRouteByName
15
     *
16
     * @return array test data
17
     */
18
    public function reverseRouteByNameDataProvider(): array
19
    {
20
        return [
21
            [
22
                'named-route-url',
23
                [],
24
                'named-route-url'
25
            ],
26
            [
27
                'route-with-params/[i:id]',
28
                [
29
                    'id' => 123
30
                ],
31
                'route-with-params/123',
32
            ],
33
            [
34
                'route-with-foo/[i:id]',
35
                [
36
                    'foo' => 123
37
                ],
38
                'route-with-foo/[i:id]',
39
            ],
40
            [
41
                'route-no-params/[i:id]',
42
                [],
43
                'route-no-params/[i:id]',
44
            ]
45
        ];
46
    }
47
48
    /**
49
     * Testing reverse route by it's name
50
     *
51
     * @param string $route
52
     *            route
53
     * @param array $parameters
54
     *            parameters to be substituted
55
     * @param string $extectedResult
56
     *            quite obviuous to describe it here )
57
     * @dataProvider reverseRouteByNameDataProvider
58
     */
59
    public function testReverseRouteByName(string $route, array $parameters, string $extectedResult): void
60
    {
61
        // setup
62
        $router = new SimpleRouter();
63
        $router->addRoute($route, function () {
64
            return 'named route result';
65
        }, 'GET', 'named-route');
66
67
        // test body
68
        $url = $router->reverse('named-route', $parameters);
69
70
        // assertions
71
        $this->assertEquals($extectedResult, $url);
72
    }
73
74
    /**
75
     * Trying to fetch unexisting route by name
76
     */
77
    public function testFetchUnexistingRouteByName(): void
78
    {
79
        // setup
80
        $router = new SimpleRouter();
81
82
        // assertions
83
        $this->expectException(\Exception::class);
84
85
        // test body
86
        $router->getRouteByName('unexisting-name');
87
    }
88
}
89