Browse Source

Start fixing tests for ControllerTask.

mark_story 12 years ago
parent
commit
79d753c2a3

+ 28 - 85
src/Console/Command/Task/ControllerTask.php

@@ -56,42 +56,28 @@ class ControllerTask extends BakeTask {
 	public function execute() {
 		parent::execute();
 
-		if (empty($this->args)) {
-			return $this->_interactive();
+		if (!isset($this->connection)) {
+			$this->connection = 'default';
 		}
 
-		if (isset($this->args[0])) {
-			if (!isset($this->connection)) {
-				$this->connection = 'default';
-			}
-			if (strtolower($this->args[0]) === 'all') {
-				return $this->all();
+		if (empty($this->args)) {
+			$this->out(__d('cake_console', 'Possible Controllers based on your current database:'));
+			foreach ($this->listAll() as $table) {
+				$this->out('- ' . $this->_controllerName($table));
 			}
+			return true;
+		}
 
-			$controller = $this->_controllerName($this->args[0]);
-			$actions = '';
+		if (strtolower($this->args[0]) === 'all') {
+			return $this->all();
+		}
 
-			if (!empty($this->params['public'])) {
-				$this->out(__d('cake_console', 'Baking basic crud methods for ') . $controller);
-				$actions .= $this->bakeActions($controller);
-			}
-			if (!empty($this->params['admin'])) {
-				$admin = $this->Project->getPrefix();
-				if ($admin) {
-					$this->out(__d('cake_console', 'Adding %s methods', $admin));
-					$actions .= "\n" . $this->bakeActions($controller, $admin);
-				}
-			}
-			if (empty($actions)) {
-				$actions = 'scaffold';
-			}
+		$controller = $this->_controllerName($this->args[0]);
+		$this->out(__d('cake_console', 'Baking basic crud methods for ') . $controller);
+		$actions = $this->bakeActions($controller);
 
-			if ($this->bake($controller, $actions)) {
-				if ($this->_checkUnitTest()) {
-					$this->bakeTest($controller);
-				}
-			}
-		}
+		$this->bake($controller, $actions);
+		$this->bakeTest($controller);
 	}
 
 /**
@@ -100,10 +86,8 @@ class ControllerTask extends BakeTask {
  * @return void
  */
 	public function all() {
-		$this->interactive = false;
-		$this->listAll($this->connection, false);
+		$this->listAll();
 		ClassRegistry::config('Model', ['ds' => $this->connection]);
-		$unitTestExists = $this->_checkUnitTest();
 
 		$admin = false;
 		if (!empty($this->params['admin'])) {
@@ -121,9 +105,8 @@ class ControllerTask extends BakeTask {
 					$this->out(__d('cake_console', 'Adding %s methods', $admin));
 					$actions .= "\n" . $this->bakeActions($controller, $admin);
 				}
-				if ($this->bake($controller, $actions) && $unitTestExists) {
-					$this->bakeTest($controller);
-				}
+				$this->bake($controller, $actions);
+				$this->bakeTest($controller);
 				$controllersCreated++;
 			}
 		}
@@ -357,6 +340,9 @@ class ControllerTask extends BakeTask {
  * @return string Baked test
  */
 	public function bakeTest($className) {
+		if (!empty($this->params['no-test'])) {
+			return;
+		}
 		$this->Test->plugin = $this->plugin;
 		$this->Test->connection = $this->connection;
 		$this->Test->interactive = $this->interactive;
@@ -412,55 +398,9 @@ class ControllerTask extends BakeTask {
  * @param string $useDbConfig Database configuration name
  * @return array Set of controllers
  */
-	public function listAll($useDbConfig = null) {
-		if ($useDbConfig === null) {
-			$useDbConfig = $this->connection;
-		}
-		$this->__tables = $this->Model->getAllTables($useDbConfig);
-
-		if ($this->interactive) {
-			$this->out(__d('cake_console', 'Possible Controllers based on your current database:'));
-			$this->hr();
-			$this->_controllerNames = [];
-			$count = count($this->__tables);
-			for ($i = 0; $i < $count; $i++) {
-				$this->_controllerNames[] = $this->_controllerName($this->_modelName($this->__tables[$i]));
-				$this->out(sprintf("%2d. %s", $i + 1, $this->_controllerNames[$i]));
-			}
-			return $this->_controllerNames;
-		}
-		return $this->__tables;
-	}
-
-/**
- * Forces the user to specify the controller he wants to bake, and returns the selected controller name.
- *
- * @param string $useDbConfig Connection name to get a controller name for.
- * @return string Controller name
- */
-	public function getName($useDbConfig = null) {
-		$controllers = $this->listAll($useDbConfig);
-		$enteredController = '';
-
-		while (!$enteredController) {
-			$enteredController = $this->in(__d('cake_console', "Enter a number from the list above,\ntype in the name of another controller, or 'q' to exit"), null, 'q');
-			if ($enteredController === 'q') {
-				$this->out(__d('cake_console', 'Exit'));
-				return $this->_stop();
-			}
-
-			if (!$enteredController || intval($enteredController) > count($controllers)) {
-				$this->err(__d('cake_console', "The Controller name you supplied was empty,\nor the number you selected was not an option. Please try again."));
-				$enteredController = '';
-			}
-		}
-
-		if (intval($enteredController) > 0 && intval($enteredController) <= count($controllers)) {
-			$controllerName = $controllers[intval($enteredController) - 1];
-		} else {
-			$controllerName = Inflector::camelize($enteredController);
-		}
-		return $controllerName;
+	public function listAll() {
+		$this->Model->connection = $this->connection;
+		return $this->Model->listAll();
 	}
 
 /**
@@ -489,6 +429,9 @@ class ControllerTask extends BakeTask {
 			'help' => __d('cake_console', 'The comma separated list of helpers to use.')
 		])->addOption('prefix', [
 			'help' => __d('cake_console', 'The namespace/routing prefix to use.')
+		])->addOption('no-test', [
+			'boolean' => true,
+			'help' => __d('cake_console', 'Do not generate a test skeleton.')
 		])->addOption('force', [
 			'short' => 'f',
 			'help' => __d('cake_console', 'Force overwriting existing files without prompting.')

+ 49 - 66
tests/TestCase/Console/Command/Task/ControllerTaskTest.php

@@ -26,19 +26,19 @@ use Cake\View\Helper;
 
 /**
  * Class BakeArticle
- *
  */
 class BakeArticlesTable extends Table {
 
-	public $hasMany = array('BakeComment');
-
-	public $hasAndBelongsToMany = array('BakeTag');
+	public function initialize(array $config) {
+		$this->hasMany('BakeComments');
+		$this->belongsToMany('BakeTags');
+	}
 
 }
 
 class_alias(
 	'Cake\Test\TestCase\Console\Command\Task\BakeArticlesTable',
-	'Cake\Model\Repository\BakeArticlesTable'
+	'TestApp\Model\Table\BakeArticlesTable'
 );
 
 /**
@@ -52,7 +52,7 @@ class ControllerTaskTest extends TestCase {
  *
  * @var array
  */
-	public $fixtures = array('core.bake_article', 'core.bake_articles_bake_tag', 'core.bake_comment', 'core.bake_tag');
+	public $fixtures = ['core.bake_article', 'core.bake_articles_bake_tag', 'core.bake_comment', 'core.bake_tag'];
 
 /**
  * setUp method
@@ -61,24 +61,25 @@ class ControllerTaskTest extends TestCase {
  */
 	public function setUp() {
 		parent::setUp();
-		$this->markTestIncomplete('Baking will not work as models do not work.');
 
 		$out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
 		$in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
 		$this->Task = $this->getMock('Cake\Console\Command\Task\ControllerTask',
-			array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
+			array('in', 'out', 'err', 'hr', 'createFile', '_stop'),
 			array($out, $out, $in)
 		);
 		$this->Task->name = 'Controller';
+		$this->Task->connection = 'test';
+
 		$this->Task->Template = new TemplateTask($out, $out, $in);
 		$this->Task->Template->params['theme'] = 'default';
 
 		$this->Task->Model = $this->getMock('Cake\Console\Command\Task\ModelTask',
-			array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest'),
+			array('in', 'out', 'err', 'createFile', '_stop'),
 			array($out, $out, $in)
 		);
 		$this->Task->Project = $this->getMock('Cake\Console\Command\Task\ProjectTask',
-			array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getPrefix'),
+			array('in', 'out', 'err', 'createFile', '_stop', 'getPrefix'),
 			array($out, $out, $in)
 		);
 		$this->Task->Test = $this->getMock('Cake\Console\Command\Task\TestTask', array(), array($out, $out, $in));
@@ -105,70 +106,18 @@ class ControllerTaskTest extends TestCase {
 			$this->markTestSkipped('Additional tables detected.');
 		}
 
-		$this->Task->connection = 'test';
-		$this->Task->interactive = true;
-		$this->Task->expects($this->at(2))->method('out')->with(' 1. BakeArticles');
-		$this->Task->expects($this->at(3))->method('out')->with(' 2. BakeArticlesBakeTags');
-		$this->Task->expects($this->at(4))->method('out')->with(' 3. BakeComments');
-		$this->Task->expects($this->at(5))->method('out')->with(' 4. BakeTags');
-
-		$expected = array('BakeArticles', 'BakeArticlesBakeTags', 'BakeComments', 'BakeTags');
-		$result = $this->Task->listAll('test');
-		$this->assertEquals($expected, $result);
-
-		$this->Task->interactive = false;
 		$result = $this->Task->listAll();
-
 		$expected = array('bake_articles', 'bake_articles_bake_tags', 'bake_comments', 'bake_tags');
 		$this->assertEquals($expected, $result);
 	}
 
 /**
- * Test that getName interacts with the user and returns the controller name.
- *
- * @return void
- */
-	public function testGetNameValidIndex() {
-		$count = count($this->Task->listAll('test'));
-		if ($count != count($this->fixtures)) {
-			$this->markTestSkipped('Additional tables detected.');
-		}
-		$this->Task->interactive = true;
-		$this->Task->expects($this->any())->method('in')->will(
-			$this->onConsecutiveCalls(3, 1)
-		);
-
-		$result = $this->Task->getName('test');
-		$expected = 'BakeComments';
-		$this->assertEquals($expected, $result);
-
-		$result = $this->Task->getName('test');
-		$expected = 'BakeArticles';
-		$this->assertEquals($expected, $result);
-	}
-
-/**
- * test getting invalid indexes.
- *
- * @return void
- */
-	public function testGetNameInvalidIndex() {
-		$this->Task->interactive = true;
-		$this->Task->expects($this->any())->method('in')
-			->will($this->onConsecutiveCalls(50, 'q'));
-
-		$this->Task->expects($this->once())->method('err');
-		$this->Task->expects($this->once())->method('_stop');
-
-		$this->Task->getName('test');
-	}
-
-/**
  * test helper interactions
  *
  * @return void
  */
 	public function testDoHelpersNo() {
+		$this->markTestIncomplete();
 		$this->Task->expects($this->any())->method('in')->will($this->returnValue('n'));
 		$result = $this->Task->doHelpers();
 		$this->assertSame(array(), $result);
@@ -180,6 +129,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testDoHelpersTrailingSpace() {
+		$this->markTestIncomplete();
 		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
 		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' Text, Number, CustomOne  '));
 		$result = $this->Task->doHelpers();
@@ -193,6 +143,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testDoHelpersTrailingCommas() {
+		$this->markTestIncomplete();
 		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
 		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' Text, Number, CustomOne, , '));
 		$result = $this->Task->doHelpers();
@@ -206,6 +157,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testDoComponentsNo() {
+		$this->markTestIncomplete();
 		$this->Task->expects($this->any())->method('in')->will($this->returnValue('n'));
 		$result = $this->Task->doComponents();
 		$this->assertSame(array('Paginator'), $result);
@@ -217,6 +169,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testDoComponentsTrailingSpaces() {
+		$this->markTestIncomplete();
 		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
 		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' RequestHandler, Security  '));
 
@@ -231,6 +184,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testDoComponentsTrailingCommas() {
+		$this->markTestIncomplete();
 		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
 		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' RequestHandler, Security, , '));
 
@@ -245,6 +199,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testConfirmController() {
+		$this->markTestIncomplete();
 		$controller = 'Posts';
 		$scaffold = false;
 		$helpers = array('Js', 'Time');
@@ -262,6 +217,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testBake() {
+		$this->markTestIncomplete();
 		$helpers = array('Js', 'Time');
 		$components = array('Acl', 'Auth');
 		$this->Task->expects($this->any())->method('createFile')->will($this->returnValue(true));
@@ -285,6 +241,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeWithPlugin() {
+		$this->markTestIncomplete();
 		$this->Task->plugin = 'ControllerTest';
 
 		//fake plugin path
@@ -319,6 +276,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeActionsUsingSessions() {
+		$this->markTestIncomplete();
 		$result = $this->Task->bakeActions('BakeArticles', null, true);
 		$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsUsingSessions.ctp');
 		$this->assertTextEquals($expected, $result);
@@ -339,14 +297,29 @@ class ControllerTaskTest extends TestCase {
 	public function testBakeTest() {
 		$this->Task->plugin = 'ControllerTest';
 		$this->Task->connection = 'test';
-		$this->Task->interactive = false;
 
-		$this->Task->Test->expects($this->once())->method('bake')->with('Controller', 'BakeArticles');
+		$this->Task->Test->expects($this->once())
+			->method('bake')
+			->with('Controller', 'BakeArticles');
 		$this->Task->bakeTest('BakeArticles');
 
 		$this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
 		$this->assertEquals($this->Task->connection, $this->Task->Test->connection);
-		$this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
+	}
+
+/**
+ * test baking a test
+ *
+ * @return void
+ */
+	public function testBakeTestDisabled() {
+		$this->Task->plugin = 'ControllerTest';
+		$this->Task->connection = 'test';
+		$this->Task->params['no-test'] = true;
+
+		$this->Task->Test->expects($this->never())
+			->method('bake');
+		$this->Task->bakeTest('BakeArticles');
 	}
 
 /**
@@ -355,6 +328,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testInteractive() {
+		$this->markTestIncomplete();
 		$count = count($this->Task->listAll('test'));
 		if ($count != count($this->fixtures)) {
 			$this->markTestSkipped('Additional tables detected.');
@@ -390,6 +364,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testInteractiveAdminMethodsNotInteractive() {
+		$this->markTestIncomplete();
 		$count = count($this->Task->listAll('test'));
 		if ($count != count($this->fixtures)) {
 			$this->markTestSkipped('Additional tables detected.');
@@ -432,6 +407,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteIntoAll() {
+		$this->markTestIncomplete();
 		$count = count($this->Task->listAll('test'));
 		if ($count != count($this->fixtures)) {
 			$this->markTestSkipped('Additional tables detected.');
@@ -458,6 +434,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteIntoAllAdmin() {
+		$this->markTestIncomplete();
 		$count = count($this->Task->listAll('test'));
 		if ($count != count($this->fixtures)) {
 			$this->markTestSkipped('Additional tables detected.');
@@ -491,6 +468,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithController() {
+		$this->markTestIncomplete();
 		$this->Task->connection = 'test';
 		$this->Task->path = '/my/path/';
 		$this->Task->args = array('BakeArticles');
@@ -522,6 +500,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithControllerNameVariations($name) {
+		$this->markTestIncomplete();
 		$this->Task->connection = 'test';
 		$this->Task->path = '/my/path/';
 		$this->Task->args = array($name);
@@ -539,6 +518,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithPublicParam() {
+		$this->markTestIncomplete();
 		$this->Task->connection = 'test';
 		$this->Task->path = '/my/path/';
 		$this->Task->args = array('BakeArticles');
@@ -558,6 +538,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithControllerAndBoth() {
+		$this->markTestIncomplete();
 		$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
 		$this->Task->connection = 'test';
 		$this->Task->path = '/my/path/';
@@ -577,6 +558,7 @@ class ControllerTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithControllerAndAdmin() {
+		$this->markTestIncomplete();
 		$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
 		$this->Task->connection = 'test';
 		$this->Task->path = '/my/path/';
@@ -589,4 +571,5 @@ class ControllerTaskTest extends TestCase {
 		);
 		$this->Task->execute();
 	}
+
 }