1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Application\Tests; |
3
|
|
|
|
4
|
|
|
use Mezon\Transport\HttpRequestParams; |
5
|
|
|
use Mezon\Router\Router; |
6
|
|
|
use Mezon\HtmlTemplate\HtmlTemplate; |
7
|
|
|
use Mezon\Application\ViewInterface; |
8
|
|
|
use Mezon\Application\View; |
9
|
|
|
|
10
|
|
|
class PresenterUnitTest extends \PHPUnit\Framework\TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Testing constructor |
15
|
|
|
*/ |
16
|
|
|
public function testConstructor(): void |
17
|
|
|
{ |
18
|
|
|
// setupp |
19
|
|
|
$presenter = new TestingPresenter(new TestingView(), 'Test'); |
20
|
|
|
|
21
|
|
|
$this->assertEquals('Test', $presenter->getPresenterName(), 'Invalid constructor call'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Testing render |
26
|
|
|
*/ |
27
|
|
|
public function testRender(): void |
28
|
|
|
{ |
29
|
|
|
// setupp |
30
|
|
|
$presenter = new TestingPresenter(new TestingView(), 'Test'); |
31
|
|
|
|
32
|
|
|
$this->assertEquals('computed content', $presenter->run(), 'Invalid controller execution'); |
33
|
|
|
$this->assertEquals('computed content 2', $presenter->run('test2'), 'Invalid controller execution'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Testing default controller |
38
|
|
|
*/ |
39
|
|
|
public function testDefault(): void |
40
|
|
|
{ |
41
|
|
|
// setupp |
42
|
|
|
$presenter = new TestingPresenter(new TestingView()); |
43
|
|
|
|
44
|
|
|
// assertionss |
45
|
|
|
$this->expectExceptionMessage('Presenter Default was not found'); |
46
|
|
|
|
47
|
|
|
// test bodyy |
48
|
|
|
$presenter->run(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Method tests buildRoute |
53
|
|
|
*/ |
54
|
|
|
public function testBuildRoute(): void |
55
|
|
|
{ |
56
|
|
|
// setup |
57
|
|
|
$presenter = new TestingPresenter(new TestingView()); |
58
|
|
|
|
59
|
|
|
// test body |
60
|
|
|
$route = $presenter->buildRoute('/test/', 'POST', 'controllerTest'); |
|
|
|
|
61
|
|
|
|
62
|
|
|
// assertions |
63
|
|
|
$this->assertEquals('/test/', $route['route']); |
64
|
|
|
$this->assertEquals('POST', $route['method']); |
65
|
|
|
$this->assertInstanceOf(TestingPresenter::class, $route['callback'][0]); |
66
|
|
|
$this->assertEquals('controllerTest', $route['callback'][1]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Testing method setPresenterName |
71
|
|
|
*/ |
72
|
|
|
public function testSetPresenterName(): void |
73
|
|
|
{ |
74
|
|
|
// setup |
75
|
|
|
$presenter = new TestingPresenter(new TestingView()); |
76
|
|
|
|
77
|
|
|
// test body |
78
|
|
|
$presenter->setPresenterName('SomeName'); |
79
|
|
|
|
80
|
|
|
// assertions |
81
|
|
|
$this->assertEquals('SomeName', $presenter->getPresenterName()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Testing method getRequestParamsFetcher |
86
|
|
|
*/ |
87
|
|
|
public function testGetParamsFetcher(): void |
88
|
|
|
{ |
89
|
|
|
// setup |
90
|
|
|
$router = new Router(); |
91
|
|
|
$presenter = new TestingPresenter(new TestingView(), '', new HttpRequestParams($router)); |
92
|
|
|
|
93
|
|
|
// test body |
94
|
|
|
$fetcher = $presenter->getRequestParamsFetcher(); |
95
|
|
|
|
96
|
|
|
// assertions |
97
|
|
|
$this->assertInstanceOf(HttpRequestParams::class, $fetcher); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Testing method getRequestParamsFetcher with exception |
102
|
|
|
*/ |
103
|
|
|
public function testGetParamsFetcherWithException(): void |
104
|
|
|
{ |
105
|
|
|
// setup |
106
|
|
|
$presenter = new TestingPresenter(new TestingView()); |
107
|
|
|
|
108
|
|
|
// assertions |
109
|
|
|
$this->expectException(\Exception::class); |
110
|
|
|
|
111
|
|
|
// test body |
112
|
|
|
$presenter->getRequestParamsFetcher(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Testing get/set method |
117
|
|
|
*/ |
118
|
|
|
public function testNullViewInPresenterConstructor(): void |
119
|
|
|
{ |
120
|
|
|
// setup |
121
|
|
|
$presenter = new TestingPresenter(null); |
122
|
|
|
|
123
|
|
|
// test body |
124
|
|
|
$presenter->setErrorCode(11); |
125
|
|
|
$presenter->setErrorMessage('msg1'); |
126
|
|
|
|
127
|
|
|
// assertions |
128
|
|
|
$this->assertEquals(11, $presenter->getErrorCode()); |
129
|
|
|
$this->assertEquals('msg1', $presenter->getErrorMessage()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Testing get/set method |
134
|
|
|
*/ |
135
|
|
|
public function testUnsetViewInPresenterConstructor(): void |
136
|
|
|
{ |
137
|
|
|
// setup |
138
|
|
|
$presenter = new TestingPresenter(); |
139
|
|
|
|
140
|
|
|
// test body |
141
|
|
|
$presenter->setErrorCode(12); |
142
|
|
|
$presenter->setErrorMessage('msg2'); |
143
|
|
|
|
144
|
|
|
// assertions |
145
|
|
|
$this->assertEquals(12, $presenter->getErrorCode()); |
146
|
|
|
$this->assertEquals('msg2', $presenter->getErrorMessage()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Testing get/set method |
151
|
|
|
*/ |
152
|
|
|
public function testGetSetDataWithRealTemplate(): void |
153
|
|
|
{ |
154
|
|
|
// setup |
155
|
|
|
$presenter = new TestingPresenter(new TestingView(new HtmlTemplate(__DIR__, 'index'))); |
156
|
|
|
|
157
|
|
|
// test body |
158
|
|
|
$presenter->setErrorCode(13); |
159
|
|
|
$presenter->setErrorMessage('msg3'); |
160
|
|
|
|
161
|
|
|
// assertions |
162
|
|
|
$this->assertEquals(13, $presenter->getErrorCode()); |
163
|
|
|
$this->assertEquals('msg3', $presenter->getErrorMessage()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Data provider for the test testSetViewParameter |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function setViewParemeterDataProvider(): array |
172
|
|
|
{ |
173
|
|
|
return [ |
174
|
|
|
[ |
175
|
|
|
new TestingView(new HtmlTemplate(__DIR__, 'index')), |
176
|
|
|
'val' |
177
|
|
|
], |
178
|
|
|
[ |
179
|
|
|
null, |
180
|
|
|
null |
181
|
|
|
] |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Testing method setViewParameter |
187
|
|
|
* |
188
|
|
|
* @param ?ViewInterface View |
189
|
|
|
* @param mixed $var expected variable name |
190
|
|
|
* @dataProvider setViewParemeterDataProvider |
191
|
|
|
*/ |
192
|
|
|
public function testSetViewParameter(?ViewInterface $view, $var): void |
193
|
|
|
{ |
194
|
|
|
// setup |
195
|
|
|
$presenter = new TestingPresenter($view); |
196
|
|
|
|
197
|
|
|
// test body |
198
|
|
|
$presenter->setViewParameter('var', 'val', true); |
199
|
|
|
|
200
|
|
|
// assertions |
201
|
|
|
$this->assertEquals($var, $presenter->getViewParameter('var')); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.