Browse Source

Merge pull request #4326 from renan/3.0-autoload-dev

Testing namespaces should be under autoload-dev
Mark Story 11 years ago
parent
commit
7328a266aa

+ 6 - 8
src/Console/Command/Task/PluginTask.php

@@ -239,22 +239,20 @@ class PluginTask extends BakeTask {
  */
 	protected function _modifyAutoloader($plugin, $path) {
 		$path = dirname($path);
+		$file = $path . DS . 'composer.json';
 
-		if (!file_exists($path . DS . 'composer.json')) {
+		if (!file_exists($file)) {
 			return false;
 		}
 
-		$file = $path . DS . 'composer.json';
 		$config = json_decode(file_get_contents($file), true);
 		$config['autoload']['psr-4'][$plugin . '\\'] = "./plugins/$plugin/src";
-		$config['autoload']['psr-4'][$plugin . '\\Test\\'] = "./plugins/$plugin/tests";
+		$config['autoload-dev']['psr-4'][$plugin . '\\Test\\'] = "./plugins/$plugin/tests";
 
 		$this->out('<info>Modifying composer autoloader</info>');
 
-		file_put_contents(
-			$file,
-			json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
-		);
+		$out = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
+		$this->createFile($file, $out);
 
 		$composer = $this->Project->findComposer();
 
@@ -265,7 +263,7 @@ class PluginTask extends BakeTask {
 
 		try {
 			$command = 'cd ' . escapeshellarg($path) . '; ';
-			$command .= 'php ' . escapeshellarg($composer) . ' dump-autoload ';
+			$command .= 'php ' . escapeshellarg($composer) . ' dump-autoload';
 			$this->callProcess($command);
 		} catch (\RuntimeException $e) {
 			$error = $e->getMessage();

+ 52 - 1
tests/TestCase/Console/Command/Task/PluginTaskTest.php

@@ -19,6 +19,7 @@ use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
 use Cake\TestSuite\TestCase;
+use Cake\Utility\File;
 use Cake\Utility\Folder;
 
 /**
@@ -36,7 +37,7 @@ class PluginTaskTest extends TestCase {
 		$this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
 
 		$this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
-			array('in', 'err', 'createFile', '_stop', 'clear'),
+			array('in', 'err', 'createFile', '_stop', 'clear', 'callProcess'),
 			array($this->io)
 		);
 		$this->Task->Template = new TemplateTask($this->io);
@@ -162,6 +163,56 @@ class PluginTaskTest extends TestCase {
 	}
 
 /**
+ * Test that baking a plugin for a project that contains a composer.json, the later
+ * will be updated
+ *
+ * @return void
+ */
+	public function testExecuteUpdateComposer() {
+		$this->Task->expects($this->at(0))->method('in')
+			->will($this->returnValue('y'));
+
+		$this->Task->Project = $this->getMock('ComposerProject', ['findComposer']);
+		$this->Task->Project->expects($this->at(0))
+			->method('findComposer')
+			->will($this->returnValue('composer.phar'));
+
+		$path = dirname($this->Task->path);
+		$file = $path . DS . 'composer.json';
+		file_put_contents($file, '{}');
+
+		$config = [
+			'autoload' => [
+				'psr-4' => [
+					'BakeTestPlugin\\' => './plugins/BakeTestPlugin/src',
+				],
+			],
+			'autoload-dev' => [
+				'psr-4' => [
+					'BakeTestPlugin\\Test\\' => './plugins/BakeTestPlugin/tests',
+				],
+			],
+		];
+		$config = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
+
+		$this->Task->expects($this->at(2))
+			->method('createFile')
+			->with($file, $config);
+
+		$this->Task->expects($this->at(3))
+			->method('callProcess')
+			->with("cd '$path'; php 'composer.phar' dump-autoload");
+
+		$this->Task->main('BakeTestPlugin');
+
+		$Folder = new Folder($this->Task->path . 'BakeTestPlugin');
+		$Folder->delete();
+
+		$File = new File($file);
+		$File->delete();
+	}
+
+/**
  * Test that findPath ignores paths that don't exist.
  *
  * @return void