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
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
11
|
|
|
*/ |
12
|
|
|
class FetchActionsUnitTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Testing action #1. |
17
|
|
|
*/ |
18
|
|
|
public function actionA1(): string |
19
|
|
|
{ |
20
|
|
|
return 'action #1'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Testing action #2. |
25
|
|
|
*/ |
26
|
|
|
public function actionA2(): string |
27
|
|
|
{ |
28
|
|
|
return 'action #2'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Two word action name |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function actionDoubleWord(): string |
37
|
|
|
{ |
38
|
|
|
return 'action double word'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Data provider for the test testClassAction |
43
|
|
|
* |
44
|
|
|
* @return array test data |
45
|
|
|
*/ |
46
|
|
|
public function classActionDataProvider(): array |
47
|
|
|
{ |
48
|
|
|
$testData = []; |
49
|
|
|
|
50
|
|
|
foreach ([ |
51
|
|
|
'GET', |
52
|
|
|
'POST' |
53
|
|
|
] as $requestMethod) { |
54
|
|
|
$testData[] = [ |
55
|
|
|
$requestMethod, |
56
|
|
|
'/a1/', |
57
|
|
|
'action #1' |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$testData[] = [ |
61
|
|
|
$requestMethod, |
62
|
|
|
'/a2/', |
63
|
|
|
'action #2' |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$testData[] = [ |
67
|
|
|
$requestMethod, |
68
|
|
|
'/double-word/', |
69
|
|
|
'action double word' |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $testData; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Method tests class actions |
78
|
|
|
* |
79
|
|
|
* @param string $requestMethod |
80
|
|
|
* request method |
81
|
|
|
* @param string $route |
82
|
|
|
* requesting route |
83
|
|
|
* @param string $expectedResult |
84
|
|
|
* expected result of route processing |
85
|
|
|
* @dataProvider classActionDataProvider |
86
|
|
|
*/ |
87
|
|
|
public function testClassAction(string $requestMethod, string $route, string $expectedResult): void |
88
|
|
|
{ |
89
|
|
|
// setup |
90
|
|
|
RouterUnitTestUtils::setRequestMethod($requestMethod); |
91
|
|
|
$router = new SimpleRouter(); |
92
|
|
|
|
93
|
|
|
// test body |
94
|
|
|
$router->fetchActions($this); |
95
|
|
|
|
96
|
|
|
// assertions |
97
|
|
|
$this->assertEquals($expectedResult, $router->callRoute($route)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Testing map for the fetchActions method |
102
|
|
|
*/ |
103
|
|
|
public function testFetchActionsWithMap(): void |
104
|
|
|
{ |
105
|
|
|
// setup |
106
|
|
|
$router = new SimpleRouter(); |
107
|
|
|
|
108
|
|
|
// test body |
109
|
|
|
$router->fetchActions($this, [ |
110
|
|
|
'A1' => 'GET', |
111
|
|
|
'A2' => 'POST', |
112
|
|
|
'DoubleWord' => [ |
113
|
|
|
'GET', |
114
|
|
|
'POST' |
115
|
|
|
] |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
// assertions #1 |
119
|
|
|
RouterUnitTestUtils::setRequestMethod('GET'); |
120
|
|
|
$this->assertEquals('action #1', $router->callRoute('a1')); |
121
|
|
|
|
122
|
|
|
// assertions #2 |
123
|
|
|
RouterUnitTestUtils::setRequestMethod('POST'); |
124
|
|
|
$this->assertEquals('action #2', $router->callRoute('a2')); |
125
|
|
|
|
126
|
|
|
// assertions #3 |
127
|
|
|
RouterUnitTestUtils::setRequestMethod('GET'); |
128
|
|
|
$this->assertEquals('action double word', $router->callRoute('double-word')); |
129
|
|
|
|
130
|
|
|
// assertions #4 |
131
|
|
|
RouterUnitTestUtils::setRequestMethod('POST'); |
132
|
|
|
$this->assertEquals('action double word', $router->callRoute('double-word')); |
133
|
|
|
|
134
|
|
|
// assertions #5 |
135
|
|
|
RouterUnitTestUtils::setRequestMethod('POST'); |
136
|
|
|
$this->expectException(\Exception::class); |
137
|
|
|
$router->callRoute('a1'); |
138
|
|
|
|
139
|
|
|
// assertions #6 |
140
|
|
|
RouterUnitTestUtils::setRequestMethod('GET'); |
141
|
|
|
$this->expectException(\Exception::class); |
142
|
|
|
$router->callRoute('a2'); |
143
|
|
|
|
144
|
|
|
// assertions #7 |
145
|
|
|
RouterUnitTestUtils::setRequestMethod('DELETE'); |
146
|
|
|
$this->expectException(\Exception::class); |
147
|
|
|
$router->callRoute('double-word'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|