-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ShellDispatcherTest.php
252 lines (216 loc) · 7.13 KB
/
ShellDispatcherTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Console;
use Cake\Console\ShellDispatcher;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\TestSuite\TestCase;
/**
* ShellDispatcherTest
*
*/
class ShellDispatcherTest extends TestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Plugin::load('TestPlugin');
Configure::write('App.namespace', 'TestApp');
$this->dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['_stop']);
}
/**
* teardown
*
* @return void
*/
public function tearDown() {
parent::tearDown();
ShellDispatcher::resetAliases();
}
/**
* Test error on missing shell
*
* @expectedException Cake\Console\Exception\MissingShellException
* @return void
*/
public function testFindShellMissing() {
$this->dispatcher->findShell('nope');
}
/**
* Test error on missing plugin shell
*
* @expectedException Cake\Console\Exception\MissingShellException
* @return void
*/
public function testFindShellMissingPlugin() {
$this->dispatcher->findShell('test_plugin.nope');
}
/**
* Verify loading of (plugin-) shells
*
* @return void
*/
public function testFindShell() {
$result = $this->dispatcher->findShell('sample');
$this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
$result = $this->dispatcher->findShell('test_plugin.example');
$this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
$this->assertEquals('TestPlugin', $result->plugin);
$this->assertEquals('Example', $result->name);
$result = $this->dispatcher->findShell('TestPlugin.example');
$this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
$result = $this->dispatcher->findShell('TestPlugin.Example');
$this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
}
/**
* Test getting shells with aliases.
*
* @return void
*/
public function testFindShellAliased() {
ShellDispatcher::alias('short', 'test_plugin.example');
$result = $this->dispatcher->findShell('short');
$this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
$this->assertEquals('TestPlugin', $result->plugin);
$this->assertEquals('Example', $result->name);
}
/**
* Test finding a shell that has a matching alias.
*
* Aliases should not overload concrete shells.
*
* @return void
*/
public function testFindShellAliasedAppShadow() {
ShellDispatcher::alias('sample', 'test_plugin.example');
$result = $this->dispatcher->findShell('sample');
$this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
$this->assertEmpty($result->plugin);
$this->assertEquals('Sample', $result->name);
}
/**
* Verify correct dispatch of Shell subclasses with a main method
*
* @return void
*/
public function testDispatchShellWithMain() {
$dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
$Shell = $this->getMock('Cake\Console\Shell');
$Shell->expects($this->once())->method('initialize');
$Shell->expects($this->once())->method('runCommand')
->with([])
->will($this->returnValue(true));
$dispatcher->expects($this->any())
->method('findShell')
->with('mock_with_main')
->will($this->returnValue($Shell));
$dispatcher->args = array('mock_with_main');
$result = $dispatcher->dispatch();
$this->assertEquals(0, $result);
$this->assertEquals(array(), $dispatcher->args);
}
/**
* Verify correct dispatch of Shell subclasses without a main method
*
* @return void
*/
public function testDispatchShellWithoutMain() {
$dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
$Shell = $this->getMock('Cake\Console\Shell');
$Shell->expects($this->once())->method('initialize');
$Shell->expects($this->once())->method('runCommand')
->with(['initdb'])
->will($this->returnValue(true));
$dispatcher->expects($this->any())
->method('findShell')
->with('mock_without_main')
->will($this->returnValue($Shell));
$dispatcher->args = array('mock_without_main', 'initdb');
$result = $dispatcher->dispatch();
$this->assertEquals(0, $result);
}
/**
* Verify you can dispatch a plugin's main shell with the plugin name alone
*
* @return void
*/
public function testDispatchShortPluginAlias() {
$dispatcher = $this->getMock(
'Cake\Console\ShellDispatcher',
['_shellExists', '_createShell']
);
$Shell = $this->getMock('Cake\Console\Shell');
$dispatcher->expects($this->at(1))
->method('_shellExists')
->with('TestPlugin.TestPlugin')
->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
$dispatcher->expects($this->at(2))
->method('_createShell')
->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.TestPlugin')
->will($this->returnValue($Shell));
$dispatcher->args = array('test_plugin');
$result = $dispatcher->dispatch();
$this->assertEquals(1, $result);
}
/**
* Ensure short plugin shell usage is case/camelized insensitive
*
* @return void
*/
public function testDispatchShortPluginAliasCamelized() {
$dispatcher = $this->getMock(
'Cake\Console\ShellDispatcher',
['_shellExists', '_createShell']
);
$Shell = $this->getMock('Cake\Console\Shell');
$dispatcher->expects($this->at(1))
->method('_shellExists')
->with('TestPlugin.TestPlugin')
->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
$dispatcher->expects($this->at(2))
->method('_createShell')
->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.TestPlugin')
->will($this->returnValue($Shell));
$dispatcher->args = ['TestPlugin'];
$result = $dispatcher->dispatch();
$this->assertEquals(1, $result);
}
/**
* Verify shifting of arguments
*
* @return void
*/
public function testShiftArgs() {
$this->dispatcher->args = array('a', 'b', 'c');
$this->assertEquals('a', $this->dispatcher->shiftArgs());
$this->assertSame($this->dispatcher->args, array('b', 'c'));
$this->dispatcher->args = array('a' => 'b', 'c', 'd');
$this->assertEquals('b', $this->dispatcher->shiftArgs());
$this->assertSame($this->dispatcher->args, array('c', 'd'));
$this->dispatcher->args = array('a', 'b' => 'c', 'd');
$this->assertEquals('a', $this->dispatcher->shiftArgs());
$this->assertSame($this->dispatcher->args, array('b' => 'c', 'd'));
$this->dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
$this->assertEquals('a', $this->dispatcher->shiftArgs());
$this->assertSame($this->dispatcher->args, array(0 => 'b', 1 => 'c'));
$this->dispatcher->args = array();
$this->assertNull($this->dispatcher->shiftArgs());
$this->assertSame(array(), $this->dispatcher->args);
}
}