Browse Source

Update PluginTask to generate phpunit boilerplate.

There were a few mistakes in the PluginTask. Also make it easier to get
plugins up and running with PHPUnit by generating some boilerplate code.
Mark Story 12 years ago
parent
commit
508b3d21a4

+ 55 - 17
src/Console/Command/Task/PluginTask.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * The Plugin Task handles creating an empty plugin, ready to be used
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -44,6 +42,13 @@ class PluginTask extends Shell {
 	public $bootstrap = null;
 
 /**
+ * Tasks this task uses.
+ *
+ * @var array
+ */
+	public $tasks = ['Template'];
+
+/**
  * initialize
  *
  * @return void
@@ -112,16 +117,17 @@ class PluginTask extends Shell {
 			$directories = [
 				'Config/Schema',
 				'Model/Behavior',
-				'Model/Datasource',
+				'Model/Table',
+				'Model/Entity',
 				'Console/Command/Task',
 				'Controller/Component',
 				'Lib',
 				'View/Helper',
-				'Test/Case/Controller/Component',
-				'Test/Case/View/Helper',
-				'Test/Case/Model/Behavior',
+				'Template',
+				'Test/TestCase/Controller/Component',
+				'Test/TestCase/View/Helper',
+				'Test/TestCase/Model/Behavior',
 				'Test/Fixture',
-				'vendor',
 				'webroot'
 			];
 
@@ -146,20 +152,15 @@ class PluginTask extends Shell {
 			$controllerFileName = $plugin . 'AppController.php';
 
 			$out = "<?php\n\n";
-			$out .= "App::uses('AppController', 'Controller');\n\n";
+			$out .= "namespace {$plugin}\\Controller;\n\n";
+			$out .= "use App\\Controller\\AppController;\n\n";
 			$out .= "class {$plugin}AppController extends AppController {\n\n";
 			$out .= "}\n";
 			$this->createFile($this->path . $plugin . DS . 'Controller/' . $controllerFileName, $out);
 
-			$modelFileName = $plugin . 'AppModel.php';
-
-			$out = "<?php\n\n";
-			$out .= "App::uses('AppModel', 'Model');\n\n";
-			$out .= "class {$plugin}AppModel extends AppModel {\n\n";
-			$out .= "}\n";
-			$this->createFile($this->path . $plugin . DS . 'Model/' . $modelFileName, $out);
-
 			$this->_modifyBootstrap($plugin);
+			$this->_generatePhpunitXml($plugin, $this->path);
+			$this->_generateTestBootstrap($plugin, $this->path);
 
 			$this->hr();
 			$this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
@@ -185,6 +186,43 @@ class PluginTask extends Shell {
 	}
 
 /**
+ * Generate a phpunit.xml stub for the plugin.
+ *
+ * @param string $plugin Name of plugin
+ * @param string $path The path to save the phpunit.xml file to.
+ * @return void
+ */
+	protected function _generatePhpunitXml($plugin, $path) {
+		$this->Template->set([
+			'plugin' => $plugin,
+			'path' => $path
+		]);
+		$this->out( __d('cake_console', 'Generating phpunit.xml file...'));
+		$out = $this->Template->generate('test', 'phpunit.xml');
+		$file = $path . $plugin . DS . 'phpunit.xml';
+		$this->createFile($file, $out);
+	}
+
+/**
+ * Generate a Test/bootstrap.php stub for the plugin.
+ *
+ * @param string $plugin Name of plugin
+ * @param string $path The path to save the phpunit.xml file to.
+ * @return void
+ */
+	protected function _generateTestBootstrap($plugin, $path) {
+		$this->Template->set([
+			'plugin' => $plugin,
+			'path' => $path,
+			'root' => ROOT
+		]);
+		$this->out( __d('cake_console', 'Generating Test/bootstrap.php file...'));
+		$out = $this->Template->generate('test', 'bootstrap');
+		$file = $path . $plugin . '/Test/bootstrap.php';
+		$this->createFile($file, $out);
+	}
+
+/**
  * find and change $this->path to the user selection
  *
  * @param array $pathOptions
@@ -221,7 +259,7 @@ class PluginTask extends Shell {
 	public function getOptionParser() {
 		$parser = parent::getOptionParser();
 		$parser->description(__d('cake_console',
-			'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
+			'Create the directory structure, AppController class and testing setup for a new plugin. ' .
 			'Can create plugins in any of your bootstrapped plugin paths.'
 		))->addArgument('name', [
 			'help' => __d('cake_console', 'CamelCased name of the plugin to create.')

+ 7 - 0
src/Console/Templates/default/test/bootstrap.ctp

@@ -0,0 +1,7 @@
+<?= "<?php\n"; ?>
+/**
+ * Test suite bootstrap for <?= $plugin ?>.
+ */
+// Customize this to be a relative path for embedded plugins.
+// For standalone plugins, this should point at a CakePHP installation.
+require '<?= $root ?>/App/Config/bootstrap.php';

+ 32 - 0
src/Console/Templates/default/test/phpunit.xml.ctp

@@ -0,0 +1,32 @@
+<?= '<?xml version="1.0" encoding="UTF-8"?>'; ?>
+<phpunit
+	colors="true"
+	processIsolation="false"
+	stopOnFailure="false"
+	syntaxCheck="false"
+	bootstrap="./Test/bootstrap.php"
+	>
+	<php>
+		<ini name="memory_limit" value="-1"/>
+		<ini name="apc.enable_cli" value="1"/>
+	</php>
+
+	<!-- Add any additional test suites you want to run here -->
+	<testsuites>
+		<testsuite name="<?= $plugin ?> Test Suite">
+			<directory>./Test/TestCase</directory>
+		</testsuite>
+	</testsuites>
+
+	<!-- Setup a listener for fixtures -->
+	<listeners>
+		<listener
+		class="\Cake\TestSuite\Fixture\FixtureInjector"
+		file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
+			<arguments>
+				<object class="\Cake\TestSuite\Fixture\FixtureManager" />
+			</arguments>
+		</listener>
+	</listeners>
+
+</phpunit>

+ 32 - 17
tests/TestCase/Console/Command/Task/PluginTaskTest.php

@@ -19,6 +19,7 @@
 namespace Cake\Test\TestCase\Console\Command\Task;
 
 use Cake\Console\Command\Task\DbConfigTask;
+use Cake\Console\Command\Task\TemplateTask;
 use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
@@ -45,8 +46,14 @@ class PluginTaskTest extends TestCase {
 			array('in', 'err', 'createFile', '_stop', 'clear'),
 			array($this->out, $this->out, $this->in)
 		);
+		$this->Task->Template = new TemplateTask($this->out, $this->out, $this->in);
+
 		$this->Task->path = TMP . 'tests/';
 		$this->Task->bootstrap = TMP . 'tests/bootstrap.php';
+
+		if (!is_dir($this->Task->path)) {
+			mkdir($this->Task->path);
+		}
 		touch($this->Task->bootstrap);
 
 		$this->_path = App::path('Plugin');
@@ -74,14 +81,10 @@ class PluginTaskTest extends TestCase {
 
 		$path = $this->Task->path . 'BakeTestPlugin';
 
-		$file = $path . DS . 'Controller/BakeTestPluginAppController.php';
+		$file = $path . '/Controller/BakeTestPluginAppController.php';
 		$this->Task->expects($this->at(1))->method('createFile')
 			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
 
-		$file = $path . DS . 'Model/BakeTestPluginAppModel.php';
-		$this->Task->expects($this->at(2))->method('createFile')
-			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
-
 		$this->Task->bake('BakeTestPlugin');
 
 		$path = $this->Task->path . 'BakeTestPlugin';
@@ -90,16 +93,17 @@ class PluginTaskTest extends TestCase {
 		$directories = array(
 			'Config/Schema',
 			'Model/Behavior',
-			'Model/Datasource',
+			'Model/Table',
+			'Model/Entity',
 			'Console/Command/Task',
 			'Controller/Component',
 			'Lib',
 			'View/Helper',
-			'Test/Case/Controller/Component',
-			'Test/Case/View/Helper',
-			'Test/Case/Model/Behavior',
+			'Test/TestCase/Controller/Component',
+			'Test/TestCase/View/Helper',
+			'Test/TestCase/Model/Behavior',
 			'Test/Fixture',
-			'vendor',
+			'Template',
 			'webroot'
 		);
 		foreach ($directories as $dir) {
@@ -116,19 +120,28 @@ class PluginTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithNoArgs() {
-		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestPlugin'));
-		$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
+		$this->Task->expects($this->at(0))
+			->method('in')
+			->will($this->returnValue('TestPlugin'));
+
+		$this->Task->expects($this->at(1))
+			->method('in')
+			->will($this->returnValue('y'));
 
 		$path = $this->Task->path . 'TestPlugin';
-		$file = $path . DS . 'Controller/TestPluginAppController.php';
+		$file = $path . '/Controller/TestPluginAppController.php';
 
 		$this->Task->expects($this->at(2))->method('createFile')
 			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
 
-		$file = $path . DS . 'Model/TestPluginAppModel.php';
+		$file = $path . '/phpunit.xml';
 		$this->Task->expects($this->at(3))->method('createFile')
 			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
 
+		$file = $path . '/Test/bootstrap.php';
+		$this->Task->expects($this->at(4))->method('createFile')
+			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
+
 		$this->Task->args = array();
 		$this->Task->execute();
 
@@ -150,13 +163,15 @@ class PluginTaskTest extends TestCase {
 		$this->Task->expects($this->at(1))->method('createFile')
 			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
 
-		$path = $this->Task->path . 'BakeTestPlugin';
-		$file = $path . DS . 'Model/BakeTestPluginAppModel.php';
+		$file = $path . '/phpunit.xml';
 		$this->Task->expects($this->at(2))->method('createFile')
 			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
 
-		$this->Task->args = array('BakeTestPlugin');
+		$file = $path . '/Test/bootstrap.php';
+		$this->Task->expects($this->at(3))->method('createFile')
+			->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
 
+		$this->Task->args = array('BakeTestPlugin');
 		$this->Task->execute();
 
 		$Folder = new Folder($this->Task->path . 'BakeTestPlugin');