Skip to content

Commit 5c84a11

Browse files
committed
tests: added test for Application
1 parent 2ddc3cc commit 5c84a11

1 file changed

Lines changed: 254 additions & 0 deletions

File tree

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
<?php
2+
3+
/**
4+
* Test: Application
5+
*/
6+
7+
use Nette\Application\Application;
8+
use Nette\Application\BadRequestException;
9+
use Nette\Application\IPresenterFactory;
10+
use Nette\Application\IRouter;
11+
use Nette\Application\Request;
12+
use Tester\Assert;
13+
14+
15+
require __DIR__ . '/../bootstrap.php';
16+
17+
18+
class GoodPresenter implements Nette\Application\IPresenter
19+
{
20+
public $request;
21+
22+
function run(Request $request)
23+
{
24+
$this->request = $request;
25+
}
26+
}
27+
28+
29+
class BadException extends Exception
30+
{
31+
}
32+
33+
34+
class BadPresenter implements Nette\Application\IPresenter
35+
{
36+
function run(Request $request)
37+
{
38+
throw new BadException;
39+
}
40+
}
41+
42+
43+
class ErrorPresenter implements Nette\Application\IPresenter
44+
{
45+
public $request;
46+
47+
function run(Request $request)
48+
{
49+
$this->request = $request;
50+
}
51+
}
52+
53+
54+
$httpRequest = Mockery::mock(Nette\Http\IRequest::class);
55+
$httpResponse = Mockery::mock(Nette\Http\IResponse::class);
56+
$httpResponse->shouldIgnoreMissing();
57+
58+
59+
// no route without error presenter
60+
Assert::exception(function () use ($httpRequest, $httpResponse) {
61+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
62+
$router = Mockery::mock(IRouter::class);
63+
$router->shouldReceive('match');
64+
65+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
66+
$app->run();
67+
}, BadRequestException::class, 'No route for HTTP request.');
68+
69+
70+
// no route with error presenter
71+
test(function () use ($httpRequest, $httpResponse) {
72+
$errorPresenter = new ErrorPresenter;
73+
74+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
75+
$presenterFactory->shouldReceive('createPresenter')->with('Error')->andReturn($errorPresenter);
76+
77+
$router = Mockery::mock(IRouter::class);
78+
$router->shouldReceive('match');
79+
80+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
81+
$app->catchExceptions = TRUE;
82+
$app->errorPresenter = 'Error';
83+
$app->run();
84+
85+
$requests = $app->getRequests();
86+
Assert::count(1, $requests);
87+
Assert::same(Request::FORWARD, $requests[0]->getMethod());
88+
Assert::same('Error', $requests[0]->getPresenterName());
89+
90+
Assert::equal($requests[0], $errorPresenter->request);
91+
Assert::null($errorPresenter->request->getParameter('request'));
92+
Assert::type(BadRequestException::class, $errorPresenter->request->getParameter('exception'));
93+
});
94+
95+
96+
// route to error presenter
97+
test(function () use ($httpRequest, $httpResponse) {
98+
$errorPresenter = new ErrorPresenter;
99+
100+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
101+
$presenterFactory->shouldReceive('createPresenter')->with('Error')->andReturn($errorPresenter);
102+
103+
$router = Mockery::mock(IRouter::class);
104+
$router->shouldReceive('match')->andReturn(new Request('Error', 'GET'));
105+
106+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
107+
$app->catchExceptions = TRUE;
108+
$app->errorPresenter = 'Error';
109+
$app->run();
110+
111+
$requests = $app->getRequests();
112+
Assert::count(1, $requests);
113+
Assert::same(Request::FORWARD, $requests[0]->getMethod());
114+
Assert::same('Error', $requests[0]->getPresenterName());
115+
116+
Assert::equal($requests[0], $errorPresenter->request);
117+
Assert::null($errorPresenter->request->getParameter('request'));
118+
Assert::type(BadRequestException::class, $errorPresenter->request->getParameter('exception'));
119+
});
120+
121+
122+
// missing presenter without error presenter
123+
Assert::exception(function () use ($httpRequest, $httpResponse) {
124+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
125+
$presenterFactory->shouldReceive('getPresenterClass')->with('Missing')->andThrow(Nette\Application\InvalidPresenterException::class);
126+
127+
$router = Mockery::mock(IRouter::class);
128+
$router->shouldReceive('match')->andReturn(new Request('Missing', 'GET'));
129+
130+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
131+
$app->run();
132+
}, BadRequestException::class);
133+
134+
135+
// missing presenter with error presenter
136+
test(function () use ($httpRequest, $httpResponse) {
137+
$errorPresenter = new ErrorPresenter;
138+
139+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
140+
$presenterFactory->shouldReceive('getPresenterClass')->with('Missing')->andThrow(Nette\Application\InvalidPresenterException::class);
141+
$presenterFactory->shouldReceive('createPresenter')->with('Error')->andReturn($errorPresenter);
142+
143+
$router = Mockery::mock(IRouter::class);
144+
$router->shouldReceive('match')->andReturn(new Request('Missing', 'GET'));
145+
146+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
147+
$app->catchExceptions = TRUE;
148+
$app->errorPresenter = 'Error';
149+
$app->run();
150+
151+
$requests = $app->getRequests();
152+
Assert::count(1, $requests);
153+
Assert::same(Request::FORWARD, $requests[0]->getMethod());
154+
Assert::same('Error', $requests[0]->getPresenterName());
155+
156+
Assert::equal($requests[0], $errorPresenter->request);
157+
Assert::null($errorPresenter->request->getParameter('request'));
158+
Assert::type(BadRequestException::class, $errorPresenter->request->getParameter('exception'));
159+
});
160+
161+
162+
// presenter error without error presenter
163+
Assert::exception(function () use ($httpRequest, $httpResponse) {
164+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
165+
$presenterFactory->shouldReceive('getPresenterClass');
166+
$presenterFactory->shouldReceive('createPresenter')->with('Bad')->andReturn(new BadPresenter);
167+
168+
$router = Mockery::mock(IRouter::class);
169+
$router->shouldReceive('match')->andReturn(new Request('Bad', 'GET'));
170+
171+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
172+
$app->run();
173+
}, BadException::class);
174+
175+
176+
// presenter error with error presenter
177+
test(function () use ($httpRequest, $httpResponse) {
178+
$errorPresenter = new ErrorPresenter;
179+
180+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
181+
$presenterFactory->shouldReceive('getPresenterClass');
182+
$presenterFactory->shouldReceive('createPresenter')->with('Bad')->andReturn(new BadPresenter);
183+
$presenterFactory->shouldReceive('createPresenter')->with('Error')->andReturn($errorPresenter);
184+
185+
$router = Mockery::mock(IRouter::class);
186+
$router->shouldReceive('match')->andReturn(new Request('Bad', 'GET'));
187+
188+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
189+
$app->catchExceptions = TRUE;
190+
$app->errorPresenter = 'Error';
191+
$app->run();
192+
193+
$requests = $app->getRequests();
194+
Assert::count(2, $requests);
195+
Assert::same('GET', $requests[0]->getMethod());
196+
Assert::same('Bad', $requests[0]->getPresenterName());
197+
Assert::same(Request::FORWARD, $requests[1]->getMethod());
198+
Assert::same('Error', $requests[1]->getPresenterName());
199+
200+
Assert::equal($requests[1], $errorPresenter->request);
201+
Assert::equal($requests[0], $errorPresenter->request->getParameter('request'));
202+
Assert::type(BadException::class, $errorPresenter->request->getParameter('exception'));
203+
});
204+
205+
206+
// no error without error presenter
207+
Assert::noError(function () use ($httpRequest, $httpResponse) {
208+
$presenter = new GoodPresenter;
209+
210+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
211+
$presenterFactory->shouldReceive('getPresenterClass');
212+
$presenterFactory->shouldReceive('createPresenter')->with('Good')->andReturn($presenter);
213+
214+
$router = Mockery::mock(IRouter::class);
215+
$router->shouldReceive('match')->andReturn(new Request('Good', 'GET'));
216+
217+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
218+
$app->run();
219+
220+
$requests = $app->getRequests();
221+
Assert::count(1, $requests);
222+
Assert::same('GET', $requests[0]->getMethod());
223+
Assert::same('Good', $requests[0]->getPresenterName());
224+
225+
Assert::equal($requests[0], $presenter->request);
226+
});
227+
228+
229+
// no error with error presenter
230+
Assert::noError(function () use ($httpRequest, $httpResponse) {
231+
$presenter = new GoodPresenter;
232+
$errorPresenter = new ErrorPresenter;
233+
234+
$presenterFactory = Mockery::mock(IPresenterFactory::class);
235+
$presenterFactory->shouldReceive('getPresenterClass');
236+
$presenterFactory->shouldReceive('createPresenter')->with('Good')->andReturn($presenter);
237+
$presenterFactory->shouldReceive('createPresenter')->with('Error')->andReturn($errorPresenter);
238+
239+
$router = Mockery::mock(IRouter::class);
240+
$router->shouldReceive('match')->andReturn(new Request('Good', 'GET'));
241+
242+
$app = new Application($presenterFactory, $router, $httpRequest, $httpResponse);
243+
$app->catchExceptions = TRUE;
244+
$app->errorPresenter = 'Error';
245+
$app->run();
246+
247+
$requests = $app->getRequests();
248+
Assert::count(1, $requests);
249+
Assert::same('GET', $requests[0]->getMethod());
250+
Assert::same('Good', $requests[0]->getPresenterName());
251+
252+
Assert::equal($requests[0], $presenter->request);
253+
Assert::null($errorPresenter->request);
254+
});

0 commit comments

Comments
 (0)