Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
		$Controller = $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 that file responses don't trigger errors.
305
 *
306
 * @return void
307
 */
308
	public function testActionWithFile() {
309
		$Controller = $this->Case->generate('TestsApps');
310
		$this->Case->testAction('/tests_apps/file');
311
		$this->assertArrayHasKey('Content-Disposition', $Controller->response->header());
312
		$this->assertArrayHasKey('Content-Length', $Controller->response->header());
313
	}
314
 
315
/**
316
 * Make sure testAction() can hit plugin controllers.
317
 *
318
 * @return void
319
 */
320
	public function testTestActionWithPlugin() {
321
		$this->Case->generate('TestPlugin.Tests');
322
		$this->Case->testAction('/test_plugin/tests/index');
323
		$this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
324
	}
325
 
326
/**
327
 * Tests using loaded routes during tests
328
 *
329
 * @return void
330
 */
331
	public function testUseRoutes() {
332
		Router::connect('/:controller/:action/*');
333
		include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
334
 
335
		$controller = $this->Case->generate('TestsApps');
336
		$controller->Components->load('RequestHandler');
337
		$result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
338
		$result = json_decode($result, true);
339
		$expected = array('cakephp' => 'cool');
340
		$this->assertEquals($expected, $result);
341
 
342
		include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
343
		$result = $this->Case->testAction('/some_alias');
344
		$this->assertEquals(5, $result);
345
	}
346
 
347
/**
348
 * Tests not using loaded routes during tests
349
 *
350
 * @expectedException MissingActionException
351
 * @return void
352
 */
353
	public function testSkipRoutes() {
354
		Router::connect('/:controller/:action/*');
355
		include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
356
 
357
		$this->Case->loadRoutes = false;
358
		$this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
359
	}
360
 
361
/**
362
 * Tests backwards compatibility with setting the return type
363
 *
364
 * @return void
365
 */
366
	public function testBCSetReturn() {
367
		$this->Case->autoMock = true;
368
 
369
		$result = $this->Case->testAction('/tests_apps/some_method');
370
		$this->assertEquals(5, $result);
371
 
372
		$data = array('var' => 'set');
373
		$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
374
			'data' => $data,
375
			'return' => 'vars'
376
		));
377
		$this->assertEquals($data, $result['data']);
378
 
379
		$result = $this->Case->testAction('/tests_apps/set_action', array(
380
			'return' => 'view'
381
		));
382
		$this->assertEquals('This is the TestsAppsController index view string', $result);
383
 
384
		$result = $this->Case->testAction('/tests_apps/set_action', array(
385
			'return' => 'contents'
386
		));
387
		$this->assertRegExp('/<html/', $result);
388
		$this->assertRegExp('/This is the TestsAppsController index view/', $result);
389
		$this->assertRegExp('/<\/html>/', $result);
390
	}
391
 
392
/**
393
 * Tests sending POST data to testAction
394
 *
395
 * @return void
396
 */
397
	public function testTestActionPostData() {
398
		$this->Case->autoMock = true;
399
 
400
		$data = array(
401
			'Post' => array(
402
				'name' => 'Some Post'
403
			)
404
		);
405
		$this->Case->testAction('/tests_apps_posts/post_var', array(
406
			'data' => $data
407
		));
408
		$this->assertEquals($this->Case->controller->viewVars['data'], $data);
409
		$this->assertEquals($this->Case->controller->data, $data);
410
 
411
		$this->Case->testAction('/tests_apps_posts/post_var/named:param', array(
412
			'data' => $data
413
		));
414
		$expected = array(
415
			'named' => 'param'
416
		);
417
		$this->assertEquals($expected, $this->Case->controller->request->named);
418
		$this->assertEquals($this->Case->controller->data, $data);
419
 
420
		$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
421
			'return' => 'vars',
422
			'method' => 'post',
423
			'data' => array(
424
				'name' => 'is jonas',
425
				'pork' => 'and beans',
426
			)
427
		));
428
		$this->assertEquals(array('name', 'pork'), array_keys($result['data']));
429
 
430
		$result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
431
		$this->assertTrue(array_key_exists('posts', $result));
432
		$this->assertEquals(4, count($result['posts']));
433
		$this->assertTrue($this->Case->controller->request->is('post'));
434
	}
435
 
436
/**
437
 * Tests sending GET data to testAction
438
 *
439
 * @return void
440
 */
441
	public function testTestActionGetData() {
442
		$this->Case->autoMock = true;
443
 
444
		$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
445
			'method' => 'get',
446
			'data' => array(
447
				'some' => 'var',
448
				'lackof' => 'creativity'
449
			)
450
		));
451
		$this->assertEquals('var', $this->Case->controller->request->query['some']);
452
		$this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
453
 
454
		$result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
455
			'return' => 'vars',
456
			'method' => 'get',
457
		));
458
		$this->assertEquals(array('var1', 'var2'), array_keys($result['params']['named']));
459
 
460
		$result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
461
			'return' => 'vars',
462
			'method' => 'get',
463
		));
464
		$this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
465
 
466
		$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
467
			'return' => 'vars',
468
			'method' => 'get',
469
			'data' => array(
470
				'red' => 'health',
471
				'blue' => 'mana'
472
			)
473
		));
474
		$query = $this->Case->controller->request->query;
475
		$this->assertTrue(isset($query['red']));
476
		$this->assertTrue(isset($query['blue']));
477
	}
478
 
479
/**
480
 * Test that REST actions with XML/JSON input work.
481
 *
482
 * @return void
483
 */
484
	public function testTestActionJsonData() {
485
		$result = $this->Case->testAction('/tests_apps_posts/input_data', array(
486
			'return' => 'vars',
487
			'method' => 'post',
488
			'data' => '{"key":"value","json":true}'
489
		));
490
		$this->assertEquals('value', $result['data']['key']);
491
		$this->assertTrue($result['data']['json']);
492
	}
493
 
494
/**
495
 * Tests autoMock ability
496
 *
497
 * @return void
498
 */
499
	public function testAutoMock() {
500
		$this->Case->autoMock = true;
501
		$this->Case->testAction('/tests_apps/set_action');
502
		$results = $this->Case->controller->viewVars;
503
		$expected = array(
504
			'var' => 'string'
505
		);
506
		$this->assertEquals($expected, $results);
507
	}
508
 
509
/**
510
 * Test using testAction and not mocking
511
 *
512
 * @return void
513
 */
514
	public function testNoMocking() {
515
		$result = $this->Case->testAction('/tests_apps/some_method');
516
		$this->Case->assertEquals(5, $result);
517
 
518
		$data = array('var' => 'set');
519
		$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
520
			'data' => $data,
521
			'return' => 'vars'
522
		));
523
		$this->assertEquals($data, $result['data']);
524
 
525
		$result = $this->Case->testAction('/tests_apps/set_action', array(
526
			'return' => 'view'
527
		));
528
		$this->assertEquals('This is the TestsAppsController index view string', $result);
529
 
530
		$result = $this->Case->testAction('/tests_apps/set_action', array(
531
			'return' => 'contents'
532
		));
533
		$this->assertRegExp('/<html/', $result);
534
		$this->assertRegExp('/This is the TestsAppsController index view/', $result);
535
		$this->assertRegExp('/<\/html>/', $result);
536
	}
537
 
538
/**
539
 * Test that controllers don't get reused.
540
 *
541
 * @return void
542
 */
543
	public function testNoControllerReuse() {
544
		$this->Case->autoMock = true;
545
		$result = $this->Case->testAction('/tests_apps/index', array(
546
			'data' => array('var' => 'first call'),
547
			'method' => 'get',
548
			'return' => 'contents',
549
		));
550
		$this->assertContains('<html', $result);
551
		$this->assertContains('This is the TestsAppsController index view', $result);
552
		$this->assertContains('first call', $result);
553
		$this->assertContains('</html>', $result);
554
 
555
		$result = $this->Case->testAction('/tests_apps/index', array(
556
			'data' => array('var' => 'second call'),
557
			'method' => 'get',
558
			'return' => 'contents'
559
		));
560
		$this->assertContains('second call', $result);
561
 
562
		$result = $this->Case->testAction('/tests_apps/index', array(
563
			'data' => array('var' => 'third call'),
564
			'method' => 'get',
565
			'return' => 'contents'
566
		));
567
		$this->assertContains('third call', $result);
568
	}
569
 
570
/**
571
 * Test that multiple calls to redirect in the same test method don't cause issues.
572
 *
573
 * @return void
574
 */
575
	public function testTestActionWithMultipleRedirect() {
576
		$this->Case->generate('TestsApps');
577
 
578
		$options = array('method' => 'get');
579
		$this->Case->testAction('/tests_apps/redirect_to', $options);
580
		$this->Case->testAction('/tests_apps/redirect_to', $options);
581
	}
582
 
583
/**
584
 * Tests that Components storing response or request objects internally during construct
585
 * will always have a fresh reference to those object available
586
 *
587
 * @return void
588
 * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
589
 */
590
	public function testComponentsSameRequestAndResponse() {
591
		$this->Case->generate('TestsApps');
592
		$options = array('method' => 'get');
593
		$this->Case->testAction('/tests_apps/index', $options);
594
		$this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
595
		$this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
596
	}
597
 
598
/**
599
 * Test that testAction() doesn't destroy data in GET & POST
600
 *
601
 * @return void
602
 */
603
	public function testRestoreGetPost() {
604
		$restored = array('new' => 'value');
605
 
606
		$_GET = $restored;
607
		$_POST = $restored;
608
 
609
		$this->Case->generate('TestsApps');
610
		$options = array('method' => 'get');
611
		$this->Case->testAction('/tests_apps/index', $options);
612
 
613
		$this->assertEquals($restored, $_GET);
614
		$this->assertEquals($restored, $_POST);
615
	}
616
 
617
}