Skip to content

Commit 62867ed

Browse files
committed
Fix PluginTask to deny option of ROOT/plugins and fixed autoloader generation
Merge branch 'DSchalla/master' into master. Closes #26
2 parents 96ba8c7 + 37c1623 commit 62867ed

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

src/Shell/Task/PluginTask.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,11 @@ protected function _modifyAutoloader($plugin, $path)
215215
return false;
216216
}
217217

218+
$autoloadPath = str_replace(ROOT, '.', $this->path);
219+
218220
$config = json_decode(file_get_contents($file), true);
219-
$config['autoload']['psr-4'][$plugin . '\\'] = "./plugins/$plugin/src";
220-
$config['autoload-dev']['psr-4'][$plugin . '\\Test\\'] = "./plugins/$plugin/tests";
221+
$config['autoload']['psr-4'][$plugin . '\\'] = $autoloadPath . $plugin . "/src";
222+
$config['autoload-dev']['psr-4'][$plugin . '\\Test\\'] = $autoloadPath . $plugin . "/tests";
221223

222224
$this->out('<info>Modifying composer autoloader</info>');
223225

@@ -235,7 +237,7 @@ protected function _modifyAutoloader($plugin, $path)
235237
$cwd = getcwd();
236238

237239
// Windows makes running multiple commands at once hard.
238-
chdir(dirname($path));
240+
chdir(dirname($this->_rootComposerFilePath()));
239241
$command = 'php ' . escapeshellarg($composer) . ' dump-autoload';
240242
$this->callProcess($command);
241243

@@ -252,7 +254,7 @@ protected function _modifyAutoloader($plugin, $path)
252254
/**
253255
* The path to the main application's composer file
254256
*
255-
* This is a test issolation wrapper
257+
* This is a test isolation wrapper
256258
*
257259
* @return string the abs file path
258260
*/
@@ -276,8 +278,18 @@ public function findPath(array $pathOptions)
276278
}
277279
}
278280
$pathOptions = array_values($pathOptions);
279-
280281
$max = count($pathOptions);
282+
283+
if ($max == 0) {
284+
$this->err('No valid plugin paths found! Please configure a plugin path that exists.');
285+
throw new \RuntimeException();
286+
}
287+
288+
if ($max === 1) {
289+
$this->path = $pathOptions[0];
290+
return;
291+
}
292+
281293
while (!$valid) {
282294
foreach ($pathOptions as $i => $option) {
283295
$this->out($i + 1 . '. ' . $option);

tests/TestCase/Shell/Task/PluginTaskTest.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public function testMainUpdateComposer()
138138
$file = TMP . 'tests' . DS . 'main-composer.json';
139139
file_put_contents($file, '{}');
140140

141+
$savePath = $this->Task->path;
142+
143+
$this->Task->path = ROOT . DS . 'tests' . DS . 'BakedPlugins/';
144+
141145
$this->Task->expects($this->any())
142146
->method('_rootComposerFilePath')
143147
->will($this->returnValue($file));
@@ -150,17 +154,21 @@ public function testMainUpdateComposer()
150154

151155
$result = file_get_contents($file);
152156
$this->assertSameAsFile(__FUNCTION__ . '.json', $result);
157+
158+
$folder = new Folder($this->Task->path);
159+
$folder->delete();
160+
161+
$this->Task->path = $savePath;
153162
}
154163

155164
/**
156165
* Test that findPath ignores paths that don't exist.
157166
*
158167
* @return void
159168
*/
160-
public function testFindPathNonExistant()
169+
public function testFindPathNonExistent()
161170
{
162171
$paths = App::path('Plugin');
163-
$last = count($paths);
164172

165173
array_unshift($paths, '/fake/path');
166174
$paths[] = '/fake/path2';
@@ -172,13 +180,29 @@ public function testFindPathNonExistant()
172180
);
173181
$this->Task->path = TMP . 'tests' . DS;
174182

175-
// Make sure the added path is filtered out.
176-
$this->Task->expects($this->exactly($last))
177-
->method('out');
183+
$this->Task->method('findPath')
184+
->will($this->returnValue($paths[0]));
178185

179-
$this->Task->expects($this->once())
180-
->method('in')
181-
->will($this->returnValue($last));
186+
$this->Task->findPath($paths);
187+
}
188+
189+
/**
190+
* Test that findPath throws RunTimeException when no
191+
* path exists for plugins
192+
*
193+
* @expectedException \RunTimeException
194+
* @return void
195+
*/
196+
public function testFindPathEmpty()
197+
{
198+
$paths = ['/fake/path', '/fake/path2'];
199+
200+
$this->Task = $this->getMock(
201+
'Bake\Shell\Task\PluginTask',
202+
['in', 'out', 'err', '_stop'],
203+
[$this->io]
204+
);
205+
$this->Task->path = TMP . 'tests' . DS;
182206

183207
$this->Task->findPath($paths);
184208
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"autoload": {
33
"psr-4": {
4-
"ComposerExample\\": "./plugins/ComposerExample/src"
4+
"ComposerExample\\": "./tests/BakedPlugins/ComposerExample/src"
55
}
66
},
77
"autoload-dev": {
88
"psr-4": {
9-
"ComposerExample\\Test\\": "./plugins/ComposerExample/tests"
9+
"ComposerExample\\Test\\": "./tests/BakedPlugins/ComposerExample/tests"
1010
}
1111
}
1212
}

0 commit comments

Comments
 (0)