1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Mezon\Transport\HttpRequestParams; |
4
|
|
|
use Mezon\Router\Router; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Controller class for testing purposes |
8
|
|
|
* |
9
|
|
|
* @author Dodonov A.A. |
10
|
|
|
*/ |
11
|
|
|
class TestingController extends \Mezon\Application\Controller |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public function controllerTest(): string |
15
|
|
|
{ |
16
|
|
|
return 'computed content'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function controllerTest2(): string |
20
|
|
|
{ |
21
|
|
|
return 'computed content 2'; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
class ControllerUnitTest extends \PHPUnit\Framework\TestCase |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Testing constructor |
30
|
|
|
*/ |
31
|
|
|
public function testConstructor(): void |
32
|
|
|
{ |
33
|
|
|
// setupp |
34
|
|
|
$controller = new TestingController('Test'); |
35
|
|
|
|
36
|
|
|
$this->assertEquals('Test', $controller->getControllerName(), 'Invalid constructor call'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Testing render |
41
|
|
|
*/ |
42
|
|
|
public function testRender(): void |
43
|
|
|
{ |
44
|
|
|
// setupp |
45
|
|
|
$controller = new TestingController('Test'); |
46
|
|
|
|
47
|
|
|
$this->assertEquals('computed content', $controller->run(), 'Invalid controller execution'); |
48
|
|
|
$this->assertEquals('computed content 2', $controller->run('test2'), 'Invalid controller execution'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Testing default controller |
53
|
|
|
*/ |
54
|
|
|
public function testDefault(): void |
55
|
|
|
{ |
56
|
|
|
// setupp |
57
|
|
|
$controller = new TestingController(); |
58
|
|
|
|
59
|
|
|
// assertionss |
60
|
|
|
$this->expectExceptionMessage('Controller Default was not found'); |
61
|
|
|
|
62
|
|
|
// test bodyy |
63
|
|
|
$controller->run(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Method tests buildRoute |
68
|
|
|
*/ |
69
|
|
|
public function testBuildRoute(): void |
70
|
|
|
{ |
71
|
|
|
// setup |
72
|
|
|
$controller = new TestingController(); |
73
|
|
|
|
74
|
|
|
// test body |
75
|
|
|
$route = $controller->buildRoute('/test/', 'POST', 'controllerTest'); |
76
|
|
|
|
77
|
|
|
// assertions |
78
|
|
|
$this->assertEquals('/test/', $route['route']); |
79
|
|
|
$this->assertEquals('POST', $route['method']); |
80
|
|
|
$this->assertInstanceOf(TestingController::class, $route['callback'][0]); |
81
|
|
|
$this->assertEquals('controllerTest', $route['callback'][1]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Testing method setControllerName |
86
|
|
|
*/ |
87
|
|
|
public function testSetControllerName(): void |
88
|
|
|
{ |
89
|
|
|
// setup |
90
|
|
|
$controller = new TestingController(); |
91
|
|
|
|
92
|
|
|
// test body |
93
|
|
|
$controller->setControllerName('SomeName'); |
94
|
|
|
|
95
|
|
|
// assertions |
96
|
|
|
$this->assertEquals('SomeName', $controller->getControllerName()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Testing method getParamsFetcher |
101
|
|
|
*/ |
102
|
|
|
public function testGetParamsFetcher(): void |
103
|
|
|
{ |
104
|
|
|
// setup |
105
|
|
|
$router = new Router(); |
106
|
|
|
$controller = new TestingController('', new HttpRequestParams($router)); |
107
|
|
|
|
108
|
|
|
// test body |
109
|
|
|
$fetcher = $controller->getParamsFetcher(); |
110
|
|
|
|
111
|
|
|
// assertions |
112
|
|
|
$this->assertInstanceOf(HttpRequestParams::class, $fetcher); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Testing method getParamsFetcher with exception |
117
|
|
|
*/ |
118
|
|
|
public function testGetParamsFetcherWithException() : void |
119
|
|
|
{ |
120
|
|
|
// setup |
121
|
|
|
$controller = new TestingController(); |
122
|
|
|
|
123
|
|
|
// assertions |
124
|
|
|
$this->expectException(\Exception::class); |
125
|
|
|
|
126
|
|
|
// test body |
127
|
|
|
$controller->getParamsFetcher(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|