統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / TestSuite / ControllerTestCaseTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (17.061 KB)

1
<?php
2
/**
3
 * ControllerTestCaseTest file
4
 *
5
 * Test Case for ControllerTestCase class
6
 *
7
 * CakePHP : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://cakephp.org CakePHP Project
16
 * @package       Cake.Test.Case.TestSuite
17
 * @since         CakePHP v 2.0
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20

    
21
App::uses('Controller', 'Controller');
22
App::uses('Model', 'Model');
23
App::uses('AppModel', 'Model');
24
App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
25

    
26
require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
27

    
28
if (!class_exists('AppController', false)) {
29
/**
30
 * AppController class
31
 *
32
 * @package       Cake.Test.Case.TestSuite
33
 */
34
        class AppController extends Controller {
35

    
36
        /**
37
         * helpers property
38
         *
39
         * @var array
40
         */
41
                public $helpers = array('Html');
42

    
43
        /**
44
         * uses property
45
         *
46
         * @var array
47
         */
48
                public $uses = array('ControllerPost');
49

    
50
        /**
51
         * components property
52
         *
53
         * @var array
54
         */
55
                public $components = array('Cookie');
56

    
57
        }
58
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
59
        define('APP_CONTROLLER_EXISTS', true);
60
}
61

    
62
/**
63
 * PostsController class
64
 */
65
if (!class_exists('PostsController')) {
66

    
67
/**
68
 * Class PostsController
69
 *
70
 * @package       Cake.Test.Case.TestSuite
71
 */
72
        class PostsController extends AppController {
73

    
74
        /**
75
         * Components array
76
         *
77
         * @var array
78
         */
79
                public $components = array(
80
                        'RequestHandler',
81
                        'Email',
82
                        'Auth'
83
                );
84
        }
85
}
86

    
87
/**
88
 * ControllerTestCaseTest controller
89
 *
90
 * @package       Cake.Test.Case.TestSuite
91
 */
92
class ControllerTestCaseTestController extends AppController {
93

    
94
/**
95
 * Uses array
96
 *
97
 * @param array
98
 */
99
        public $uses = array('TestPlugin.TestPluginComment');
100

    
101
}
102

    
103
/**
104
 * ControllerTestCaseTest
105
 *
106
 * @package       Cake.Test.Case.TestSuite
107
 */
108
class ControllerTestCaseTest extends CakeTestCase {
109

    
110
/**
111
 * fixtures property
112
 *
113
 * @var array
114
 */
115
        public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
116

    
117
/**
118
 * reset environment.
119
 *
120
 * @return void
121
 */
122
        public function setUp() {
123
                parent::setUp();
124
                App::build(array(
125
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
126
                        'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
127
                        'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
128
                        'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
129
                ), App::RESET);
130
                CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
131
                $this->Case = $this->getMockForAbstractClass('ControllerTestCase');
132
                Router::reload();
133
        }
134

    
135
/**
136
 * tearDown
137
 *
138
 * @return void
139
 */
140
        public function tearDown() {
141
                parent::tearDown();
142
                CakePlugin::unload();
143
                $this->Case->controller = null;
144
        }
145

    
146
/**
147
 * Test that ControllerTestCase::generate() creates mock objects correctly
148
 *
149
 * @return void
150
 */
151
        public function testGenerate() {
152
                if (defined('APP_CONTROLLER_EXISTS')) {
153
                        $this->markTestSkipped('AppController exists, cannot run.');
154
                }
155
                $Posts = $this->Case->generate('Posts');
156
                $this->assertEquals('Posts', $Posts->name);
157
                $this->assertEquals('Post', $Posts->modelClass);
158
                $this->assertNull($Posts->response->send());
159

    
160
                $Posts = $this->Case->generate('Posts', array(
161
                        'methods' => array(
162
                                'render'
163
                        )
164
                ));
165
                $this->assertNull($Posts->render('index'));
166

    
167
                $Posts = $this->Case->generate('Posts', array(
168
                        'models' => array('Post'),
169
                        'components' => array('RequestHandler')
170
                ));
171

    
172
                $this->assertInstanceOf('Post', $Posts->Post);
173
                $this->assertNull($Posts->Post->save(array()));
174
                $this->assertNull($Posts->Post->find('all'));
175
                $this->assertEquals('posts', $Posts->Post->useTable);
176
                $this->assertNull($Posts->RequestHandler->isAjax());
177

    
178
                $Posts = $this->Case->generate('Posts', array(
179
                        'models' => array(
180
                                'Post' => true
181
                        )
182
                ));
183
                $this->assertNull($Posts->Post->save(array()));
184
                $this->assertNull($Posts->Post->find('all'));
185

    
186
                $Posts = $this->Case->generate('Posts', array(
187
                        'models' => array(
188
                                'Post' => array('save'),
189
                        )
190
                ));
191
                $this->assertNull($Posts->Post->save(array()));
192
                $this->assertInternalType('array', $Posts->Post->find('all'));
193

    
194
                $Posts = $this->Case->generate('Posts', array(
195
                        'models' => array('Post'),
196
                        'components' => array(
197
                                'RequestHandler' => array('isPut'),
198
                                'Email' => array('send'),
199
                                'Session'
200
                        )
201
                ));
202
                $Posts->RequestHandler->expects($this->once())
203
                        ->method('isPut')
204
                        ->will($this->returnValue(true));
205
                $this->assertTrue($Posts->RequestHandler->isPut());
206

    
207
                $Posts->Auth->Session->expects($this->any())
208
                        ->method('write')
209
                        ->will($this->returnValue('written!'));
210
                $this->assertEquals('written!', $Posts->Auth->Session->write('something'));
211
        }
212

    
213
/**
214
 * testGenerateWithComponentConfig
215
 *
216
 * @return void
217
 */
218
        public function testGenerateWithComponentConfig() {
219
                $Tests = $this->Case->generate('TestConfigs', array(
220
                ));
221

    
222
                $expected = array('some' => 'config');
223
                $settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
224
                $this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
225

    
226
                $Tests = $this->Case->generate('TestConfigs', array(
227
                        'components' => array(
228
                                'RequestHandler' => array('isPut')
229
                        )
230
                ));
231

    
232
                $expected = array('some' => 'config');
233
                $settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
234
                $this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
235
        }
236

    
237
/**
238
 * Tests ControllerTestCase::generate() using classes from plugins
239
 *
240
 * @return void
241
 */
242
        public function testGenerateWithPlugin() {
243
                $Tests = $this->Case->generate('TestPlugin.Tests', array(
244
                        'models' => array(
245
                                'TestPlugin.TestPluginComment'
246
                        ),
247
                        'components' => array(
248
                                'TestPlugin.Plugins'
249
                        )
250
                ));
251
                $this->assertEquals('Tests', $Tests->name);
252
                $this->assertInstanceOf('PluginsComponent', $Tests->Plugins);
253

    
254
                $result = ClassRegistry::init('TestPlugin.TestPluginComment');
255
                $this->assertInstanceOf('TestPluginComment', $result);
256

    
257
                $Tests = $this->Case->generate('ControllerTestCaseTest', array(
258
                        'models' => array(
259
                                'TestPlugin.TestPluginComment' => array('save')
260
                        )
261
                ));
262
                $this->assertInstanceOf('TestPluginComment', $Tests->TestPluginComment);
263
                $Tests->TestPluginComment->expects($this->at(0))
264
                        ->method('save')
265
                        ->will($this->returnValue(true));
266
                $Tests->TestPluginComment->expects($this->at(1))
267
                        ->method('save')
268
                        ->will($this->returnValue(false));
269
                $this->assertTrue($Tests->TestPluginComment->save(array()));
270
                $this->assertFalse($Tests->TestPluginComment->save(array()));
271
        }
272

    
273
/**
274
 * Tests testAction
275
 *
276
 * @return void
277
 */
278
        public function testTestAction() {
279
                $this->Case->generate('TestsApps');
280
                $this->Case->testAction('/tests_apps/index');
281
                $this->assertInternalType('array', $this->Case->controller->viewVars);
282

    
283
                $this->Case->testAction('/tests_apps/set_action');
284
                $results = $this->Case->controller->viewVars;
285
                $expected = array(
286
                        'var' => 'string'
287
                );
288
                $this->assertEquals($expected, $results);
289

    
290
                $result = $this->Case->controller->response->body();
291
                $this->assertRegExp('/This is the TestsAppsController index view/', $result);
292

    
293
                $Controller = $this->Case->generate('TestsApps');
294
                $this->Case->testAction('/tests_apps/redirect_to');
295
                $results = $this->Case->headers;
296
                $expected = array(
297
                        'Location' => 'http://cakephp.org'
298
                );
299
                $this->assertEquals($expected, $results);
300
                $this->assertSame(302, $Controller->response->statusCode());
301
        }
302

    
303
/**
304
 * Test array URLs with testAction()
305
 *
306
 * @return void
307
 */
308
        public function testTestActionArrayUrls() {
309
                $this->Case->generate('TestsApps');
310
                $this->Case->testAction(array('controller' => 'tests_apps', 'action' => 'index'));
311
                $this->assertInternalType('array', $this->Case->controller->viewVars);
312
        }
313

    
314
/**
315
 * Test that file responses don't trigger errors.
316
 *
317
 * @return void
318
 */
319
        public function testActionWithFile() {
320
                $Controller = $this->Case->generate('TestsApps');
321
                $this->Case->testAction('/tests_apps/file');
322
                $this->assertArrayHasKey('Content-Disposition', $Controller->response->header());
323
                $this->assertArrayHasKey('Content-Length', $Controller->response->header());
324
        }
325

    
326
/**
327
 * Make sure testAction() can hit plugin controllers.
328
 *
329
 * @return void
330
 */
331
        public function testTestActionWithPlugin() {
332
                $this->Case->generate('TestPlugin.Tests');
333
                $this->Case->testAction('/test_plugin/tests/index');
334
                $this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
335
        }
336

    
337
/**
338
 * Tests using loaded routes during tests
339
 *
340
 * @return void
341
 */
342
        public function testUseRoutes() {
343
                Router::connect('/:controller/:action/*');
344
                include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
345

    
346
                $controller = $this->Case->generate('TestsApps');
347
                $controller->Components->load('RequestHandler');
348
                $result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
349
                $result = json_decode($result, true);
350
                $expected = array('cakephp' => 'cool');
351
                $this->assertEquals($expected, $result);
352

    
353
                include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
354
                $result = $this->Case->testAction('/some_alias');
355
                $this->assertEquals(5, $result);
356
        }
357

    
358
/**
359
 * Tests not using loaded routes during tests
360
 *
361
 * @expectedException MissingActionException
362
 * @return void
363
 */
364
        public function testSkipRoutes() {
365
                Router::connect('/:controller/:action/*');
366
                include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
367

    
368
                $this->Case->loadRoutes = false;
369
                $this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
370
        }
371

    
372
/**
373
 * Tests backwards compatibility with setting the return type
374
 *
375
 * @return void
376
 */
377
        public function testBCSetReturn() {
378
                $this->Case->autoMock = true;
379

    
380
                $result = $this->Case->testAction('/tests_apps/some_method');
381
                $this->assertEquals(5, $result);
382

    
383
                $data = array('var' => 'set');
384
                $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
385
                        'data' => $data,
386
                        'return' => 'vars'
387
                ));
388
                $this->assertEquals($data, $result['data']);
389

    
390
                $result = $this->Case->testAction('/tests_apps/set_action', array(
391
                        'return' => 'view'
392
                ));
393
                $this->assertEquals('This is the TestsAppsController index view string', $result);
394

    
395
                $result = $this->Case->testAction('/tests_apps/set_action', array(
396
                        'return' => 'contents'
397
                ));
398
                $this->assertRegExp('/<html/', $result);
399
                $this->assertRegExp('/This is the TestsAppsController index view/', $result);
400
                $this->assertRegExp('/<\/html>/', $result);
401
        }
402

    
403
/**
404
 * Tests sending POST data to testAction
405
 *
406
 * @return void
407
 */
408
        public function testTestActionPostData() {
409
                $this->Case->autoMock = true;
410

    
411
                $data = array(
412
                        'Post' => array(
413
                                'name' => 'Some Post'
414
                        )
415
                );
416
                $this->Case->testAction('/tests_apps_posts/post_var', array(
417
                        'data' => $data
418
                ));
419
                $this->assertEquals($this->Case->controller->viewVars['data'], $data);
420
                $this->assertEquals($this->Case->controller->data, $data);
421

    
422
                $this->Case->testAction('/tests_apps_posts/post_var/named:param', array(
423
                        'data' => $data
424
                ));
425
                $expected = array(
426
                        'named' => 'param'
427
                );
428
                $this->assertEquals($expected, $this->Case->controller->request->named);
429
                $this->assertEquals($this->Case->controller->data, $data);
430

    
431
                $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
432
                        'return' => 'vars',
433
                        'method' => 'post',
434
                        'data' => array(
435
                                'name' => 'is jonas',
436
                                'pork' => 'and beans',
437
                        )
438
                ));
439
                $this->assertEquals(array('name', 'pork'), array_keys($result['data']));
440

    
441
                $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
442
                $this->assertTrue(array_key_exists('posts', $result));
443
                $this->assertEquals(4, count($result['posts']));
444
                $this->assertTrue($this->Case->controller->request->is('post'));
445
        }
446

    
447
/**
448
 * Tests sending GET data to testAction
449
 *
450
 * @return void
451
 */
452
        public function testTestActionGetData() {
453
                $this->Case->autoMock = true;
454

    
455
                $this->Case->testAction('/tests_apps_posts/url_var', array(
456
                        'method' => 'get',
457
                        'data' => array(
458
                                'some' => 'var',
459
                                'lackof' => 'creativity'
460
                        )
461
                ));
462
                $this->assertEquals('var', $this->Case->controller->request->query['some']);
463
                $this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
464

    
465
                $result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
466
                        'return' => 'vars',
467
                        'method' => 'get',
468
                ));
469
                $this->assertEquals(array('var1', 'var2'), array_keys($result['params']['named']));
470

    
471
                $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
472
                        'return' => 'vars',
473
                        'method' => 'get',
474
                ));
475
                $this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
476

    
477
                $this->Case->testAction('/tests_apps_posts/url_var', array(
478
                        'return' => 'vars',
479
                        'method' => 'get',
480
                        'data' => array(
481
                                'red' => 'health',
482
                                'blue' => 'mana'
483
                        )
484
                ));
485
                $query = $this->Case->controller->request->query;
486
                $this->assertTrue(isset($query['red']));
487
                $this->assertTrue(isset($query['blue']));
488
        }
489

    
490
/**
491
 * Test that REST actions with XML/JSON input work.
492
 *
493
 * @return void
494
 */
495
        public function testTestActionJsonData() {
496
                $result = $this->Case->testAction('/tests_apps_posts/input_data', array(
497
                        'return' => 'vars',
498
                        'method' => 'post',
499
                        'data' => '{"key":"value","json":true}'
500
                ));
501
                $this->assertEquals('value', $result['data']['key']);
502
                $this->assertTrue($result['data']['json']);
503
        }
504

    
505
/**
506
 * Tests autoMock ability
507
 *
508
 * @return void
509
 */
510
        public function testAutoMock() {
511
                $this->Case->autoMock = true;
512
                $this->Case->testAction('/tests_apps/set_action');
513
                $results = $this->Case->controller->viewVars;
514
                $expected = array(
515
                        'var' => 'string'
516
                );
517
                $this->assertEquals($expected, $results);
518
        }
519

    
520
/**
521
 * Test using testAction and not mocking
522
 *
523
 * @return void
524
 */
525
        public function testNoMocking() {
526
                $result = $this->Case->testAction('/tests_apps/some_method');
527
                $this->Case->assertEquals(5, $result);
528

    
529
                $data = array('var' => 'set');
530
                $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
531
                        'data' => $data,
532
                        'return' => 'vars'
533
                ));
534
                $this->assertEquals($data, $result['data']);
535

    
536
                $result = $this->Case->testAction('/tests_apps/set_action', array(
537
                        'return' => 'view'
538
                ));
539
                $this->assertEquals('This is the TestsAppsController index view string', $result);
540

    
541
                $result = $this->Case->testAction('/tests_apps/set_action', array(
542
                        'return' => 'contents'
543
                ));
544
                $this->assertRegExp('/<html/', $result);
545
                $this->assertRegExp('/This is the TestsAppsController index view/', $result);
546
                $this->assertRegExp('/<\/html>/', $result);
547
        }
548

    
549
/**
550
 * Test that controllers don't get reused.
551
 *
552
 * @return void
553
 */
554
        public function testNoControllerReuse() {
555
                $this->Case->autoMock = true;
556
                $result = $this->Case->testAction('/tests_apps/index', array(
557
                        'data' => array('var' => 'first call'),
558
                        'method' => 'get',
559
                        'return' => 'contents',
560
                ));
561
                $this->assertContains('<html', $result);
562
                $this->assertContains('This is the TestsAppsController index view', $result);
563
                $this->assertContains('first call', $result);
564
                $this->assertContains('</html>', $result);
565

    
566
                $result = $this->Case->testAction('/tests_apps/index', array(
567
                        'data' => array('var' => 'second call'),
568
                        'method' => 'get',
569
                        'return' => 'contents'
570
                ));
571
                $this->assertContains('second call', $result);
572

    
573
                $result = $this->Case->testAction('/tests_apps/index', array(
574
                        'data' => array('var' => 'third call'),
575
                        'method' => 'get',
576
                        'return' => 'contents'
577
                ));
578
                $this->assertContains('third call', $result);
579
        }
580

    
581
/**
582
 * Test that multiple calls to redirect in the same test method don't cause issues.
583
 *
584
 * @return void
585
 */
586
        public function testTestActionWithMultipleRedirect() {
587
                $this->Case->generate('TestsApps');
588

    
589
                $options = array('method' => 'get');
590
                $this->Case->testAction('/tests_apps/redirect_to', $options);
591
                $this->Case->testAction('/tests_apps/redirect_to', $options);
592
        }
593

    
594
/**
595
 * Tests that Components storing response or request objects internally during construct
596
 * will always have a fresh reference to those object available
597
 *
598
 * @return void
599
 * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
600
 */
601
        public function testComponentsSameRequestAndResponse() {
602
                $this->Case->generate('TestsApps');
603
                $options = array('method' => 'get');
604
                $this->Case->testAction('/tests_apps/index', $options);
605
                $this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
606
                $this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
607
        }
608

    
609
/**
610
 * Test that testAction() doesn't destroy data in GET & POST
611
 *
612
 * @return void
613
 */
614
        public function testRestoreGetPost() {
615
                $restored = array('new' => 'value');
616

    
617
                $_GET = $restored;
618
                $_POST = $restored;
619

    
620
                $this->Case->generate('TestsApps');
621
                $options = array('method' => 'get');
622
                $this->Case->testAction('/tests_apps/index', $options);
623

    
624
                $this->assertEquals($restored, $_GET);
625
                $this->assertEquals($restored, $_POST);
626
        }
627

    
628
}