Skip to content

Commit cbce40a

Browse files
committed
Completed test coverage for current Shell.
Also fixes #2.
1 parent 10af465 commit cbce40a

2 files changed

Lines changed: 94 additions & 83 deletions

File tree

src/Shell/ConfigReadShell.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public function _welcome() {
4949
public function startup() {
5050
parent::startup();
5151

52-
Configure::write('debug', 0);
53-
5452
if (isset($this->params['h'])) {
5553
return $this->help();
5654
}

tests/TestCase/Shell/ConfigReadShellTest.php

Lines changed: 94 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77
use ConfigRead\Shell\ConfigReadShell;
88

99
use Cake\Console\ConsoleOptionParser;
10-
use Cake\Console\Shell;
11-
// use Cake\Core\App;
12-
// use Cake\Core\Configure;
13-
// use Cake\Core\Plugin;
14-
// use Cake\Filesystem\Folder;
15-
// use Cake\Log\Log;
10+
use \Cake\Console\Shell;
11+
use Cake\Core\Configure;
1612
use Cake\TestSuite\TestCase;
17-
// use Cake\Utility\Hash;
1813

1914
/**
2015
* TestConfigReadShell class
@@ -23,8 +18,6 @@
2318
*/
2419
class TestConfigReadShell extends ConfigReadShell
2520
{
26-
public $formatBash = false;
27-
2821
public function fetchVal($key) {
2922
return parent::fetchVal($key);
3023
}
@@ -53,6 +46,15 @@ class ConfigReadShellTest extends TestCase
5346
public $fixtures = [
5447
];
5548

49+
/**
50+
* Acts as an accumulator for output produced by the Shell.
51+
*
52+
* @var array
53+
* @see ::initSUIT()
54+
*/
55+
public $output = [
56+
];
57+
5658
/**
5759
* setUp method
5860
*
@@ -62,6 +64,7 @@ public function setUp()
6264
{
6365
parent::setUp();
6466

67+
$this->output = [];
6568
$this->Shell = $this->initSUT();
6669
}
6770

@@ -73,10 +76,22 @@ public function setUp()
7376
public function tearDown()
7477
{
7578
unset($this->io, $this->Shell);
79+
$this->output = [];
7680

7781
parent::tearDown();
7882
}
7983

84+
/**
85+
* Helper for accumulating I/O output generated by the Shell.
86+
*
87+
* @param string $s The output string being printed.
88+
* @see ::initSUT()
89+
*/
90+
public function outputCollector($s)
91+
{
92+
$this->output[] = $s;
93+
}
94+
8095
/**
8196
* Helper for determing the subject class to initialize for testing.
8297
*
@@ -96,7 +111,7 @@ public function tearDown()
96111
*/
97112
protected function getSUTClassName()
98113
{
99-
$testCaseClass = get_class($this);
114+
$testCaseClass = '\\' . get_class($this);
100115
// -> ConfigRead\Test\TestCase\Shell\ConfigReadShellTest
101116

102117
$testingOverrideClass = preg_replace(
@@ -142,18 +157,25 @@ protected function initSUT($additionalMocks = []) {
142157
$defaultMocks = [
143158
'help', //'in', 'out', 'hr', 'error', 'err', '_stop', 'initialize', '_run', 'clear',
144159
];
145-
$this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
160+
161+
$this->io = $this->getMock('\Cake\Console\ConsoleIo', [], [], '', false);
162+
$this->io->expects($this->any())
163+
->method('out')
164+
->with($this->anything())
165+
->will($this->returnCallback([$this, 'outputCollector']));
166+
146167

147168
$class = $this->getSUTClassName();
169+
$mockedMethods = array_merge($defaultMocks, $additionalMocks);
148170
$shell = $this->getMock(
149171
$class,
150-
array_merge($defaultMocks, $additionalMocks),
151-
[$this->io],
152-
"Mock_{$this->classBasename}"
172+
$mockedMethods,
173+
[$this->io]
153174
);
154175

155-
$shell->OptionParser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [null, false]);
176+
$shell->OptionParser = $this->getMock('\Cake\Console\ConsoleOptionParser', [], [null, false]);
156177

178+
//@TODO: Update this Cake2-centric code to load models in a Cake 3 Shell.
157179
// Load and attach all fixtures defined in this test case.
158180
// foreach ($this->fixtures as $fixture) {
159181
// $modelName = str_replace('App.', '', implode('.', array_map('Inflector::classify', explode('.', $fixture))));
@@ -227,82 +249,73 @@ public function testStartupBashModeMultiArgs()
227249
*/
228250
public function testMain()
229251
{
230-
$io = $this->getMock('Cake\Console\ConsoleIo');
231-
$shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
252+
$expected = [
253+
'key' => 'val',
254+
'debug' => true,
255+
'ary' => [
256+
'foo' => 'bar',
257+
'fizz' => 'buzz',
258+
],
259+
];
232260

233-
$shell->expects($this->once())->method('startup');
234-
$shell->expects($this->once())->method('main')
235-
->with('debug')
236-
->will($this->returnValue('canary'));
261+
$shell = $this->initSUT(['fetchVal', 'iterateOnKey']);
262+
$shell->args = array_keys($expected);
237263

238-
$result = $shell->runCommand(['debug', '--verbose']);
239-
$this->assertEquals('canary', $result);
240-
}
264+
$i = 0;
265+
foreach ($expected as $k => $v) {
266+
$shell->expects($this->at($i++))
267+
->method('fetchVal')
268+
->with($k)
269+
->will($this->returnValue($v));
270+
$shell->expects($this->at($i++))
271+
->method('iterateOnKey')
272+
->with($k, $v);
273+
}
241274

242-
/**
243-
* Test reading params
244-
*
245-
* @dataProvider paramReadingDataProvider
246-
*/
247-
public function testParamReading($toRead, $expected)
248-
{
249-
$this->Shell->params = [
250-
'key' => 'value',
251-
'help' => false,
252-
'emptykey' => '',
253-
'truthy' => true
254-
];
255-
$this->assertSame($expected, $this->Shell->param($toRead));
256-
}
275+
$shell->startup();
276+
$shell->main();
257277

258-
/**
259-
* Data provider for testing reading values with Shell::param()
260-
*
261-
* @return array
262-
*/
263-
public function paramReadingDataProvider()
264-
{
265-
return [
266-
[
267-
'key',
268-
'value',
269-
],
270-
[
271-
'help',
272-
false,
273-
],
274-
[
275-
'emptykey',
276-
'',
277-
],
278-
[
279-
'truthy',
280-
true,
281-
],
282-
[
283-
'does_not_exist',
284-
null,
285-
]
286-
];
278+
$this->assertTrue(
279+
$shell->formatBash,
280+
'Bash output should be engaged automatically by presence of multiple command line args.'
281+
);
287282
}
288283

289284
/**
290-
* Tests __debugInfo
285+
* test main(), including associated protected methods.
291286
*
292287
* @return void
293288
*/
294-
public function testDebugInfo()
289+
public function testMainIntegrationStyle()
295290
{
296-
$expected = [
297-
'plugin' => null,
298-
'command' => null,
299-
'tasks' => [],
300-
'params' => [],
301-
'args' => [],
302-
'interactive' => true,
303-
'name' => str_replace('Shell', '', "Mock_{$this->classBasename}"),
304-
];
305-
$result = $this->Shell->__debugInfo();
306-
$this->assertEquals($expected, $result);
291+
$configure = [
292+
'key' => 'val',
293+
'debug' => true,
294+
'ary' => [
295+
'foo' => 'bar',
296+
'fizz' => 'buzz',
297+
],
298+
];
299+
$expected = [
300+
"KEY='val'",
301+
"DEBUG='1'",
302+
"ARY_FOO='bar'",
303+
"ARY_FIZZ='buzz'",
304+
];
305+
Configure::write($configure);
306+
$this->Shell->args = array_keys($configure);
307+
308+
$this->Shell->startup();
309+
$this->Shell->main();
310+
311+
$this->assertEquals(
312+
$expected,
313+
$this->output,
314+
'The output produced from running the shell on the given arguments should match our expected result.'
315+
);
316+
$this->assertTrue(
317+
$this->Shell->formatBash,
318+
'Bash output should be engaged automatically by presence of multiple command line args.'
319+
);
307320
}
308321
}

0 commit comments

Comments
 (0)