統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Console / ShellDispatcherTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (14.32 KB)

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
                $Dispatcher = new TestShellDispatcher();
432
                $result = $Dispatcher->getShell('test_plugin');
433
                $this->assertInstanceOf('TestPluginShell', $result);
434

    
435
                $Dispatcher = new TestShellDispatcher();
436
                $result = $Dispatcher->getShell('TestPlugin');
437
                $this->assertInstanceOf('TestPluginShell', $result);
438
        }
439

    
440
/**
441
 * Verify correct dispatch of Shell subclasses with a main method
442
 *
443
 * @return void
444
 */
445
        public function testDispatchShellWithMain() {
446
                $Dispatcher = new TestShellDispatcher();
447
                $Shell = $this->getMock('Shell');
448

    
449
                $Shell->expects($this->once())->method('initialize');
450
                $Shell->expects($this->once())->method('runCommand')
451
                        ->with(null, array())
452
                        ->will($this->returnValue(true));
453

    
454
                $Dispatcher->TestShell = $Shell;
455

    
456
                $Dispatcher->args = array('mock_with_main');
457
                $result = $Dispatcher->dispatch();
458
                $this->assertTrue($result);
459
                $this->assertEquals(array(), $Dispatcher->args);
460
        }
461

    
462
/**
463
 * Verify correct dispatch of Shell subclasses without a main method
464
 *
465
 * @return void
466
 */
467
        public function testDispatchShellWithoutMain() {
468
                $Dispatcher = new TestShellDispatcher();
469
                $Shell = $this->getMock('Shell');
470

    
471
                $Shell->expects($this->once())->method('initialize');
472
                $Shell->expects($this->once())->method('runCommand')
473
                        ->with('initdb', array('initdb'))
474
                        ->will($this->returnValue(true));
475

    
476
                $Dispatcher->TestShell = $Shell;
477

    
478
                $Dispatcher->args = array('mock_without_main', 'initdb');
479
                $result = $Dispatcher->dispatch();
480
                $this->assertTrue($result);
481
        }
482

    
483
/**
484
 * Verify correct dispatch of custom classes with a main method
485
 *
486
 * @return void
487
 */
488
        public function testDispatchNotAShellWithMain() {
489
                $Dispatcher = new TestShellDispatcher();
490
                $methods = get_class_methods('Object');
491
                array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
492
                $Shell = $this->getMock('Object', $methods);
493

    
494
                $Shell->expects($this->never())->method('initialize');
495
                $Shell->expects($this->once())->method('startup');
496
                $Shell->expects($this->once())->method('main')->will($this->returnValue(true));
497
                $Dispatcher->TestShell = $Shell;
498

    
499
                $Dispatcher->args = array('mock_with_main_not_a');
500
                $result = $Dispatcher->dispatch();
501
                $this->assertTrue($result);
502
                $this->assertEquals(array(), $Dispatcher->args);
503

    
504
                $Shell = $this->getMock('Object', $methods);
505
                $Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
506
                $Shell->expects($this->once())->method('startup');
507
                $Dispatcher->TestShell = $Shell;
508

    
509
                $Dispatcher->args = array('mock_with_main_not_a', 'initdb');
510
                $result = $Dispatcher->dispatch();
511
                $this->assertTrue($result);
512
        }
513

    
514
/**
515
 * Verify correct dispatch of custom classes without a main method
516
 *
517
 * @return void
518
 */
519
        public function testDispatchNotAShellWithoutMain() {
520
                $Dispatcher = new TestShellDispatcher();
521
                $methods = get_class_methods('Object');
522
                array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
523
                $Shell = $this->getMock('Object', $methods);
524

    
525
                $Shell->expects($this->never())->method('initialize');
526
                $Shell->expects($this->once())->method('startup');
527
                $Shell->expects($this->once())->method('main')->will($this->returnValue(true));
528
                $Dispatcher->TestShell = $Shell;
529

    
530
                $Dispatcher->args = array('mock_without_main_not_a');
531
                $result = $Dispatcher->dispatch();
532
                $this->assertTrue($result);
533
                $this->assertEquals(array(), $Dispatcher->args);
534

    
535
                $Shell = $this->getMock('Object', $methods);
536
                $Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
537
                $Shell->expects($this->once())->method('startup');
538
                $Dispatcher->TestShell = $Shell;
539

    
540
                $Dispatcher->args = array('mock_without_main_not_a', 'initdb');
541
                $result = $Dispatcher->dispatch();
542
                $this->assertTrue($result);
543
        }
544

    
545
/**
546
 * Verify shifting of arguments
547
 *
548
 * @return void
549
 */
550
        public function testShiftArgs() {
551
                $Dispatcher = new TestShellDispatcher();
552

    
553
                $Dispatcher->args = array('a', 'b', 'c');
554
                $this->assertEquals('a', $Dispatcher->shiftArgs());
555
                $this->assertSame($Dispatcher->args, array('b', 'c'));
556

    
557
                $Dispatcher->args = array('a' => 'b', 'c', 'd');
558
                $this->assertEquals('b', $Dispatcher->shiftArgs());
559
                $this->assertSame($Dispatcher->args, array('c', 'd'));
560

    
561
                $Dispatcher->args = array('a', 'b' => 'c', 'd');
562
                $this->assertEquals('a', $Dispatcher->shiftArgs());
563
                $this->assertSame($Dispatcher->args, array('b' => 'c', 'd'));
564

    
565
                $Dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
566
                $this->assertEquals('a', $Dispatcher->shiftArgs());
567
                $this->assertSame($Dispatcher->args, array(0 => 'b', 1 => 'c'));
568

    
569
                $Dispatcher->args = array();
570
                $this->assertNull($Dispatcher->shiftArgs());
571
                $this->assertSame(array(), $Dispatcher->args);
572
        }
573

    
574
}