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