Skip to content

Commit 10af465

Browse files
committed
Update namespaces and initSUT() to be namespace-aware.
1 parent 002a289 commit 10af465

2 files changed

Lines changed: 87 additions & 40 deletions

File tree

src/Shell/ConfigReadShell.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* ConfigReadShell
44
*
5-
* @package App.Console.Command
5+
* @package ConfigRead\Shell
66
*/
77
namespace ConfigRead\Shell;
88

@@ -147,7 +147,7 @@ protected function printVal($key, $val) {
147147

148148
if ($this->formatBash) {
149149
$key = strtoupper(str_replace('.', '_', $key));
150-
$format = ($this->formatBash ? '%1$s=%2$s' : '%2$s');
150+
$format = '%1$s=%2$s';
151151
}
152152

153153
$this->out(sprintf($format, $key, $val));
@@ -160,6 +160,7 @@ protected function printVal($key, $val) {
160160
*
161161
* @access public
162162
* @return void
163+
* @codeCoverageIgnore
163164
*/
164165
public function getOptionParser() {
165166
$parser = parent::getOptionParser();

tests/TestCase/Shell/ConfigReadShellTest.php

Lines changed: 84 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
*
44
*/
5-
namespace ConfigReadShell\Test\TestCase\Shell;
5+
namespace ConfigRead\Test\TestCase\Shell;
66

77
use ConfigRead\Shell\ConfigReadShell;
88

@@ -23,6 +23,8 @@
2323
*/
2424
class TestConfigReadShell extends ConfigReadShell
2525
{
26+
public $formatBash = false;
27+
2628
public function fetchVal($key) {
2729
return parent::fetchVal($key);
2830
}
@@ -75,6 +77,49 @@ public function tearDown()
7577
parent::tearDown();
7678
}
7779

80+
/**
81+
* Helper for determing the subject class to initialize for testing.
82+
*
83+
* Methodology:
84+
* - Take the name of this testing class: `SomeObjectTest`
85+
* - If there exists a `TestSomeObject` class (presumed to extend
86+
* SomeObject to expose private/protected methods for testing)
87+
* then return that.
88+
* - Otherwise, guess the namespace of the subject class by
89+
* removing `*\Test\TestCase\*\*Test` from this testing classes
90+
* name and use that.
91+
* - As a side-effect, set a local class property with the
92+
* non-namespaced `SomeObject` name for future reference in tests.
93+
*
94+
* @return string The fully-namespaced class name to instantiate.
95+
* @see ::initSUT()
96+
*/
97+
protected function getSUTClassName()
98+
{
99+
$testCaseClass = get_class($this);
100+
// -> ConfigRead\Test\TestCase\Shell\ConfigReadShellTest
101+
102+
$testingOverrideClass = preg_replace(
103+
'/^(.*)\\\([^\\\]+)Test$/',
104+
'\1\\\Test\2',
105+
$testCaseClass
106+
); // -> ConfigRead\Test\TestCase\Shell\TestConfigReadShell
107+
108+
$testedClass = preg_replace(
109+
'/^(.*)\\\Test\\\TestCase\\\(.*)Test$/',
110+
'\1\\\\\2',
111+
$testCaseClass
112+
); // -> ConfigRead\Shell\ConfigReadShell
113+
114+
$this->classBasename = preg_replace(
115+
'/^.*\\\([^\\\]+)$/',
116+
'\1',
117+
$testedClass
118+
); // -> ConfigReadShell
119+
120+
return (class_exists($testingOverrideClass) ? $testingOverrideClass : $testedClass);
121+
}
122+
78123
/**
79124
* Helper for setting up an instance of the target Shell with proper
80125
* mocked methods.
@@ -94,19 +139,17 @@ public function tearDown()
94139
* @return mixed A partially mocked copy of the Shell matching the test class's name.
95140
*/
96141
protected function initSUT($additionalMocks = []) {
97-
$defaultMocks = array(
98-
'in', 'out', 'hr', 'help', 'error', 'err', '_stop', 'initialize', '_run', 'clear',
99-
);
142+
$defaultMocks = [
143+
'help', //'in', 'out', 'hr', 'error', 'err', '_stop', 'initialize', '_run', 'clear',
144+
];
100145
$this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
101146

102-
$class = preg_replace('/(.*)Test$/', '\1', get_class($this));
103-
$testClass = preg_replace('/(.*)\\\([^\\\]+)$/', '\1\\\Test\2', $class);
104-
$class = (class_exists($testClass) ? $testClass : $class);
105-
147+
$class = $this->getSUTClassName();
106148
$shell = $this->getMock(
107149
$class,
108150
array_merge($defaultMocks, $additionalMocks),
109-
[$this->io]
151+
[$this->io],
152+
"Mock_{$this->classBasename}"
110153
);
111154

112155
$shell->OptionParser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [null, false]);
@@ -138,45 +181,47 @@ public function testStartupHelp()
138181
);
139182
}
140183

141-
/*
142-
public function startup() {
143-
parent::startup();
144-
145-
//Configure::write('debug', 0);
146-
147-
if (isset($this->params['h'])) {
148-
return $this->help();
149-
}
150-
151-
if (isset($this->params['b'])) {
152-
$this->formatBash = true;
153-
// Make up for Cake snagging the next arg as the value for `-b`.
154-
array_unshift($this->args, $this->params['b']);
155-
}
156-
157-
if (count($this->args) > 1) {
158-
$this->formatBash = true;
159-
}
160-
}
184+
/**
185+
* Confirm that startup() engages bash output mode when -b flag is present.
186+
*
187+
* @return void
188+
*/
189+
public function testStartupBashModeFlag()
190+
{
191+
$this->Shell->params = ['b' => 'canary'];
161192

162-
*/
193+
$this->Shell->startup();
194+
195+
$this->assertEquals(
196+
['canary'],
197+
$this->Shell->args,
198+
'Shell args should contain the value mistakenly captured by the -b option.'
199+
);
200+
$this->assertTrue(
201+
$this->Shell->formatBash,
202+
'Bash output formatting should be enabled by the presence of the -b option.'
203+
);
204+
}
163205

164206
/**
165-
* testOut method
207+
* Confirm that startup() engages bash output mode when multiple args are present.
166208
*
167209
* @return void
168210
*/
169-
public function testOut()
211+
public function testStartupBashModeMultiArgs()
170212
{
171-
$this->io->expects($this->once())
172-
->method('out')
173-
->with('Just a test', 1);
213+
$this->Shell->args = ['debug', 'Datasources.default.host'];
174214

175-
$this->Shell->out('Just a test');
215+
$this->Shell->startup();
216+
217+
$this->assertTrue(
218+
$this->Shell->formatBash,
219+
'Bash output formatting should be enabled by the presence of multiple arguments.'
220+
);
176221
}
177222

178223
/**
179-
* test main.
224+
* test main().
180225
*
181226
* @return void
182227
*/
@@ -254,7 +299,8 @@ public function testDebugInfo()
254299
'tasks' => [],
255300
'params' => [],
256301
'args' => [],
257-
'interactive' => true
302+
'interactive' => true,
303+
'name' => str_replace('Shell', '', "Mock_{$this->classBasename}"),
258304
];
259305
$result = $this->Shell->__debugInfo();
260306
$this->assertEquals($expected, $result);

0 commit comments

Comments
 (0)