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