-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ControllerTestCaseTest.php
528 lines (466 loc) · 14.2 KB
/
ControllerTestCaseTest.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
/**
* ControllerTestCaseTest file
*
* Test Case for ControllerTestCase class
*
* PHP version 5
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
* @link http://cakephp.org CakePHP Project
* @package Cake.Test.Case.TestSuite
* @since CakePHP v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Controller', 'Controller');
App::uses('Model', 'Model');
App::uses('AppModel', 'Model');
App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
/**
* AppController class
*
* @package Cake.Test.Case.TestSuite
*/
if (!class_exists('AppController', false)) {
/**
* AppController class
*
* @package Cake.Test.Case.TestSuite
*/
class AppController extends Controller {
/**
* helpers property
*
* @var array
* @access public
*/
public $helpers = array('Html');
/**
* uses property
*
* @var array
* @access public
*/
public $uses = array('ControllerPost');
/**
* components property
*
* @var array
* @access public
*/
public $components = array('Cookie');
}
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
define('APP_CONTROLLER_EXISTS', true);
}
/**
* PostsController class
*/
if (!class_exists('PostsController')) {
class PostsController extends AppController {
/**
* Components array
*
* @var array
*/
public $components = array(
'RequestHandler',
'Email',
'Auth'
);
}
}
/**
* ControllerTestCaseTest controller
*/
class ControllerTestCaseTestController extends AppController {
/**
* Uses array
*
* @param array
*/
public $uses = array('TestPlugin.TestPluginComment');
}
/**
* ControllerTestCaseTest
*
* @package Cake.Test.Case.TestSuite
*/
class ControllerTestCaseTest extends CakeTestCase {
/**
* fixtures property
*
* @var array
*/
public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
/**
* reset environment.
*
* @return void
*/
public function setUp() {
parent::setUp();
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
), App::RESET);
CakePlugin::loadAll();
$this->Case = $this->getMockForAbstractClass('ControllerTestCase');
Router::reload();
}
/**
* teardown
*
* @return void
*/
public function tearDown() {
parent::tearDown();
CakePlugin::unload();
$this->Case->controller = null;
}
/**
* Test that ControllerTestCase::generate() creates mock objects correctly
*/
public function testGenerate() {
if (defined('APP_CONTROLLER_EXISTS')) {
$this->markTestSkipped('AppController exists, cannot run.');
}
$Posts = $this->Case->generate('Posts');
$this->assertEquals($Posts->name, 'Posts');
$this->assertEquals($Posts->modelClass, 'Post');
$this->assertNull($Posts->response->send());
$Posts = $this->Case->generate('Posts', array(
'methods' => array(
'render'
)
));
$this->assertNull($Posts->render('index'));
$Posts = $this->Case->generate('Posts', array(
'models' => array('Post'),
'components' => array('RequestHandler')
));
$this->assertInstanceOf('Post', $Posts->Post);
$this->assertNull($Posts->Post->save(array()));
$this->assertNull($Posts->Post->find('all'));
$this->assertEquals($Posts->Post->useTable, 'posts');
$this->assertNull($Posts->RequestHandler->isAjax());
$Posts = $this->Case->generate('Posts', array(
'models' => array(
'Post' => true
)
));
$this->assertNull($Posts->Post->save(array()));
$this->assertNull($Posts->Post->find('all'));
$Posts = $this->Case->generate('Posts', array(
'models' => array(
'Post' => array('save'),
)
));
$this->assertNull($Posts->Post->save(array()));
$this->assertInternalType('array', $Posts->Post->find('all'));
$Posts = $this->Case->generate('Posts', array(
'models' => array('Post'),
'components' => array(
'RequestHandler' => array('isPut'),
'Email' => array('send'),
'Session'
)
));
$Posts->RequestHandler->expects($this->once())
->method('isPut')
->will($this->returnValue(true));
$this->assertTrue($Posts->RequestHandler->isPut());
$Posts->Auth->Session->expects($this->any())
->method('write')
->will($this->returnValue('written!'));
$this->assertEquals($Posts->Auth->Session->write('something'), 'written!');
}
/**
* Tests ControllerTestCase::generate() using classes from plugins
*/
public function testGenerateWithPlugin() {
$Tests = $this->Case->generate('TestPlugin.Tests', array(
'models' => array(
'TestPlugin.TestPluginComment'
),
'components' => array(
'TestPlugin.PluginsComponent'
)
));
$this->assertEquals($Tests->name, 'Tests');
$this->assertInstanceOf('PluginsComponentComponent', $Tests->PluginsComponent);
$result = ClassRegistry::init('TestPlugin.TestPluginComment');
$this->assertInstanceOf('TestPluginComment', $result);
$Tests = $this->Case->generate('ControllerTestCaseTest', array(
'models' => array(
'TestPlugin.TestPluginComment' => array('save')
)
));
$this->assertInstanceOf('TestPluginComment', $Tests->TestPluginComment);
$Tests->TestPluginComment->expects($this->at(0))
->method('save')
->will($this->returnValue(true));
$Tests->TestPluginComment->expects($this->at(1))
->method('save')
->will($this->returnValue(false));
$this->assertTrue($Tests->TestPluginComment->save(array()));
$this->assertFalse($Tests->TestPluginComment->save(array()));
}
/**
* Tests testAction
*/
public function testTestAction() {
$Controller = $this->Case->generate('TestsApps');
$this->Case->testAction('/tests_apps/index');
$this->assertInternalType('array', $this->Case->controller->viewVars);
$this->Case->testAction('/tests_apps/set_action');
$results = $this->Case->controller->viewVars;
$expected = array(
'var' => 'string'
);
$this->assertEquals($expected, $results);
$result = $this->Case->controller->response->body();
$this->assertPattern('/This is the TestsAppsController index view/', $result);
$Controller = $this->Case->generate('TestsApps');
$this->Case->testAction('/tests_apps/redirect_to');
$results = $this->Case->headers;
$expected = array(
'Location' => 'http://cakephp.org'
);
$this->assertEquals($expected, $results);
}
/**
* Make sure testAction() can hit plugin controllers.
*
* @return void
*/
public function testTestActionWithPlugin() {
$Controller = $this->Case->generate('TestPlugin.Tests');
$this->Case->testAction('/test_plugin/tests/index');
$this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
}
/**
* Tests using loaded routes during tests
*
* @return void
*/
public function testUseRoutes() {
Router::connect('/:controller/:action/*');
include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
$controller = $this->Case->generate('TestsApps');
$controller->Components->load('RequestHandler');
$result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'view'));
$result = json_decode($result, true);
$expected = array('cakephp' => 'cool');
$this->assertEquals($expected, $result);
include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
$result = $this->Case->testAction('/some_alias');
$this->assertEquals($result, 5);
}
/**
* Tests not using loaded routes during tests
*
* @expectedException MissingActionException
*/
public function testSkipRoutes() {
Router::connect('/:controller/:action/*');
include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
$this->Case->loadRoutes = false;
$result = $this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
}
/**
* Tests backwards compatibility with setting the return type
*/
public function testBCSetReturn() {
$this->Case->autoMock = true;
$result = $this->Case->testAction('/tests_apps/some_method');
$this->assertEquals($result, 5);
$data = array('var' => 'set');
$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
'data' => $data,
'return' => 'vars'
));
$this->assertEquals($result['data'], $data);
$result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'view'
));
$this->assertEquals($result, 'This is the TestsAppsController index view string');
$result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'contents'
));
$this->assertPattern('/<html/', $result);
$this->assertPattern('/This is the TestsAppsController index view/', $result);
$this->assertPattern('/<\/html>/', $result);
}
/**
* Tests sending POST data to testAction
*/
public function testTestActionPostData() {
$this->Case->autoMock = true;
$data = array(
'Post' => array(
'name' => 'Some Post'
)
);
$this->Case->testAction('/tests_apps_posts/post_var', array(
'data' => $data
));
$this->assertEquals($this->Case->controller->viewVars['data'], $data);
$this->assertEquals($this->Case->controller->data, $data);
$this->Case->testAction('/tests_apps_posts/post_var/named:param', array(
'data' => $data
));
$expected = array(
'named' => 'param'
);
$this->assertEqual($this->Case->controller->request->named, $expected);
$this->assertEquals($this->Case->controller->data, $data);
$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
'return' => 'vars',
'method' => 'post',
'data' => array(
'name' => 'is jonas',
'pork' => 'and beans',
)
));
$this->assertEqual(array_keys($result['data']), array('name', 'pork'));
$result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
$this->assertTrue(array_key_exists('posts', $result));
$this->assertEqual(count($result['posts']), 4);
$this->assertTrue($this->Case->controller->request->is('post'));
}
/**
* Tests sending GET data to testAction
*/
public function testTestActionGetData() {
$this->Case->autoMock = true;
$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
'method' => 'get',
'data' => array(
'some' => 'var',
'lackof' => 'creativity'
)
));
$this->assertEquals($this->Case->controller->request->query['some'], 'var');
$this->assertEquals($this->Case->controller->request->query['lackof'], 'creativity');
$result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
'return' => 'vars',
'method' => 'get',
));
$this->assertEqual(array_keys($result['params']['named']), array('var1', 'var2'));
$result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
'return' => 'vars',
'method' => 'get',
));
$this->assertEqual($result['params']['pass'], array('gogo', 'val2'));
$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
'return' => 'vars',
'method' => 'get',
'data' => array(
'red' => 'health',
'blue' => 'mana'
)
));
$query = $this->Case->controller->request->query;
$this->assertTrue(isset($query['red']));
$this->assertTrue(isset($query['blue']));
}
/**
* Test that REST actions with XML/JSON input work.
*
* @return void
*/
public function testTestActionJsonData() {
$result = $this->Case->testAction('/tests_apps_posts/input_data', array(
'return' => 'vars',
'method' => 'post',
'data' => '{"key":"value","json":true}'
));
$this->assertEquals('value', $result['data']['key']);
$this->assertTrue($result['data']['json']);
}
/**
* Tests autoMock ability
*/
public function testAutoMock() {
$this->Case->autoMock = true;
$this->Case->testAction('/tests_apps/set_action');
$results = $this->Case->controller->viewVars;
$expected = array(
'var' => 'string'
);
$this->assertEquals($expected, $results);
}
/**
* Test using testAction and not mocking
*/
public function testNoMocking() {
$result = $this->Case->testAction('/tests_apps/some_method');
$this->Case->assertEquals($result, 5);
$data = array('var' => 'set');
$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
'data' => $data,
'return' => 'vars'
));
$this->assertEquals($result['data'], $data);
$result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'view'
));
$this->assertEquals($result, 'This is the TestsAppsController index view string');
$result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'contents'
));
$this->assertPattern('/<html/', $result);
$this->assertPattern('/This is the TestsAppsController index view/', $result);
$this->assertPattern('/<\/html>/', $result);
}
/**
* Test that controllers don't get reused.
*
* @return void
*/
public function testNoControllerReuse() {
$this->Case->autoMock = true;
$result = $this->Case->testAction('/tests_apps/index', array(
'data' => array('var' => 'first call'),
'method' => 'get',
'return' => 'contents',
));
$this->assertContains('<html', $result);
$this->assertContains('This is the TestsAppsController index view', $result);
$this->assertContains('first call', $result);
$this->assertContains('</html>', $result);
$result = $this->Case->testAction('/tests_apps/index', array(
'data' => array('var' => 'second call'),
'method' => 'get',
'return' => 'contents'
));
$this->assertContains('second call', $result);
$result = $this->Case->testAction('/tests_apps/index', array(
'data' => array('var' => 'third call'),
'method' => 'get',
'return' => 'contents'
));
$this->assertContains('third call', $result);
}
/**
* Test that multiple calls to redirect in the same test method don't cause issues.
*
* @return void
*/
public function testTestActionWithMultipleRedirect() {
$Controller = $this->Case->generate('TestsApps');
$options = array('method' => 'get');
$this->Case->testAction('/tests_apps/redirect_to', $options);
$this->Case->testAction('/tests_apps/redirect_to', $options);
}
}