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