Browse Source

Merge pull request #3359 from cakephp/3.0-shell-bake

3.0 shell bake
José Lorenzo Rodríguez 12 years ago
parent
commit
759f595bc4

+ 52 - 0
src/Console/Command/Task/ShellTask.php

@@ -0,0 +1,52 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Console\Command\Task;
+
+use Cake\Console\Command\Task\SimpleBakeTask;
+
+/**
+ * Shell code generator.
+ */
+class ShellTask extends SimpleBakeTask {
+
+/**
+ * Task name used in path generation.
+ *
+ * @var string
+ */
+	public $pathFragment = 'Console/Command/';
+
+/**
+ * {@inheritDoc}
+ */
+	public function name() {
+		return 'shell';
+	}
+
+/**
+ * {@inheritDoc}
+ */
+	public function fileName($name) {
+		return $name . 'Shell.php';
+	}
+
+/**
+ * {@inheritDoc}
+ */
+	public function template() {
+		return 'shell';
+	}
+
+}

+ 3 - 0
src/Console/Command/Task/SimpleBakeTask.php

@@ -74,6 +74,9 @@ abstract class SimpleBakeTask extends BakeTask {
  */
 	public function execute($name = null) {
 		parent::execute();
+		if (empty($name)) {
+			return $this->error('You must provide a name to bake a ' . $this->name());
+		}
 		$name = Inflector::classify($name);
 		$this->bake($name);
 		$this->bakeTest($name);

+ 8 - 2
src/Console/Command/Task/TestTask.php

@@ -50,7 +50,8 @@ class TestTask extends BakeTask {
 		'Controller' => 'Controller',
 		'Component' => 'Controller\Component',
 		'Behavior' => 'Model\Behavior',
-		'Helper' => 'View\Helper'
+		'Helper' => 'View\Helper',
+		'Shell' => 'Console\Command',
 	];
 
 /**
@@ -64,7 +65,8 @@ class TestTask extends BakeTask {
 		'controller' => 'Controller',
 		'component' => 'Component',
 		'behavior' => 'Behavior',
-		'helper' => 'Helper'
+		'helper' => 'Helper',
+		'shell' => 'Shell',
 	];
 
 /**
@@ -429,6 +431,10 @@ class TestTask extends BakeTask {
 			$pre = "\$registry = new ComponentRegistry();\n";
 			$construct = "new {$className}(\$registry);\n";
 		}
+		if ($type === 'shell') {
+			$pre = "\$this->io = \$this->getMock('Cake\Console\ConsoleIo');\n";
+			$construct = "new {$className}(\$this->io);\n";
+		}
 		return [$pre, $construct, $post];
 	}
 

+ 34 - 0
src/Console/Templates/default/classes/shell.ctp

@@ -0,0 +1,34 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+echo "<?php\n";
+?>
+namespace <?= $namespace ?>\Console\Command;
+
+use Cake\Console\Shell;
+
+/**
+ * <?= $name ?> shell command.
+ */
+class <?= $name ?>Shell extends Shell {
+
+/**
+ * main() method.
+ *
+ * @return bool|int Success or error code.
+ */
+	public function main() {
+	}
+}

+ 2 - 1
tests/TestCase/Console/Command/BakeShellTest.php

@@ -104,7 +104,7 @@ class BakeShellTest extends TestCase {
 			->method('out')
 			->with($this->stringContains('The following commands'));
 
-		$this->Shell->expects($this->exactly(17))
+		$this->Shell->expects($this->exactly(18))
 			->method('out');
 
 		$this->Shell->loadTasks();
@@ -142,6 +142,7 @@ class BakeShellTest extends TestCase {
 			'Model',
 			'Plugin',
 			'Project',
+			'Shell',
 			'Test',
 			'View',
 			'Zerg',

+ 2 - 2
tests/TestCase/Console/Command/CompletionShellTest.php

@@ -164,7 +164,7 @@ class CompletionShellTest extends TestCase {
 		$this->Shell->runCommand('subCommands', array('subCommands', 'CORE.bake'));
 		$output = $this->out->output;
 
-		$expected = "behavior component controller fixture helper model plugin project test view widget zerg\n";
+		$expected = "behavior component controller fixture helper model plugin project shell test view widget zerg\n";
 		$this->assertEquals($expected, $output);
 	}
 
@@ -229,7 +229,7 @@ class CompletionShellTest extends TestCase {
 		$this->Shell->runCommand('subCommands', array('subCommands', 'bake'));
 		$output = $this->out->output;
 
-		$expected = "behavior component controller fixture helper model plugin project test view widget zerg\n";
+		$expected = "behavior component controller fixture helper model plugin project shell test view widget zerg\n";
 		$this->assertEquals($expected, $output);
 	}
 

+ 27 - 0
tests/TestCase/Console/Command/Task/TestTaskTest.php

@@ -273,6 +273,8 @@ class TestTaskTest extends TestCase {
 			['helper', 'FormHelper', 'App\View\Helper\FormHelper'],
 			['Component', 'Auth', 'App\Controller\Component\AuthComponent'],
 			['component', 'AuthComponent', 'App\Controller\Component\AuthComponent'],
+			['Shell', 'Example', 'App\Console\Command\ExampleShell'],
+			['shell', 'Example', 'App\Console\Command\ExampleShell'],
 		];
 	}
 
@@ -472,6 +474,29 @@ class TestTaskTest extends TestCase {
 	}
 
 /**
+ * Test baking a test for a concrete model.
+ *
+ * @return void
+ */
+	public function testBakeShellTest() {
+		$this->Task->expects($this->once())
+			->method('createFile')
+			->will($this->returnValue(true));
+
+		$result = $this->Task->bake('Shell', 'Articles');
+
+		$this->assertContains("use App\Console\Command\ArticlesShell", $result);
+		$this->assertContains('class ArticlesShellTest extends TestCase', $result);
+
+		$this->assertContains('function setUp()', $result);
+		$this->assertContains("\$this->io = \$this->getMock('Cake\Console\ConsoleIo');", $result);
+		$this->assertContains("\$this->Articles = new ArticlesShell(\$this->io);", $result);
+
+		$this->assertContains('function tearDown()', $result);
+		$this->assertContains('unset($this->Articles)', $result);
+	}
+
+/**
  * test Constructor generation ensure that constructClasses is called for controllers
  *
  * @return void
@@ -579,6 +604,8 @@ class TestTaskTest extends TestCase {
 			array('controller', 'App\Controller\PostsController', 'TestCase/Controller/PostsControllerTest.php'),
 			array('behavior', 'App\Model\Behavior\TreeBehavior', 'TestCase/Model/Behavior/TreeBehaviorTest.php'),
 			array('component', 'App\Controller\Component\AuthComponent', 'TestCase/Controller/Component/AuthComponentTest.php'),
+			['Shell', 'App\Console\Command\ExampleShell', 'TestCase/Console/Command/ExampleShellTest.php'],
+			['shell', 'App\Console\Command\ExampleShell', 'TestCase/Console/Command/ExampleShellTest.php'],
 		);
 	}