This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExamplePresenterTest.php
204 lines (181 loc) · 5.99 KB
/
ExamplePresenterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php declare(strict_types = 1);
namespace Wavevision\NetteTests\TestAppTests\Presenters;
use Nette\Application\Responses\JsonResponse;
use Nette\Application\Responses\VoidResponse;
use Nette\Http\IRequest;
use PHPUnit\Framework\AssertionFailedError;
use Wavevision\NetteTests\InvalidState;
use Wavevision\NetteTests\Mocks\InjectRequestMock;
use Wavevision\NetteTests\Runners\PresenterRequest;
use Wavevision\NetteTests\Runners\PresenterResponse;
use Wavevision\NetteTests\TestApp\Models\BrokenSignal;
use Wavevision\NetteTests\TestApp\Presenters\ExamplePresenter;
use Wavevision\NetteTests\TestCases\PresenterTestCase;
class ExamplePresenterTest extends PresenterTestCase
{
use InjectRequestMock;
public function testFlashMessage(): void
{
$this->assertSame(
[
[
'message' => 'Hello there!',
'type' => 'warning',
],
],
$this->extractFlashMessages(
$this->runPresenter(new PresenterRequest(ExamplePresenter::class, 'flash'))
)
);
}
public function testAjaxRequest(): void
{
$this->assertInstanceOf(
VoidResponse::class,
$this->runPresenter(new PresenterRequest(ExamplePresenter::class, 'ajax'))->getResponse()
);
$this->assertTrue(
$this->extractJsonResponsePayload(
$this->runPresenter((new PresenterRequest(ExamplePresenter::class, 'ajax'))->setAjax(true))
)
);
}
public function testTextResponse(): void
{
$this->assertStringContainsString(
'Hello there!',
$this->extractTextResponseContent(
$this->runPresenter(new PresenterRequest(ExamplePresenter::class, 'textResponse'))
)
);
}
public function testJsonResponse(): void
{
$this->assertEquals(
[
'id' => 1,
'post' => ['42'],
],
$this->extractJsonResponsePayload(
$this->runPresenter(
new PresenterRequest(
ExamplePresenter::class,
'jsonResponse',
['id' => 1],
IRequest::POST,
['42']
)
)
)
);
}
public function testRedirectResponse(): void
{
$this->assertEquals(
'https://9gag.com',
$this->extractRedirectResponseUrl(
$this->runPresenter(new PresenterRequest(ExamplePresenter::class, 'redirectResponse'))
)
);
}
public function testFileResponse(): void
{
$this->assertStringContainsString(
'Presenters/file.txt',
$this->extractFileResponseFile(
$this->runPresenter(new PresenterRequest(ExamplePresenter::class, 'fileResponse'))
)
);
}
public function testForwardResponse(): void
{
$request = $this->extractForwardResponseRequest(
$this->runPresenter(new PresenterRequest(ExamplePresenter::class, 'forwardResponse'))
);
$this->assertEquals('Example', $request->getPresenterName());
}
public function testSignal(): void
{
$presenterRequest = (new PresenterRequest(ExamplePresenter::class))->setSignal('brokenSignal');
$this->expectException(BrokenSignal::class);
$this->runPresenter($presenterRequest);
}
public function testNonExistingPresenter(): void
{
$this->expectException(InvalidState::class);
$this->expectExceptionMessage("Presenter not found for class 'NotPresenter'.");
$this->runPresenter((new PresenterRequest('NotPresenter')));
}
public function testBeforeRunCallback(): void
{
$called = false;
$presenterRequest = (new PresenterRequest(ExamplePresenter::class))->addBeforeRunCallback(
function (PresenterRequest $presenterRequest) use (&$called): void {
$called = true;
$this->assertInstanceOf(ExamplePresenter::class, $presenterRequest->getPresenter());
$this->assertEquals('Example', $presenterRequest->getPresenterName());
}
);
$this->runPresenter($presenterRequest);
$this->assertTrue($called);
}
public function testNotTextResponse(): void
{
$presenterResponse = $this->getTerminatePresenterResponse();
$this->expectExceptionMessage("Invalid response type 'Nette\Application\Responses\TextResponse' was expected.");
$this->extractTextResponse($presenterResponse);
}
public function testNotJsonResponse(): void
{
$presenterResponse = $this->getTerminatePresenterResponse();
$this->expectExceptionMessage("Invalid response type 'Nette\Application\Responses\JsonResponse' was expected.");
$this->extractJsonResponse($presenterResponse);
}
public function testNotRedirectResponse(): void
{
$presenterResponse = $this->getTerminatePresenterResponse();
$this->expectExceptionMessage(
"Invalid response type 'Nette\Application\Responses\RedirectResponse' was expected."
);
$this->extractRedirectResponse($presenterResponse);
}
public function testNotFileResponse(): void
{
$presenterResponse = $this->getTerminatePresenterResponse();
$this->expectExceptionMessage(
"Invalid response type 'Nette\Application\Responses\FileResponse' was expected."
);
$this->extractFileResponse($presenterResponse);
}
public function testNotForwardResponse(): void
{
$presenterResponse = $this->getTerminatePresenterResponse();
$this->expectExceptionMessage(
"Invalid response type 'Nette\Application\Responses\ForwardResponse' was expected."
);
$this->extractForwardResponse($presenterResponse);
}
public function testQueryMock(): void
{
$presenterRequest = new PresenterRequest(ExamplePresenter::class, 'queryMock', ['q' => '42']);
$presenterResponse = $this->runPresenter($presenterRequest);
$this->assertInstanceOf(VoidResponse::class, $presenterResponse->getResponse());
}
public function testHeadersMock(): void
{
$headers = ['4' => '2'];
$this->requestMock->setHeaderMock($headers);
$presenterRequest = new PresenterRequest(ExamplePresenter::class, 'headersMock');
$presenterResponse = $this->runPresenter($presenterRequest);
$response = $presenterResponse->getResponse();
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals($headers, $response->getPayload());
}
private function getTerminatePresenterResponse(): PresenterResponse
{
$presenterRequest = new PresenterRequest(ExamplePresenter::class, 'terminateResponse');
$presenterResponse = $this->runPresenter($presenterRequest);
$this->expectException(AssertionFailedError::class);
return $presenterResponse;
}
}