/
IntegrationTestCaseTest.php
335 lines (290 loc) · 8.79 KB
/
IntegrationTestCaseTest.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\TestSuite;
use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\Network\Response;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\TestSuite\IntegrationTestCase;
use Cake\Test\Fixture\AssertIntegrationTestCase;
/**
* Self test of the IntegrationTestCase
*/
class IntegrationTestCaseTest extends IntegrationTestCase
{
/**
* Setup method
*
* @return void
*/
public function setUp()
{
parent::setUp();
Configure::write('App.namespace', 'TestApp');
Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
DispatcherFactory::clear();
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
}
/**
* Test building a request.
*
* @return void
*/
public function testRequestBuilding()
{
$this->configRequest([
'headers' => ['X-CSRF-Token' => 'abc123'],
'base' => '',
'webroot' => '/'
]);
$this->cookie('split_token', 'def345');
$this->session(['User' => ['id' => 1, 'username' => 'mark']]);
$request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$this->assertEquals('abc123', $request->header('X-CSRF-Token'));
$this->assertEquals('tasks/add', $request->url);
$this->assertEquals(['split_token' => 'def345'], $request->cookies);
$this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
}
/**
* Test sending get requests.
*
* @return void
*/
public function testGet()
{
$this->assertNull($this->_response);
$this->get('/request_action/test_request_action');
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Network\Response', $this->_response);
$this->assertEquals('This is a test', $this->_response->body());
}
/**
* Test sending requests stores references to controller/view/layout.
*
* @return void
*/
public function testRequestSetsProperties()
{
$this->post('/posts/index');
$this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
$this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
$this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
$this->assertTemplate('index');
$this->assertLayout('default');
$this->assertEquals('value', $this->viewVariable('test'));
}
/**
* Test array URLs
*
* @return void
*/
public function testArrayUrls()
{
$this->post(['controller' => 'Posts', 'action' => 'index']);
$this->assertEquals('value', $this->viewVariable('test'));
}
/**
* Test flash and cookie assertions
*
* @return void
*/
public function testFlashSessionAndCookieAsserts()
{
$this->post('/posts/index');
$this->assertSession('An error message', 'Flash.flash.message');
$this->assertCookie(1, 'remember_me');
}
/**
* Test error handling and error page rendering.
*
* @return void
*/
public function testPostAndErrorHandling()
{
$this->post('/request_action/error_method');
$this->assertResponseNotEmpty();
$this->assertResponseContains('Not there or here');
$this->assertResponseContains('<!DOCTYPE html>');
}
/**
* Test redirecting and integration tests.
*
* @return void
*/
public function testRedirect()
{
$this->post('/tests_apps/redirect_to');
$this->assertResponseSuccess();
$this->assertResponseCode(302);
}
/**
* Test redirecting and integration tests.
*
* @return void
*/
public function testRedirectPermanent()
{
$this->post('/tests_apps/redirect_to_permanent');
$this->assertResponseSuccess();
$this->assertResponseCode(301);
}
/**
* Test the responseOk status assertion
*
* @return void
*/
public function testAssertResponseStatusCodes()
{
$this->_response = new Response();
$this->_response->statusCode(200);
$this->assertResponseOk();
$this->_response->statusCode(201);
$this->assertResponseOk();
$this->_response->statusCode(204);
$this->assertResponseOk();
$this->_response->statusCode(202);
$this->assertResponseSuccess();
$this->_response->statusCode(302);
$this->assertResponseSuccess();
$this->_response->statusCode(400);
$this->assertResponseError();
$this->_response->statusCode(417);
$this->assertResponseError();
$this->_response->statusCode(500);
$this->assertResponseFailure();
$this->_response->statusCode(505);
$this->assertResponseFailure();
$this->_response->statusCode(301);
$this->assertResponseCode(301);
}
/**
* Test the location header assertion.
*
* @return void
*/
public function testAssertRedirect()
{
$this->_response = new Response();
$this->_response->header('Location', 'http://localhost/tasks/index');
$this->assertRedirect();
$this->assertRedirect('/tasks/index');
$this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
$this->assertResponseEmpty();
}
/**
* Test the location header assertion.
*
* @return void
*/
public function testAssertNoRedirect()
{
$this->_response = new Response();
$this->assertNoRedirect();
}
/**
* Test the location header assertion.
*
* @return void
*/
public function testAssertNoRedirectFail()
{
$test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
$result = $test->run();
ob_start();
$this->assertFalse($result->wasSuccessful());
$this->assertEquals(1, $result->failureCount());
}
/**
* Test the location header assertion string contains
*
* @return void
*/
public function testAssertRedirectContains()
{
$this->_response = new Response();
$this->_response->header('Location', 'http://localhost/tasks/index');
$this->assertRedirectContains('/tasks/index');
}
/**
* Test the header assertion.
*
* @return void
*/
public function testAssertHeader()
{
$this->_response = new Response();
$this->_response->header('Etag', 'abc123');
$this->assertHeader('Etag', 'abc123');
}
/**
* Test the content type assertion.
*
* @return void
*/
public function testAssertContentType()
{
$this->_response = new Response();
$this->_response->type('json');
$this->assertContentType('json');
$this->assertContentType('application/json');
}
/**
* Test the content assertion.
*
* @return void
*/
public function testAssertResponseContains()
{
$this->_response = new Response();
$this->_response->body('Some content');
$this->assertResponseContains('content');
}
/**
* Test the negated content assertion.
*
* @return void
*/
public function testAssertResponseNotContains()
{
$this->_response = new Response();
$this->_response->body('Some content');
$this->assertResponseNotContains('contents');
}
/**
* Test that works in tandem with testEventManagerReset2 to
* test the EventManager reset.
*
* The return value is passed to testEventManagerReset2 as
* an arguments.
*
* @return \Cake\Event\EventManager
*/
public function testEventManagerReset1()
{
return EventManager::instance();
}
/**
* Test if the EventManager is reset between tests.
*
* @depends testEventManagerReset1
* @return void
*/
public function testEventManagerReset2($prevEventManager)
{
$this->assertNotSame($prevEventManager, EventManager::instance());
}
}