Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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