Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * ShellDispatcherTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Console
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ShellDispatcher', 'Console');
20
 
21
/**
22
 * TestShellDispatcher class
23
 *
24
 * @package       Cake.Test.Case.Console
25
 */
26
class TestShellDispatcher extends ShellDispatcher {
27
 
28
/**
29
 * params property
30
 *
31
 * @var array
32
 */
33
	public $params = array();
34
 
35
/**
36
 * stopped property
37
 *
38
 * @var string
39
 */
40
	public $stopped = null;
41
 
42
/**
43
 * TestShell
44
 *
45
 * @var mixed
46
 */
47
	public $TestShell;
48
 
49
/**
50
 * _initEnvironment method
51
 *
52
 * @return void
53
 */
54
	protected function _initEnvironment() {
55
	}
56
 
57
/**
58
 * clear method
59
 *
60
 * @return void
61
 */
62
	public function clear() {
63
	}
64
 
65
/**
66
 * _stop method
67
 *
68
 * @return void
69
 */
70
	protected function _stop($status = 0) {
71
		$this->stopped = 'Stopped with status: ' . $status;
72
		return $status;
73
	}
74
 
75
/**
76
 * getShell
77
 *
78
 * @param string $shell
79
 * @return mixed
80
 */
81
	public function getShell($shell) {
82
		return $this->_getShell($shell);
83
	}
84
 
85
/**
86
 * _getShell
87
 *
88
 * @param string $plugin
89
 * @return mixed
90
 */
91
	protected function _getShell($shell) {
92
		if (isset($this->TestShell)) {
93
			return $this->TestShell;
94
		}
95
		return parent::_getShell($shell);
96
	}
97
 
98
}
99
 
100
/**
101
 * ShellDispatcherTest
102
 *
103
 * @package       Cake.Test.Case.Console
104
 */
105
class ShellDispatcherTest extends CakeTestCase {
106
 
107
/**
108
 * setUp method
109
 *
110
 * @return void
111
 */
112
	public function setUp() {
113
		parent::setUp();
114
		App::build(array(
115
			'Plugin' => array(
116
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
117
			),
118
			'Console/Command' => array(
119
				CAKE . 'Test' . DS . 'test_app' . DS . 'Console' . DS . 'Command' . DS
120
			)
121
		), App::RESET);
122
		CakePlugin::load('TestPlugin');
123
	}
124
 
125
/**
126
 * tearDown method
127
 *
128
 * @return void
129
 */
130
	public function tearDown() {
131
		parent::tearDown();
132
		CakePlugin::unload();
133
	}
134
 
135
/**
136
 * testParseParams method
137
 *
138
 * @return void
139
 */
140
	public function testParseParams() {
141
		$Dispatcher = new TestShellDispatcher();
142
 
143
		$params = array(
144
			'/cake/1.2.x.x/cake/console/cake.php',
145
			'bake',
146
			'-app',
147
			'new',
148
			'-working',
149
			'/var/www/htdocs'
150
		);
151
		$expected = array(
152
			'app' => 'new',
153
			'webroot' => 'webroot',
154
			'working' => str_replace('/', DS, '/var/www/htdocs/new'),
155
			'root' => str_replace('/', DS, '/var/www/htdocs')
156
		);
157
		$Dispatcher->parseParams($params);
158
		$this->assertEquals($expected, $Dispatcher->params);
159
 
160
		$params = array('cake.php');
161
		$expected = array(
162
			'app' => 'app',
163
			'webroot' => 'webroot',
164
			'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'app'),
165
			'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH)),
166
		);
167
		$Dispatcher->params = $Dispatcher->args = array();
168
		$Dispatcher->parseParams($params);
169
		$this->assertEquals($expected, $Dispatcher->params);
170
 
171
		$params = array(
172
			'cake.php',
173
			'-app',
174
			'new',
175
		);
176
		$expected = array(
177
			'app' => 'new',
178
			'webroot' => 'webroot',
179
			'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
180
			'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH))
181
		);
182
		$Dispatcher->params = $Dispatcher->args = array();
183
		$Dispatcher->parseParams($params);
184
		$this->assertEquals($expected, $Dispatcher->params);
185
 
186
		$params = array(
187
			'./cake.php',
188
			'bake',
189
			'-app',
190
			'new',
191
			'-working',
192
			'/cake/1.2.x.x/cake/console'
193
		);
194
 
195
		$expected = array(
196
			'app' => 'new',
197
			'webroot' => 'webroot',
198
			'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
199
			'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH))
200
		);
201
 
202
		$Dispatcher->params = $Dispatcher->args = array();
203
		$Dispatcher->parseParams($params);
204
		$this->assertEquals($expected, $Dispatcher->params);
205
 
206
		$params = array(
207
			'./console/cake.php',
208
			'bake',
209
			'-app',
210
			'new',
211
			'-working',
212
			'/cake/1.2.x.x/cake'
213
		);
214
		$expected = array(
215
			'app' => 'new',
216
			'webroot' => 'webroot',
217
			'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
218
			'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH))
219
		);
220
		$Dispatcher->params = $Dispatcher->args = array();
221
		$Dispatcher->parseParams($params);
222
		$this->assertEquals($expected, $Dispatcher->params);
223
 
224
		$params = array(
225
			'./console/cake.php',
226
			'bake',
227
			'-app',
228
			'new',
229
			'-dry',
230
			'-working',
231
			'/cake/1.2.x.x/cake'
232
		);
233
		$expected = array(
234
			'app' => 'new',
235
			'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
236
			'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH)),
237
			'webroot' => 'webroot'
238
		);
239
		$Dispatcher->params = $Dispatcher->args = array();
240
		$Dispatcher->parseParams($params);
241
		$this->assertEquals($expected, $Dispatcher->params);
242
 
243
		$params = array(
244
			'./console/cake.php',
245
			'-working',
246
			'/cake/1.2.x.x/cake',
247
			'schema',
248
			'run',
249
			'create',
250
			'-dry',
251
			'-f',
252
			'-name',
253
			'DbAcl'
254
		);
255
		$expected = array(
256
			'app' => 'app',
257
			'webroot' => 'webroot',
258
			'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'app'),
259
			'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH)),
260
		);
261
		$Dispatcher->params = $Dispatcher->args = array();
262
		$Dispatcher->parseParams($params);
263
		$this->assertEquals($expected, $Dispatcher->params);
264
 
265
		$expected = array(
266
			'./console/cake.php', 'schema', 'run', 'create', '-dry', '-f', '-name', 'DbAcl'
267
		);
268
		$this->assertEquals($expected, $Dispatcher->args);
269
 
270
		$params = array(
271
			'/cake/1.2.x.x/cake/console/cake.php',
272
			'-working',
273
			'/cake/1.2.x.x/app',
274
			'schema',
275
			'run',
276
			'create',
277
			'-dry',
278
			'-name',
279
			'DbAcl'
280
		);
281
		$expected = array(
282
			'app' => 'app',
283
			'webroot' => 'webroot',
284
			'working' => str_replace('/', DS, '/cake/1.2.x.x/app'),
285
			'root' => str_replace('/', DS, '/cake/1.2.x.x'),
286
		);
287
		$Dispatcher->params = $Dispatcher->args = array();
288
		$Dispatcher->parseParams($params);
289
		$this->assertEquals($expected, $Dispatcher->params);
290
 
291
		$params = array(
292
			'cake.php',
293
			'-working',
294
			'C:/wamp/www/cake/app',
295
			'bake',
296
			'-app',
297
			'C:/wamp/www/apps/cake/app',
298
		);
299
		$expected = array(
300
			'app' => 'app',
301
			'webroot' => 'webroot',
302
			'working' => 'C:\wamp\www\apps\cake\app',
303
			'root' => 'C:\wamp\www\apps\cake'
304
		);
305
 
306
		$Dispatcher->params = $Dispatcher->args = array();
307
		$Dispatcher->parseParams($params);
308
		$this->assertEquals($expected, $Dispatcher->params);
309
 
310
		$params = array(
311
			'cake.php',
312
			'-working',
313
			'C:\wamp\www\cake\app',
314
			'bake',
315
			'-app',
316
			'C:\wamp\www\apps\cake\app',
317
		);
318
		$expected = array(
319
			'app' => 'app',
320
			'webroot' => 'webroot',
321
			'working' => 'C:\wamp\www\apps\cake\app',
322
			'root' => 'C:\wamp\www\apps\cake'
323
		);
324
		$Dispatcher->params = $Dispatcher->args = array();
325
		$Dispatcher->parseParams($params);
326
		$this->assertEquals($expected, $Dispatcher->params);
327
 
328
		$params = array(
329
			'cake.php',
330
			'-working',
331
			'C:\wamp\www\apps',
332
			'bake',
333
			'-app',
334
			'cake\app',
335
			'-url',
336
			'http://example.com/some/url/with/a/path'
337
		);
338
		$expected = array(
339
			'app' => 'app',
340
			'webroot' => 'webroot',
341
			'working' => 'C:\wamp\www\apps\cake\app',
342
			'root' => 'C:\wamp\www\apps\cake',
343
		);
344
		$Dispatcher->params = $Dispatcher->args = array();
345
		$Dispatcher->parseParams($params);
346
		$this->assertEquals($expected, $Dispatcher->params);
347
 
348
		$params = array(
349
			'/home/amelo/dev/cake-common/cake/console/cake.php',
350
			'-root',
351
			'/home/amelo/dev/lsbu-vacancy',
352
			'-working',
353
			'/home/amelo/dev/lsbu-vacancy',
354
			'-app',
355
			'app',
356
		);
357
		$expected = array(
358
			'app' => 'app',
359
			'webroot' => 'webroot',
360
			'working' => '/home/amelo/dev/lsbu-vacancy/app',
361
			'root' => '/home/amelo/dev/lsbu-vacancy',
362
		);
363
		$Dispatcher->params = $Dispatcher->args = array();
364
		$Dispatcher->parseParams($params);
365
		$this->assertEquals($expected, $Dispatcher->params);
366
 
367
		$params = array(
368
			'/cake/1.2.x.x/cake/console/cake.php',
369
			'bake',
370
			'-app',
371
			'new',
372
			'-app',
373
			'old',
374
			'-working',
375
			'/var/www/htdocs'
376
		);
377
		$expected = array(
378
			'app' => 'old',
379
			'webroot' => 'webroot',
380
			'working' => str_replace('/', DS, '/var/www/htdocs/old'),
381
			'root' => str_replace('/', DS, '/var/www/htdocs')
382
		);
383
		$Dispatcher->parseParams($params);
384
		$this->assertEquals($expected, $Dispatcher->params);
385
 
386
		if (DS === '\\') {
387
			$params = array(
388
				'cake.php',
389
				'-working',
390
				'D:\www',
391
				'bake',
392
				'my_app',
393
			);
394
			$expected = array(
395
				'working' => 'D:\\\\www',
396
				'app' => 'www',
397
				'root' => 'D:\\',
398
				'webroot' => 'webroot'
399
			);
400
 
401
			$Dispatcher->params = $Dispatcher->args = array();
402
			$Dispatcher->parseParams($params);
403
			$this->assertEquals($expected, $Dispatcher->params);
404
		}
405
	}
406
 
407
/**
408
 * Verify loading of (plugin-) shells
409
 *
410
 * @return void
411
 */
412
	public function testGetShell() {
413
		$this->skipIf(class_exists('SampleShell'), 'SampleShell Class already loaded.');
414
		$this->skipIf(class_exists('ExampleShell'), 'ExampleShell Class already loaded.');
415
 
416
		$Dispatcher = new TestShellDispatcher();
417
 
418
		$result = $Dispatcher->getShell('sample');
419
		$this->assertInstanceOf('SampleShell', $result);
420
 
421
		$Dispatcher = new TestShellDispatcher();
422
		$result = $Dispatcher->getShell('test_plugin.example');
423
		$this->assertInstanceOf('ExampleShell', $result);
424
		$this->assertEquals('TestPlugin', $result->plugin);
425
		$this->assertEquals('Example', $result->name);
426
 
427
		$Dispatcher = new TestShellDispatcher();
428
		$result = $Dispatcher->getShell('TestPlugin.example');
429
		$this->assertInstanceOf('ExampleShell', $result);
430
	}
431
 
432
/**
433
 * Verify correct dispatch of Shell subclasses with a main method
434
 *
435
 * @return void
436
 */
437
	public function testDispatchShellWithMain() {
438
		$Dispatcher = new TestShellDispatcher();
439
		$Shell = $this->getMock('Shell');
440
 
441
		$Shell->expects($this->once())->method('initialize');
442
		$Shell->expects($this->once())->method('runCommand')
443
			->with(null, array())
444
			->will($this->returnValue(true));
445
 
446
		$Dispatcher->TestShell = $Shell;
447
 
448
		$Dispatcher->args = array('mock_with_main');
449
		$result = $Dispatcher->dispatch();
450
		$this->assertTrue($result);
451
		$this->assertEquals(array(), $Dispatcher->args);
452
	}
453
 
454
/**
455
 * Verify correct dispatch of Shell subclasses without a main method
456
 *
457
 * @return void
458
 */
459
	public function testDispatchShellWithoutMain() {
460
		$Dispatcher = new TestShellDispatcher();
461
		$Shell = $this->getMock('Shell');
462
 
463
		$Shell->expects($this->once())->method('initialize');
464
		$Shell->expects($this->once())->method('runCommand')
465
			->with('initdb', array('initdb'))
466
			->will($this->returnValue(true));
467
 
468
		$Dispatcher->TestShell = $Shell;
469
 
470
		$Dispatcher->args = array('mock_without_main', 'initdb');
471
		$result = $Dispatcher->dispatch();
472
		$this->assertTrue($result);
473
	}
474
 
475
/**
476
 * Verify correct dispatch of custom classes with a main method
477
 *
478
 * @return void
479
 */
480
	public function testDispatchNotAShellWithMain() {
481
		$Dispatcher = new TestShellDispatcher();
482
		$methods = get_class_methods('Object');
483
		array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
484
		$Shell = $this->getMock('Object', $methods);
485
 
486
		$Shell->expects($this->never())->method('initialize');
487
		$Shell->expects($this->once())->method('startup');
488
		$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
489
		$Dispatcher->TestShell = $Shell;
490
 
491
		$Dispatcher->args = array('mock_with_main_not_a');
492
		$result = $Dispatcher->dispatch();
493
		$this->assertTrue($result);
494
		$this->assertEquals(array(), $Dispatcher->args);
495
 
496
		$Shell = $this->getMock('Object', $methods);
497
		$Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
498
		$Shell->expects($this->once())->method('startup');
499
		$Dispatcher->TestShell = $Shell;
500
 
501
		$Dispatcher->args = array('mock_with_main_not_a', 'initdb');
502
		$result = $Dispatcher->dispatch();
503
		$this->assertTrue($result);
504
	}
505
 
506
/**
507
 * Verify correct dispatch of custom classes without a main method
508
 *
509
 * @return void
510
 */
511
	public function testDispatchNotAShellWithoutMain() {
512
		$Dispatcher = new TestShellDispatcher();
513
		$methods = get_class_methods('Object');
514
		array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
515
		$Shell = $this->getMock('Object', $methods);
516
 
517
		$Shell->expects($this->never())->method('initialize');
518
		$Shell->expects($this->once())->method('startup');
519
		$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
520
		$Dispatcher->TestShell = $Shell;
521
 
522
		$Dispatcher->args = array('mock_without_main_not_a');
523
		$result = $Dispatcher->dispatch();
524
		$this->assertTrue($result);
525
		$this->assertEquals(array(), $Dispatcher->args);
526
 
527
		$Shell = $this->getMock('Object', $methods);
528
		$Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
529
		$Shell->expects($this->once())->method('startup');
530
		$Dispatcher->TestShell = $Shell;
531
 
532
		$Dispatcher->args = array('mock_without_main_not_a', 'initdb');
533
		$result = $Dispatcher->dispatch();
534
		$this->assertTrue($result);
535
	}
536
 
537
/**
538
 * Verify shifting of arguments
539
 *
540
 * @return void
541
 */
542
	public function testShiftArgs() {
543
		$Dispatcher = new TestShellDispatcher();
544
 
545
		$Dispatcher->args = array('a', 'b', 'c');
546
		$this->assertEquals('a', $Dispatcher->shiftArgs());
547
		$this->assertSame($Dispatcher->args, array('b', 'c'));
548
 
549
		$Dispatcher->args = array('a' => 'b', 'c', 'd');
550
		$this->assertEquals('b', $Dispatcher->shiftArgs());
551
		$this->assertSame($Dispatcher->args, array('c', 'd'));
552
 
553
		$Dispatcher->args = array('a', 'b' => 'c', 'd');
554
		$this->assertEquals('a', $Dispatcher->shiftArgs());
555
		$this->assertSame($Dispatcher->args, array('b' => 'c', 'd'));
556
 
557
		$Dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
558
		$this->assertEquals('a', $Dispatcher->shiftArgs());
559
		$this->assertSame($Dispatcher->args, array(0 => 'b', 1 => 'c'));
560
 
561
		$Dispatcher->args = array();
562
		$this->assertNull($Dispatcher->shiftArgs());
563
		$this->assertSame(array(), $Dispatcher->args);
564
	}
565
 
566
}