Passed
Push — master ( 0ee5ba...9ecbd5 )
by Alex
06:47
created

ReverseRouteUnitTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

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

3 Methods

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