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

GetCallbackUnitTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
c 0
b 0
f 0
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCallback() 0 17 2
A testGetCallbackWithUnexistingRoute() 0 13 1
A getCallbackDataProvider() 0 14 1
1
<?php
2
namespace Mezon\Router\Tests\Standart;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Router\Router;
6
use Mezon\Router\Tests\Base\RouterUnitTestUtils;
7
8
/**
9
 *
10
 * @psalm-suppress PropertyNotSetInConstructor
11
 */
12
class GetCallbackUnitTest extends TestCase
13
{
14
15
    /**
16
     * Data provider for the
17
     *
18
     * @return array testing dataset
19
     */
20
    public function getCallbackDataProvider(): array
21
    {
22
        return [
23
            [
24
                'some-static-route',
25
                'some-static-route'
26
            ],
27
            [
28
                'some/non-static/route/[i:id]',
29
                'some/non-static/route/1'
30
            ],
31
            [
32
                '*',
33
                'unexisting-route'
34
            ]
35
        ];
36
    }
37
38
    /**
39
     * Testing getting callback
40
     *
41
     * @param string $route
42
     *            route
43
     * @param string $url
44
     *            concrete URL
45
     * @dataProvider getCallbackDataProvider
46
     */
47
    public function testGetCallback(string $route, string $url): void
48
    {
49
        // setup
50
        RouterUnitTestUtils::setRequestMethod('GET');
51
        $router = new Router();
52
        $router->addRoute($route, function () {
53
            return 'route result';
54
        });
55
56
        // test body
57
        $callback = $router->getCallback($url);
58
59
        // assertions
60
        if (is_callable($callback)) {
61
            $this->assertEquals('route result', $callback());
62
        } else {
63
            $this->fail();
64
        }
65
    }
66
67
    /**
68
     * Testing case with unexisting callback
69
     */
70
    public function testGetCallbackWithUnexistingRoute(): void
71
    {
72
        // setup
73
        $router = new Router();
74
        $router->addRoute('existing-route', function () {
75
            return 'existing route result';
76
        });
77
78
        // assertions
79
        $this->expectException(\Exception::class);
80
81
        // test body
82
        $router->getCallback('unexisting-route');
83
    }
84
}
85