Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ControllerTask Test Case
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Console.Command.Task
15
 * @since         CakePHP(tm) v 1.3
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ConsoleOutput', 'Console');
20
App::uses('ConsoleInput', 'Console');
21
App::uses('ShellDispatcher', 'Console');
22
App::uses('Shell', 'Console');
23
App::uses('CakeSchema', 'Model');
24
App::uses('ClassRegistry', 'Utility');
25
App::uses('Helper', 'View/Helper');
26
App::uses('ProjectTask', 'Console/Command/Task');
27
App::uses('ControllerTask', 'Console/Command/Task');
28
App::uses('ModelTask', 'Console/Command/Task');
29
App::uses('TemplateTask', 'Console/Command/Task');
30
App::uses('TestTask', 'Console/Command/Task');
31
App::uses('Model', 'Model');
32
 
33
App::uses('BakeArticle', 'Model');
34
App::uses('BakeComment', 'Model');
35
App::uses('BakeTags', 'Model');
36
$imported = class_exists('BakeArticle') || class_exists('BakeComment') || class_exists('BakeTag');
37
 
38
if (!$imported) {
39
	define('ARTICLE_MODEL_CREATED', true);
40
 
41
/**
42
 * Class BakeArticle
43
 */
44
	class BakeArticle extends Model {
45
 
46
		public $hasMany = array('BakeComment');
47
 
48
		public $hasAndBelongsToMany = array('BakeTag');
49
 
50
	}
51
}
52
 
53
/**
54
 * ControllerTaskTest class
55
 *
56
 * @package       Cake.Test.Case.Console.Command.Task
57
 */
58
class ControllerTaskTest extends CakeTestCase {
59
 
60
/**
61
 * fixtures
62
 *
63
 * @var array
64
 */
65
	public $fixtures = array('core.bake_article', 'core.bake_articles_bake_tag', 'core.bake_comment', 'core.bake_tag');
66
 
67
/**
68
 * setUp method
69
 *
70
 * @return void
71
 */
72
	public function setUp() {
73
		parent::setUp();
74
		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
75
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
76
		$this->Task = $this->getMock('ControllerTask',
77
			array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
78
			array($out, $out, $in)
79
		);
80
		$this->Task->name = 'Controller';
81
		$this->Task->Template = new TemplateTask($out, $out, $in);
82
		$this->Task->Template->params['theme'] = 'default';
83
 
84
		$this->Task->Model = $this->getMock('ModelTask',
85
			array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest'),
86
			array($out, $out, $in)
87
		);
88
		$this->Task->Project = $this->getMock('ProjectTask',
89
			array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getPrefix'),
90
			array($out, $out, $in)
91
		);
92
		$this->Task->Test = $this->getMock('TestTask', array(), array($out, $out, $in));
93
 
94
		if (!defined('ARTICLE_MODEL_CREATED')) {
95
			$this->markTestSkipped('Could not run as an Article, Tag or Comment model was already loaded.');
96
		}
97
	}
98
 
99
/**
100
 * tearDown method
101
 *
102
 * @return void
103
 */
104
	public function tearDown() {
105
		unset($this->Task);
106
		ClassRegistry::flush();
107
		App::build();
108
		parent::tearDown();
109
	}
110
 
111
/**
112
 * test ListAll
113
 *
114
 * @return void
115
 */
116
	public function testListAll() {
117
		$count = count($this->Task->listAll('test'));
118
		if ($count != count($this->fixtures)) {
119
			$this->markTestSkipped('Additional tables detected.');
120
		}
121
 
122
		$this->Task->connection = 'test';
123
		$this->Task->interactive = true;
124
		$this->Task->expects($this->at(2))->method('out')->with(' 1. BakeArticles');
125
		$this->Task->expects($this->at(3))->method('out')->with(' 2. BakeArticlesBakeTags');
126
		$this->Task->expects($this->at(4))->method('out')->with(' 3. BakeComments');
127
		$this->Task->expects($this->at(5))->method('out')->with(' 4. BakeTags');
128
 
129
		$expected = array('BakeArticles', 'BakeArticlesBakeTags', 'BakeComments', 'BakeTags');
130
		$result = $this->Task->listAll('test');
131
		$this->assertEquals($expected, $result);
132
 
133
		$this->Task->interactive = false;
134
		$result = $this->Task->listAll();
135
 
136
		$expected = array('bake_articles', 'bake_articles_bake_tags', 'bake_comments', 'bake_tags');
137
		$this->assertEquals($expected, $result);
138
	}
139
 
140
/**
141
 * Test that getName interacts with the user and returns the controller name.
142
 *
143
 * @return void
144
 */
145
	public function testGetNameValidIndex() {
146
		$count = count($this->Task->listAll('test'));
147
		if ($count != count($this->fixtures)) {
148
			$this->markTestSkipped('Additional tables detected.');
149
		}
150
		$this->Task->interactive = true;
151
		$this->Task->expects($this->any())->method('in')->will(
152
			$this->onConsecutiveCalls(3, 1)
153
		);
154
 
155
		$result = $this->Task->getName('test');
156
		$expected = 'BakeComments';
157
		$this->assertEquals($expected, $result);
158
 
159
		$result = $this->Task->getName('test');
160
		$expected = 'BakeArticles';
161
		$this->assertEquals($expected, $result);
162
	}
163
 
164
/**
165
 * test getting invalid indexes.
166
 *
167
 * @return void
168
 */
169
	public function testGetNameInvalidIndex() {
170
		$this->Task->interactive = true;
171
		$this->Task->expects($this->any())->method('in')
172
			->will($this->onConsecutiveCalls(50, 'q'));
173
 
174
		$this->Task->expects($this->once())->method('err');
175
		$this->Task->expects($this->once())->method('_stop');
176
 
177
		$this->Task->getName('test');
178
	}
179
 
180
/**
181
 * test helper interactions
182
 *
183
 * @return void
184
 */
185
	public function testDoHelpersNo() {
186
		$this->Task->expects($this->any())->method('in')->will($this->returnValue('n'));
187
		$result = $this->Task->doHelpers();
188
		$this->assertSame(array(), $result);
189
	}
190
 
191
/**
192
 * test getting helper values
193
 *
194
 * @return void
195
 */
196
	public function testDoHelpersTrailingSpace() {
197
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
198
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' Text, Number, CustomOne  '));
199
		$result = $this->Task->doHelpers();
200
		$expected = array('Text', 'Number', 'CustomOne');
201
		$this->assertEquals($expected, $result);
202
	}
203
 
204
/**
205
 * test doHelpers with extra commas
206
 *
207
 * @return void
208
 */
209
	public function testDoHelpersTrailingCommas() {
210
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
211
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' Text, Number, CustomOne, , '));
212
		$result = $this->Task->doHelpers();
213
		$expected = array('Text', 'Number', 'CustomOne');
214
		$this->assertEquals($expected, $result);
215
	}
216
 
217
/**
218
 * test component interactions
219
 *
220
 * @return void
221
 */
222
	public function testDoComponentsNo() {
223
		$this->Task->expects($this->any())->method('in')->will($this->returnValue('n'));
224
		$result = $this->Task->doComponents();
225
		$this->assertSame(array('Paginator'), $result);
226
	}
227
 
228
/**
229
 * test components with spaces
230
 *
231
 * @return void
232
 */
233
	public function testDoComponentsTrailingSpaces() {
234
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
235
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' RequestHandler, Security  '));
236
 
237
		$result = $this->Task->doComponents();
238
		$expected = array('Paginator', 'RequestHandler', 'Security');
239
		$this->assertEquals($expected, $result);
240
	}
241
 
242
/**
243
 * test components with commas
244
 *
245
 * @return void
246
 */
247
	public function testDoComponentsTrailingCommas() {
248
		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
249
		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' RequestHandler, Security, , '));
250
 
251
		$result = $this->Task->doComponents();
252
		$expected = array('Paginator', 'RequestHandler', 'Security');
253
		$this->assertEquals($expected, $result);
254
	}
255
 
256
/**
257
 * test Confirming controller user interaction
258
 *
259
 * @return void
260
 */
261
	public function testConfirmController() {
262
		$controller = 'Posts';
263
		$scaffold = false;
264
		$helpers = array('Js', 'Time');
265
		$components = array('Acl', 'Auth');
266
 
267
		$this->Task->expects($this->at(4))->method('out')->with("Controller Name:\n\t$controller");
268
		$this->Task->expects($this->at(5))->method('out')->with("Helpers:\n\tJs, Time");
269
		$this->Task->expects($this->at(6))->method('out')->with("Components:\n\tAcl, Auth");
270
		$this->Task->confirmController($controller, $scaffold, $helpers, $components);
271
	}
272
 
273
/**
274
 * test the bake method
275
 *
276
 * @return void
277
 */
278
	public function testBake() {
279
		$helpers = array('Js', 'Time');
280
		$components = array('Acl', 'Auth');
281
		$this->Task->expects($this->any())->method('createFile')->will($this->returnValue(true));
282
 
283
		$result = $this->Task->bake('Articles', null, $helpers, $components);
284
		$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoActions.ctp');
285
		$this->assertTextEquals($expected, $result);
286
 
287
		$result = $this->Task->bake('Articles', null, array(), array());
288
		$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoHelpersOrComponents.ctp');
289
		$this->assertTextEquals($expected, $result);
290
 
291
		$result = $this->Task->bake('Articles', 'scaffold', $helpers, $components);
292
		$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'Scaffold.ctp');
293
		$this->assertTextEquals($expected, $result);
294
	}
295
 
296
/**
297
 * test bake() with a -plugin param
298
 *
299
 * @return void
300
 */
301
	public function testBakeWithPlugin() {
302
		$this->Task->plugin = 'ControllerTest';
303
 
304
		//fake plugin path
305
		CakePlugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
306
		$path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Controller' . DS . 'ArticlesController.php';
307
 
308
		$this->Task->expects($this->at(1))->method('createFile')->with(
309
			$path,
310
			new PHPUnit_Framework_Constraint_IsAnything()
311
		);
312
		$this->Task->expects($this->at(3))->method('createFile')->with(
313
			$path,
314
			$this->stringContains('ArticlesController extends ControllerTestAppController')
315
		)->will($this->returnValue(true));
316
 
317
		$this->Task->bake('Articles', '--actions--', array(), array(), array());
318
 
319
		$this->Task->plugin = 'ControllerTest';
320
		$path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Controller' . DS . 'ArticlesController.php';
321
		$result = $this->Task->bake('Articles', '--actions--', array(), array(), array());
322
 
323
		$this->assertContains("App::uses('ControllerTestAppController', 'ControllerTest.Controller');", $result);
324
		$this->assertEquals('ControllerTest', $this->Task->Template->templateVars['plugin']);
325
		$this->assertEquals('ControllerTest.', $this->Task->Template->templateVars['pluginPath']);
326
 
327
		CakePlugin::unload();
328
	}
329
 
330
/**
331
 * test that bakeActions is creating the correct controller Code. (Using sessions)
332
 *
333
 * @return void
334
 */
335
	public function testBakeActionsUsingSessions() {
336
		$result = $this->Task->bakeActions('BakeArticles', null, true);
337
		$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsUsingSessions.ctp');
338
		$this->assertTextEquals($expected, $result);
339
 
340
		$result = $this->Task->bakeActions('BakeArticles', 'admin_', true);
341
		$this->assertContains('function admin_index() {', $result);
342
		$this->assertContains('function admin_add()', $result);
343
		$this->assertContains('function admin_view($id = null)', $result);
344
		$this->assertContains('function admin_edit($id = null)', $result);
345
		$this->assertContains('function admin_delete($id = null)', $result);
346
	}
347
 
348
/**
349
 * Test baking with Controller::flash() or no sessions.
350
 *
351
 * @return void
352
 */
353
	public function testBakeActionsWithNoSessions() {
354
		$result = $this->Task->bakeActions('BakeArticles', null, false);
355
		$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsWithNoSessions.ctp');
356
		$this->assertTextEquals($expected, $result);
357
	}
358
 
359
/**
360
 * test baking a test
361
 *
362
 * @return void
363
 */
364
	public function testBakeTest() {
365
		$this->Task->plugin = 'ControllerTest';
366
		$this->Task->connection = 'test';
367
		$this->Task->interactive = false;
368
 
369
		$this->Task->Test->expects($this->once())->method('bake')->with('Controller', 'BakeArticles');
370
		$this->Task->bakeTest('BakeArticles');
371
 
372
		$this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
373
		$this->assertEquals($this->Task->connection, $this->Task->Test->connection);
374
		$this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
375
	}
376
 
377
/**
378
 * test Interactive mode.
379
 *
380
 * @return void
381
 */
382
	public function testInteractive() {
383
		$count = count($this->Task->listAll('test'));
384
		if ($count != count($this->fixtures)) {
385
			$this->markTestSkipped('Additional tables detected.');
386
		}
387
 
388
		$this->Task->connection = 'test';
389
		$this->Task->path = '/my/path/';
390
 
391
		$this->Task->expects($this->any())->method('in')
392
			->will($this->onConsecutiveCalls(
393
				'1',
394
				'y', // build interactive
395
				'n', // build no scaffolds
396
				'y', // build normal methods
397
				'n', // build admin methods
398
				'n', // helpers?
399
				'n', // components?
400
				'y', // sessions ?
401
				'y' // looks good?
402
			));
403
 
404
		$filename = '/my/path/BakeArticlesController.php';
405
		$this->Task->expects($this->once())->method('createFile')->with(
406
			$filename,
407
			$this->stringContains('class BakeArticlesController')
408
		);
409
		$this->Task->execute();
410
	}
411
 
412
/**
413
 * test Interactive mode.
414
 *
415
 * @return void
416
 */
417
	public function testInteractiveAdminMethodsNotInteractive() {
418
		$count = count($this->Task->listAll('test'));
419
		if ($count != count($this->fixtures)) {
420
			$this->markTestSkipped('Additional tables detected.');
421
		}
422
 
423
		$this->Task->connection = 'test';
424
		$this->Task->interactive = true;
425
		$this->Task->path = '/my/path/';
426
 
427
		$this->Task->expects($this->any())->method('in')
428
			->will($this->onConsecutiveCalls(
429
				'1',
430
				'y', // build interactive
431
				'n', // build no scaffolds
432
				'y', // build normal methods
433
				'y', // build admin methods
434
				'n', // helpers?
435
				'n', // components?
436
				'y', // sessions ?
437
				'y' // looks good?
438
			));
439
 
440
		$this->Task->Project->expects($this->any())
441
			->method('getPrefix')
442
			->will($this->returnValue('admin_'));
443
 
444
		$filename = '/my/path/BakeArticlesController.php';
445
		$this->Task->expects($this->once())->method('createFile')->with(
446
			$filename,
447
			$this->stringContains('class BakeArticlesController')
448
		)->will($this->returnValue(true));
449
 
450
		$result = $this->Task->execute();
451
		$this->assertRegExp('/admin_index/', $result);
452
	}
453
 
454
/**
455
 * test that execute runs all when the first arg == all
456
 *
457
 * @return void
458
 */
459
	public function testExecuteIntoAll() {
460
		$count = count($this->Task->listAll('test'));
461
		if ($count != count($this->fixtures)) {
462
			$this->markTestSkipped('Additional tables detected.');
463
		}
464
 
465
		$this->Task->connection = 'test';
466
		$this->Task->path = '/my/path/';
467
		$this->Task->args = array('all');
468
 
469
		$this->Task->expects($this->any())->method('_checkUnitTest')->will($this->returnValue(true));
470
		$this->Task->Test->expects($this->once())->method('bake');
471
 
472
		$filename = '/my/path/BakeArticlesController.php';
473
		$this->Task->expects($this->once())->method('createFile')->with(
474
			$filename,
475
			$this->stringContains('class BakeArticlesController')
476
		)->will($this->returnValue(true));
477
 
478
		$this->Task->execute();
479
	}
480
 
481
/**
482
 * Test execute() with all and --admin
483
 *
484
 * @return void
485
 */
486
	public function testExecuteIntoAllAdmin() {
487
		$count = count($this->Task->listAll('test'));
488
		if ($count != count($this->fixtures)) {
489
			$this->markTestSkipped('Additional tables detected.');
490
		}
491
 
492
		$this->Task->connection = 'test';
493
		$this->Task->path = '/my/path/';
494
		$this->Task->args = array('all');
495
		$this->Task->params['admin'] = true;
496
 
497
		$this->Task->Project->expects($this->any())
498
			->method('getPrefix')
499
			->will($this->returnValue('admin_'));
500
		$this->Task->expects($this->any())
501
			->method('_checkUnitTest')
502
			->will($this->returnValue(true));
503
		$this->Task->Test->expects($this->once())->method('bake');
504
 
505
		$filename = '/my/path/BakeArticlesController.php';
506
		$this->Task->expects($this->once())->method('createFile')->with(
507
			$filename,
508
			$this->stringContains('function admin_index')
509
		)->will($this->returnValue(true));
510
 
511
		$this->Task->execute();
512
	}
513
 
514
/**
515
 * test that `cake bake controller foos` works.
516
 *
517
 * @return void
518
 */
519
	public function testExecuteWithController() {
520
		$this->Task->connection = 'test';
521
		$this->Task->path = '/my/path/';
522
		$this->Task->args = array('BakeArticles');
523
 
524
		$filename = '/my/path/BakeArticlesController.php';
525
		$this->Task->expects($this->once())->method('createFile')->with(
526
			$filename,
527
			$this->stringContains('$scaffold')
528
		);
529
 
530
		$this->Task->execute();
531
	}
532
 
533
/**
534
 * data provider for testExecuteWithControllerNameVariations
535
 *
536
 * @return void
537
 */
538
	public static function nameVariations() {
539
		return array(
540
			array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
541
		);
542
	}
543
 
544
/**
545
 * test that both plural and singular forms work for controller baking.
546
 *
547
 * @dataProvider nameVariations
548
 * @return void
549
 */
550
	public function testExecuteWithControllerNameVariations($name) {
551
		$this->Task->connection = 'test';
552
		$this->Task->path = '/my/path/';
553
		$this->Task->args = array($name);
554
 
555
		$filename = '/my/path/BakeArticlesController.php';
556
		$this->Task->expects($this->once())->method('createFile')->with(
557
			$filename, $this->stringContains('$scaffold')
558
		);
559
		$this->Task->execute();
560
	}
561
 
562
/**
563
 * test that `cake bake controller foo scaffold` works.
564
 *
565
 * @return void
566
 */
567
	public function testExecuteWithPublicParam() {
568
		$this->Task->connection = 'test';
569
		$this->Task->path = '/my/path/';
570
		$this->Task->args = array('BakeArticles');
571
		$this->Task->params = array('public' => true);
572
 
573
		$filename = '/my/path/BakeArticlesController.php';
574
		$expected = new PHPUnit_Framework_Constraint_Not($this->stringContains('$scaffold'));
575
		$this->Task->expects($this->once())->method('createFile')->with(
576
			$filename, $expected
577
		);
578
		$this->Task->execute();
579
	}
580
 
581
/**
582
 * test that `cake bake controller foos both` works.
583
 *
584
 * @return void
585
 */
586
	public function testExecuteWithControllerAndBoth() {
587
		$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
588
		$this->Task->connection = 'test';
589
		$this->Task->path = '/my/path/';
590
		$this->Task->args = array('BakeArticles');
591
		$this->Task->params = array('public' => true, 'admin' => true);
592
 
593
		$filename = '/my/path/BakeArticlesController.php';
594
		$this->Task->expects($this->once())->method('createFile')->with(
595
			$filename, $this->stringContains('admin_index')
596
		);
597
		$this->Task->execute();
598
	}
599
 
600
/**
601
 * test that `cake bake controller foos admin` works.
602
 *
603
 * @return void
604
 */
605
	public function testExecuteWithControllerAndAdmin() {
606
		$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
607
		$this->Task->connection = 'test';
608
		$this->Task->path = '/my/path/';
609
		$this->Task->args = array('BakeArticles');
610
		$this->Task->params = array('admin' => true);
611
 
612
		$filename = '/my/path/BakeArticlesController.php';
613
		$this->Task->expects($this->once())->method('createFile')->with(
614
			$filename, $this->stringContains('admin_index')
615
		);
616
		$this->Task->execute();
617
	}
618
}