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