Browse Source

Merge pull request #3217 from markstory/3.0-behavior-bake

3.0 behavior bake
José Lorenzo Rodríguez 12 years ago
parent
commit
08e5c05313

+ 5 - 1
src/Console/Command/BakeShell.php

@@ -38,7 +38,7 @@ class BakeShell extends Shell {
  *
  * @var array
  */
-	public $tasks = ['Project', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test'];
+	public $tasks = ['Behavior', 'Project', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test'];
 
 /**
  * The connection being used.
@@ -83,6 +83,7 @@ class BakeShell extends Shell {
 		$this->out(__d('cake_console', 'The following commands you can generate skeleton code your your application.'));
 		$this->out(__d('cake_console', 'Available bake commands:'));
 		$this->out('');
+		$this->out(__d('cake_console', 'behavior'));
 		$this->out(__d('cake_console', 'model'));
 		$this->out(__d('cake_console', 'view'));
 		$this->out(__d('cake_console', 'controller'));
@@ -154,6 +155,9 @@ class BakeShell extends Shell {
 		])->addSubcommand('plugin', [
 			'help' => __d('cake_console', 'Bake a new plugin folder in the path supplied or in current directory if no path is specified.'),
 			'parser' => $this->Plugin->getOptionParser()
+		])->addSubcommand('behavior', [
+			'help' => __d('cake_console', 'Bake a behavior.'),
+			'parser' => $this->Behavior->getOptionParser()
 		])->addSubcommand('model', [
 			'help' => __d('cake_console', 'Bake a model.'),
 			'parser' => $this->Model->getOptionParser()

+ 7 - 0
src/Console/Command/Task/BakeTask.php

@@ -27,6 +27,13 @@ use Cake\Core\Configure;
 class BakeTask extends Shell {
 
 /**
+ * The default path to bake files into.
+ *
+ * @var string
+ */
+	public $path;
+
+/**
  * Name of plugin
  *
  * @var string

+ 125 - 0
src/Console/Command/Task/BehaviorTask.php

@@ -0,0 +1,125 @@
+<?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\BakeTask;
+use Cake\Core\App;
+use Cake\Core\Configure;
+use Cake\Core\Plugin;
+use Cake\Utility\Inflector;
+
+/**
+ * Behavior code generator.
+ */
+class BehaviorTask extends BakeTask {
+
+/**
+ * Tasks to be loaded by this Task
+ *
+ * @var array
+ */
+	public $tasks = ['Test', 'Template'];
+
+/**
+ * Task name used in path generation.
+ *
+ * @var string
+ */
+	public $name = 'Model/Behavior';
+
+/**
+ * Override initialize
+ *
+ * @return void
+ */
+	public function initialize() {
+		$this->path = current(App::path('Model/Behavior'));
+	}
+
+/**
+ * Execute method
+ *
+ * @return void
+ */
+	public function execute() {
+		parent::execute();
+		$name = Inflector::classify($this->args[0]);
+		$this->bake($name);
+		$this->bakeTest($name);
+	}
+
+/**
+ * Generate a class stub
+ *
+ * @param string $className The classname to generate.
+ * @return void
+ */
+	public function bake($name) {
+		$namespace = Configure::read('App.namespace');
+		if ($this->plugin) {
+			$namespace = Plugin::getNamespace($this->plugin);
+		}
+		$data = compact('name', 'namespace');
+		$this->Template->set($data);
+		$contents = $this->Template->generate('classes', 'behavior');
+
+		$path = $this->getPath();
+		$filename = $path . $name . 'Behavior.php';
+		$this->createFile($filename, $contents);
+		return $contents;
+	}
+
+/**
+ * Generate a test case.
+ *
+ * @return void
+ */
+	public function bakeTest($className) {
+		if (!empty($this->params['no-test'])) {
+			return;
+		}
+		$this->Test->plugin = $this->plugin;
+		return $this->Test->bake('Behavior', $className);
+	}
+
+/**
+ * Gets the option parser instance and configures it.
+ *
+ * @return \Cake\Console\ConsoleOptionParser
+ */
+	public function getOptionParser() {
+		$parser = parent::getOptionParser();
+		$parser->description(
+			__d('cake_console', 'Bake a behavior class file.')
+		)->addArgument('name', [
+			'help' => __d('cake_console', 'Name of the Behavior to bake. Can use Plugin.name to bake controllers into plugins.')
+		])->addOption('plugin', [
+			'short' => 'p',
+			'help' => __d('cake_console', 'Plugin to bake the behavior into.')
+		])->addOption('theme', [
+			'short' => 't',
+			'help' => __d('cake_console', 'Theme to use when baking code.')
+		])->addOption('no-test', [
+			'boolean' => true,
+			'help' => __d('cake_console', 'Do not generate a test skeleton.')
+		])->addOption('force', [
+			'short' => 'f',
+			'boolean' => true,
+			'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
+		]);
+
+		return $parser;
+	}
+}

+ 0 - 8
src/Console/Command/Task/ControllerTask.php

@@ -18,7 +18,6 @@ use Cake\Console\Shell;
 use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\ORM\TableRegistry;
-use Cake\Utility\ClassRegistry;
 use Cake\Utility\Inflector;
 
 /**
@@ -35,13 +34,6 @@ class ControllerTask extends BakeTask {
 	public $tasks = ['Model', 'Test', 'Template'];
 
 /**
- * path to Controller directory
- *
- * @var array
- */
-	public $path = null;
-
-/**
  * Override initialize
  *
  * @return void

+ 25 - 0
src/Console/Templates/default/classes/behavior.ctp

@@ -0,0 +1,25 @@
+<?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
+ */
+echo "<?php\n"; ?>
+namespace <?= $namespace ?>\Model\Behavior;
+
+use Cake\ORM\Behavior;
+
+/**
+ * <?= $name ?> behavior
+ */
+class <?= $name ?>Behavior extends Behavior {
+
+}

+ 7 - 3
src/TestSuite/Fixture/FixtureInjector.php

@@ -130,8 +130,10 @@ class FixtureInjector implements PHPUnit_Framework_TestListener {
  */
 	public function startTest(PHPUnit_Framework_Test $test) {
 		$test->fixtureManager = $this->_fixtureManager;
-		$this->_fixtureManager->fixturize($test);
-		$this->_fixtureManager->load($test);
+		if ($test instanceof TestCase) {
+			$this->_fixtureManager->fixturize($test);
+			$this->_fixtureManager->load($test);
+		}
 	}
 
 /**
@@ -142,7 +144,9 @@ class FixtureInjector implements PHPUnit_Framework_TestListener {
  * @return void
  */
 	public function endTest(PHPUnit_Framework_Test $test, $time) {
-		$this->_fixtureManager->unload($test);
+		if ($test instanceof TestCase) {
+			$this->_fixtureManager->unload($test);
+		}
 	}
 
 /**

+ 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->Shell->stdout->output;
 
-		$expected = "controller fixture model plugin project test view\n";
+		$expected = "behavior controller fixture model plugin project test view\n";
 		$this->assertEquals($expected, $output);
 	}
 
@@ -229,7 +229,7 @@ class CompletionShellTest extends TestCase {
 		$this->Shell->runCommand('subCommands', array('subCommands', 'bake'));
 		$output = $this->Shell->stdout->output;
 
-		$expected = "controller fixture model plugin project test view\n";
+		$expected = "behavior controller fixture model plugin project test view\n";
 		$this->assertEquals($expected, $output);
 	}
 

+ 0 - 1
tests/TestCase/Console/Command/Task/FixtureTaskTest.php

@@ -20,7 +20,6 @@ use Cake\Core\Plugin;
 use Cake\Datasource\ConnectionManager;
 use Cake\ORM\TableRegistry;
 use Cake\TestSuite\TestCase;
-use Cake\Utility\ClassRegistry;
 
 /**
  * FixtureTaskTest class