Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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
	public function testGenerate() {
150
		if (defined('APP_CONTROLLER_EXISTS')) {
151
			$this->markTestSkipped('AppController exists, cannot run.');
152
		}
153
		$Posts = $this->Case->generate('Posts');
154
		$this->assertEquals('Posts', $Posts->name);
155
		$this->assertEquals('Post', $Posts->modelClass);
156
		$this->assertNull($Posts->response->send());
157
 
158
		$Posts = $this->Case->generate('Posts', array(
159
			'methods' => array(
160
				'render'
161
			)
162
		));
163
		$this->assertNull($Posts->render('index'));
164
 
165
		$Posts = $this->Case->generate('Posts', array(
166
			'models' => array('Post'),
167
			'components' => array('RequestHandler')
168
		));
169
 
170
		$this->assertInstanceOf('Post', $Posts->Post);
171
		$this->assertNull($Posts->Post->save(array()));
172
		$this->assertNull($Posts->Post->find('all'));
173
		$this->assertEquals('posts', $Posts->Post->useTable);
174
		$this->assertNull($Posts->RequestHandler->isAjax());
175
 
176
		$Posts = $this->Case->generate('Posts', array(
177
			'models' => array(
178
				'Post' => true
179
			)
180
		));
181
		$this->assertNull($Posts->Post->save(array()));
182
		$this->assertNull($Posts->Post->find('all'));
183
 
184
		$Posts = $this->Case->generate('Posts', array(
185
			'models' => array(
186
				'Post' => array('save'),
187
			)
188
		));
189
		$this->assertNull($Posts->Post->save(array()));
190
		$this->assertInternalType('array', $Posts->Post->find('all'));
191
 
192
		$Posts = $this->Case->generate('Posts', array(
193
			'models' => array('Post'),
194
			'components' => array(
195
				'RequestHandler' => array('isPut'),
196
				'Email' => array('send'),
197
				'Session'
198
			)
199
		));
200
		$Posts->RequestHandler->expects($this->once())
201
			->method('isPut')
202
			->will($this->returnValue(true));
203
		$this->assertTrue($Posts->RequestHandler->isPut());
204
 
205
		$Posts->Auth->Session->expects($this->any())
206
			->method('write')
207
			->will($this->returnValue('written!'));
208
		$this->assertEquals('written!', $Posts->Auth->Session->write('something'));
209
	}
210
 
211
/**
212
 * testGenerateWithComponentConfig
213
 */
214
	public function testGenerateWithComponentConfig() {
215
		$Tests = $this->Case->generate('TestConfigs', array(
216
		));
217
 
218
		$expected = array('some' => 'config');
219
		$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
220
		$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
221
 
222
		$Tests = $this->Case->generate('TestConfigs', array(
223
			'components' => array(
224
				'RequestHandler' => array('isPut')
225
			)
226
		));
227
 
228
		$expected = array('some' => 'config');
229
		$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
230
		$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
231
	}
232
 
233
/**
234
 * Tests ControllerTestCase::generate() using classes from plugins
235
 */
236
	public function testGenerateWithPlugin() {
237
		$Tests = $this->Case->generate('TestPlugin.Tests', array(
238
			'models' => array(
239
				'TestPlugin.TestPluginComment'
240
			),
241
			'components' => array(
242
				'TestPlugin.Plugins'
243
			)
244
		));
245
		$this->assertEquals('Tests', $Tests->name);
246
		$this->assertInstanceOf('PluginsComponent', $Tests->Plugins);
247
 
248
		$result = ClassRegistry::init('TestPlugin.TestPluginComment');
249
		$this->assertInstanceOf('TestPluginComment', $result);
250
 
251
		$Tests = $this->Case->generate('ControllerTestCaseTest', array(
252
			'models' => array(
253
				'TestPlugin.TestPluginComment' => array('save')
254
			)
255
		));
256
		$this->assertInstanceOf('TestPluginComment', $Tests->TestPluginComment);
257
		$Tests->TestPluginComment->expects($this->at(0))
258
			->method('save')
259
			->will($this->returnValue(true));
260
		$Tests->TestPluginComment->expects($this->at(1))
261
			->method('save')
262
			->will($this->returnValue(false));
263
		$this->assertTrue($Tests->TestPluginComment->save(array()));
264
		$this->assertFalse($Tests->TestPluginComment->save(array()));
265
	}
266
 
267
/**
268
 * Tests testAction
269
 */
270
	public function testTestAction() {
271
		$Controller = $this->Case->generate('TestsApps');
272
		$this->Case->testAction('/tests_apps/index');
273
		$this->assertInternalType('array', $this->Case->controller->viewVars);
274
 
275
		$this->Case->testAction('/tests_apps/set_action');
276
		$results = $this->Case->controller->viewVars;
277
		$expected = array(
278
			'var' => 'string'
279
		);
280
		$this->assertEquals($expected, $results);
281
 
282
		$result = $this->Case->controller->response->body();
283
		$this->assertRegExp('/This is the TestsAppsController index view/', $result);
284
 
285
		$Controller = $this->Case->generate('TestsApps');
286
		$this->Case->testAction('/tests_apps/redirect_to');
287
		$results = $this->Case->headers;
288
		$expected = array(
289
			'Location' => 'http://cakephp.org'
290
		);
291
		$this->assertEquals($expected, $results);
292
	}
293
 
294
/**
295
 * Make sure testAction() can hit plugin controllers.
296
 *
297
 * @return void
298
 */
299
	public function testTestActionWithPlugin() {
300
		$this->Case->generate('TestPlugin.Tests');
301
		$this->Case->testAction('/test_plugin/tests/index');
302
		$this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
303
	}
304
 
305
/**
306
 * Tests using loaded routes during tests
307
 *
308
 * @return void
309
 */
310
	public function testUseRoutes() {
311
		Router::connect('/:controller/:action/*');
312
		include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
313
 
314
		$controller = $this->Case->generate('TestsApps');
315
		$controller->Components->load('RequestHandler');
316
		$result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
317
		$result = json_decode($result, true);
318
		$expected = array('cakephp' => 'cool');
319
		$this->assertEquals($expected, $result);
320
 
321
		include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
322
		$result = $this->Case->testAction('/some_alias');
323
		$this->assertEquals(5, $result);
324
	}
325
 
326
/**
327
 * Tests not using loaded routes during tests
328
 *
329
 * @expectedException MissingActionException
330
 */
331
	public function testSkipRoutes() {
332
		Router::connect('/:controller/:action/*');
333
		include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
334
 
335
		$this->Case->loadRoutes = false;
336
		$this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
337
	}
338
 
339
/**
340
 * Tests backwards compatibility with setting the return type
341
 */
342
	public function testBCSetReturn() {
343
		$this->Case->autoMock = true;
344
 
345
		$result = $this->Case->testAction('/tests_apps/some_method');
346
		$this->assertEquals(5, $result);
347
 
348
		$data = array('var' => 'set');
349
		$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
350
			'data' => $data,
351
			'return' => 'vars'
352
		));
353
		$this->assertEquals($data, $result['data']);
354
 
355
		$result = $this->Case->testAction('/tests_apps/set_action', array(
356
			'return' => 'view'
357
		));
358
		$this->assertEquals('This is the TestsAppsController index view string', $result);
359
 
360
		$result = $this->Case->testAction('/tests_apps/set_action', array(
361
			'return' => 'contents'
362
		));
363
		$this->assertRegExp('/<html/', $result);
364
		$this->assertRegExp('/This is the TestsAppsController index view/', $result);
365
		$this->assertRegExp('/<\/html>/', $result);
366
	}
367
 
368
/**
369
 * Tests sending POST data to testAction
370
 */
371
	public function testTestActionPostData() {
372
		$this->Case->autoMock = true;
373
 
374
		$data = array(
375
			'Post' => array(
376
				'name' => 'Some Post'
377
			)
378
		);
379
		$this->Case->testAction('/tests_apps_posts/post_var', array(
380
			'data' => $data
381
		));
382
		$this->assertEquals($this->Case->controller->viewVars['data'], $data);
383
		$this->assertEquals($this->Case->controller->data, $data);
384
 
385
		$this->Case->testAction('/tests_apps_posts/post_var/named:param', array(
386
			'data' => $data
387
		));
388
		$expected = array(
389
			'named' => 'param'
390
		);
391
		$this->assertEquals($expected, $this->Case->controller->request->named);
392
		$this->assertEquals($this->Case->controller->data, $data);
393
 
394
		$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
395
			'return' => 'vars',
396
			'method' => 'post',
397
			'data' => array(
398
				'name' => 'is jonas',
399
				'pork' => 'and beans',
400
			)
401
		));
402
		$this->assertEquals(array('name', 'pork'), array_keys($result['data']));
403
 
404
		$result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
405
		$this->assertTrue(array_key_exists('posts', $result));
406
		$this->assertEquals(4, count($result['posts']));
407
		$this->assertTrue($this->Case->controller->request->is('post'));
408
	}
409
 
410
/**
411
 * Tests sending GET data to testAction
412
 */
413
	public function testTestActionGetData() {
414
		$this->Case->autoMock = true;
415
 
416
		$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
417
			'method' => 'get',
418
			'data' => array(
419
				'some' => 'var',
420
				'lackof' => 'creativity'
421
			)
422
		));
423
		$this->assertEquals('var', $this->Case->controller->request->query['some']);
424
		$this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
425
 
426
		$result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
427
			'return' => 'vars',
428
			'method' => 'get',
429
		));
430
		$this->assertEquals(array('var1', 'var2'), array_keys($result['params']['named']));
431
 
432
		$result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
433
			'return' => 'vars',
434
			'method' => 'get',
435
		));
436
		$this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
437
 
438
		$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
439
			'return' => 'vars',
440
			'method' => 'get',
441
			'data' => array(
442
				'red' => 'health',
443
				'blue' => 'mana'
444
			)
445
		));
446
		$query = $this->Case->controller->request->query;
447
		$this->assertTrue(isset($query['red']));
448
		$this->assertTrue(isset($query['blue']));
449
	}
450
 
451
/**
452
 * Test that REST actions with XML/JSON input work.
453
 *
454
 * @return void
455
 */
456
	public function testTestActionJsonData() {
457
		$result = $this->Case->testAction('/tests_apps_posts/input_data', array(
458
			'return' => 'vars',
459
			'method' => 'post',
460
			'data' => '{"key":"value","json":true}'
461
		));
462
		$this->assertEquals('value', $result['data']['key']);
463
		$this->assertTrue($result['data']['json']);
464
	}
465
 
466
/**
467
 * Tests autoMock ability
468
 */
469
	public function testAutoMock() {
470
		$this->Case->autoMock = true;
471
		$this->Case->testAction('/tests_apps/set_action');
472
		$results = $this->Case->controller->viewVars;
473
		$expected = array(
474
			'var' => 'string'
475
		);
476
		$this->assertEquals($expected, $results);
477
	}
478
 
479
/**
480
 * Test using testAction and not mocking
481
 */
482
	public function testNoMocking() {
483
		$result = $this->Case->testAction('/tests_apps/some_method');
484
		$this->Case->assertEquals(5, $result);
485
 
486
		$data = array('var' => 'set');
487
		$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
488
			'data' => $data,
489
			'return' => 'vars'
490
		));
491
		$this->assertEquals($data, $result['data']);
492
 
493
		$result = $this->Case->testAction('/tests_apps/set_action', array(
494
			'return' => 'view'
495
		));
496
		$this->assertEquals('This is the TestsAppsController index view string', $result);
497
 
498
		$result = $this->Case->testAction('/tests_apps/set_action', array(
499
			'return' => 'contents'
500
		));
501
		$this->assertRegExp('/<html/', $result);
502
		$this->assertRegExp('/This is the TestsAppsController index view/', $result);
503
		$this->assertRegExp('/<\/html>/', $result);
504
	}
505
 
506
/**
507
 * Test that controllers don't get reused.
508
 *
509
 * @return void
510
 */
511
	public function testNoControllerReuse() {
512
		$this->Case->autoMock = true;
513
		$result = $this->Case->testAction('/tests_apps/index', array(
514
			'data' => array('var' => 'first call'),
515
			'method' => 'get',
516
			'return' => 'contents',
517
		));
518
		$this->assertContains('<html', $result);
519
		$this->assertContains('This is the TestsAppsController index view', $result);
520
		$this->assertContains('first call', $result);
521
		$this->assertContains('</html>', $result);
522
 
523
		$result = $this->Case->testAction('/tests_apps/index', array(
524
			'data' => array('var' => 'second call'),
525
			'method' => 'get',
526
			'return' => 'contents'
527
		));
528
		$this->assertContains('second call', $result);
529
 
530
		$result = $this->Case->testAction('/tests_apps/index', array(
531
			'data' => array('var' => 'third call'),
532
			'method' => 'get',
533
			'return' => 'contents'
534
		));
535
		$this->assertContains('third call', $result);
536
	}
537
 
538
/**
539
 * Test that multiple calls to redirect in the same test method don't cause issues.
540
 *
541
 * @return void
542
 */
543
	public function testTestActionWithMultipleRedirect() {
544
		$this->Case->generate('TestsApps');
545
 
546
		$options = array('method' => 'get');
547
		$this->Case->testAction('/tests_apps/redirect_to', $options);
548
		$this->Case->testAction('/tests_apps/redirect_to', $options);
549
	}
550
 
551
/**
552
 * Tests that Components storing response or request objects internally during construct
553
 * will always have a fresh reference to those object available
554
 *
555
 * @return void
556
 * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
557
 */
558
	public function testComponentsSameRequestAndResponse() {
559
		$this->Case->generate('TestsApps');
560
		$options = array('method' => 'get');
561
		$this->Case->testAction('/tests_apps/index', $options);
562
		$this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
563
		$this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
564
	}
565
 
566
/**
567
 * Test that testAction() doesn't destroy data in GET & POST
568
 *
569
 * @return void
570
 */
571
	public function testRestoreGetPost() {
572
		$restored = array('new' => 'value');
573
 
574
		$_GET = $restored;
575
		$_POST = $restored;
576
 
577
		$this->Case->generate('TestsApps');
578
		$options = array('method' => 'get');
579
		$this->Case->testAction('/tests_apps/index', $options);
580
 
581
		$this->assertEquals($restored, $_GET);
582
		$this->assertEquals($restored, $_POST);
583
	}
584
 
585
}