|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + */ |
| 5 | +namespace ConfigReadShell\Test\TestCase\Shell; |
| 6 | + |
| 7 | +use ConfigRead\Shell\ConfigReadShell; |
| 8 | + |
| 9 | +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; |
| 16 | +use Cake\TestSuite\TestCase; |
| 17 | +// use Cake\Utility\Hash; |
| 18 | + |
| 19 | +/** |
| 20 | + * TestConfigReadShell class |
| 21 | + * |
| 22 | + * Exposes protected methods for direct testing. |
| 23 | + */ |
| 24 | +class TestConfigReadShell extends ConfigReadShell |
| 25 | +{ |
| 26 | + public function fetchVal($key) { |
| 27 | + return parent::fetchVal($key); |
| 28 | + } |
| 29 | + |
| 30 | + public function iterateOnKey($key, $val) { |
| 31 | + return parent::iterateOnKey($key, $val); |
| 32 | + } |
| 33 | + |
| 34 | + public function printVal($key, $val) { |
| 35 | + return parent::printVal($key, $val); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * ConfigReadShellTest class |
| 41 | + * |
| 42 | + */ |
| 43 | +class ConfigReadShellTest extends TestCase |
| 44 | +{ |
| 45 | + |
| 46 | + /** |
| 47 | + * Fixtures used in this test case |
| 48 | + * |
| 49 | + * @var array |
| 50 | + */ |
| 51 | + public $fixtures = [ |
| 52 | + ]; |
| 53 | + |
| 54 | + /** |
| 55 | + * setUp method |
| 56 | + * |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function setUp() |
| 60 | + { |
| 61 | + parent::setUp(); |
| 62 | + |
| 63 | + $this->Shell = $this->initSUT(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * tearDown method |
| 68 | + * |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public function tearDown() |
| 72 | + { |
| 73 | + unset($this->io, $this->Shell); |
| 74 | + |
| 75 | + parent::tearDown(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Helper for setting up an instance of the target Shell with proper |
| 80 | + * mocked methods. |
| 81 | + * |
| 82 | + * The Shell that will be mocked is taken from the test class name |
| 83 | + * automatically. Example: `SomeShellTest extends CakeTestCase` will |
| 84 | + * create a mocked copy of `SomeShell`. Will check for a subclassed |
| 85 | + * `TestSomeShell` and instantiate that instead, if available, to |
| 86 | + * allow for overriding protected methods. |
| 87 | + * |
| 88 | + * All of the fixtures defined in the test class will be "installed" |
| 89 | + * into the mocked Shell. |
| 90 | + * |
| 91 | + * Typically called in ::setUp() or at the beginning |
| 92 | + * of a test method (if additional mocked methods are necessary.) |
| 93 | + * |
| 94 | + * @return mixed A partially mocked copy of the Shell matching the test class's name. |
| 95 | + */ |
| 96 | + protected function initSUT($additionalMocks = []) { |
| 97 | + $defaultMocks = array( |
| 98 | + 'in', 'out', 'hr', 'help', 'error', 'err', '_stop', 'initialize', '_run', 'clear', |
| 99 | + ); |
| 100 | + $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false); |
| 101 | + |
| 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 | + |
| 106 | + $shell = $this->getMock( |
| 107 | + $class, |
| 108 | + array_merge($defaultMocks, $additionalMocks), |
| 109 | + [$this->io] |
| 110 | + ); |
| 111 | + |
| 112 | + $shell->OptionParser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [null, false]); |
| 113 | + |
| 114 | + // Load and attach all fixtures defined in this test case. |
| 115 | +// foreach ($this->fixtures as $fixture) { |
| 116 | +// $modelName = str_replace('App.', '', implode('.', array_map('Inflector::classify', explode('.', $fixture)))); |
| 117 | +// $propName = str_replace('.', '', $modelName); |
| 118 | +// $shell->{$propName} = ClassRegistry::init($modelName); |
| 119 | +// } |
| 120 | + return $shell; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Confirm that startup() engages help output when flag is present. |
| 125 | + * |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public function testStartupHelp() |
| 129 | + { |
| 130 | + $this->Shell->params = ['h' => true]; |
| 131 | + $this->Shell->expects($this->once()) |
| 132 | + ->method('help') |
| 133 | + ->will($this->returnValue('canary')); |
| 134 | + $this->assertEquals( |
| 135 | + 'canary', |
| 136 | + $this->Shell->startup(), |
| 137 | + 'Shell should return help() when -h is passed.' |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 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 | + } |
| 161 | +
|
| 162 | +*/ |
| 163 | + |
| 164 | + /** |
| 165 | + * testOut method |
| 166 | + * |
| 167 | + * @return void |
| 168 | + */ |
| 169 | + public function testOut() |
| 170 | + { |
| 171 | + $this->io->expects($this->once()) |
| 172 | + ->method('out') |
| 173 | + ->with('Just a test', 1); |
| 174 | + |
| 175 | + $this->Shell->out('Just a test'); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * test main. |
| 180 | + * |
| 181 | + * @return void |
| 182 | + */ |
| 183 | + public function testMain() |
| 184 | + { |
| 185 | + $io = $this->getMock('Cake\Console\ConsoleIo'); |
| 186 | + $shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]); |
| 187 | + |
| 188 | + $shell->expects($this->once())->method('startup'); |
| 189 | + $shell->expects($this->once())->method('main') |
| 190 | + ->with('debug') |
| 191 | + ->will($this->returnValue('canary')); |
| 192 | + |
| 193 | + $result = $shell->runCommand(['debug', '--verbose']); |
| 194 | + $this->assertEquals('canary', $result); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Test reading params |
| 199 | + * |
| 200 | + * @dataProvider paramReadingDataProvider |
| 201 | + */ |
| 202 | + public function testParamReading($toRead, $expected) |
| 203 | + { |
| 204 | + $this->Shell->params = [ |
| 205 | + 'key' => 'value', |
| 206 | + 'help' => false, |
| 207 | + 'emptykey' => '', |
| 208 | + 'truthy' => true |
| 209 | + ]; |
| 210 | + $this->assertSame($expected, $this->Shell->param($toRead)); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Data provider for testing reading values with Shell::param() |
| 215 | + * |
| 216 | + * @return array |
| 217 | + */ |
| 218 | + public function paramReadingDataProvider() |
| 219 | + { |
| 220 | + return [ |
| 221 | + [ |
| 222 | + 'key', |
| 223 | + 'value', |
| 224 | + ], |
| 225 | + [ |
| 226 | + 'help', |
| 227 | + false, |
| 228 | + ], |
| 229 | + [ |
| 230 | + 'emptykey', |
| 231 | + '', |
| 232 | + ], |
| 233 | + [ |
| 234 | + 'truthy', |
| 235 | + true, |
| 236 | + ], |
| 237 | + [ |
| 238 | + 'does_not_exist', |
| 239 | + null, |
| 240 | + ] |
| 241 | + ]; |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Tests __debugInfo |
| 246 | + * |
| 247 | + * @return void |
| 248 | + */ |
| 249 | + public function testDebugInfo() |
| 250 | + { |
| 251 | + $expected = [ |
| 252 | + 'plugin' => null, |
| 253 | + 'command' => null, |
| 254 | + 'tasks' => [], |
| 255 | + 'params' => [], |
| 256 | + 'args' => [], |
| 257 | + 'interactive' => true |
| 258 | + ]; |
| 259 | + $result = $this->Shell->__debugInfo(); |
| 260 | + $this->assertEquals($expected, $result); |
| 261 | + } |
| 262 | +} |
0 commit comments