Skip to content

Commit

Permalink
Update PluginTask to generate phpunit boilerplate.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Mar 10, 2014
1 parent 8d0cf77 commit 508b3d2
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 34 deletions.
72 changes: 55 additions & 17 deletions 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)
*
Expand Down Expand Up @@ -43,6 +41,13 @@ class PluginTask extends Shell {
*/
public $bootstrap = null;

/**
* Tasks this task uses.
*
* @var array
*/
public $tasks = ['Template'];

/**
* initialize
*
Expand Down Expand Up @@ -112,16 +117,17 @@ public function bake($plugin) {
$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'
];

Expand All @@ -146,20 +152,15 @@ public function bake($plugin) {
$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);
Expand All @@ -184,6 +185,43 @@ protected function _modifyBootstrap($plugin) {
}
}

/**
* 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
*
Expand Down Expand Up @@ -221,7 +259,7 @@ public function findPath($pathOptions) {
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.')
Expand Down
7 changes: 7 additions & 0 deletions 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 changes: 32 additions & 0 deletions 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>
49 changes: 32 additions & 17 deletions tests/TestCase/Console/Command/Task/PluginTaskTest.php
Expand Up @@ -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;
Expand All @@ -45,8 +46,14 @@ public function setUp() {
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');
Expand Down Expand Up @@ -74,14 +81,10 @@ public function testBakeFoldersAndFiles() {

$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';
Expand All @@ -90,16 +93,17 @@ public function testBakeFoldersAndFiles() {
$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) {
Expand All @@ -116,19 +120,28 @@ public function testBakeFoldersAndFiles() {
* @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();

Expand All @@ -150,13 +163,15 @@ public function testExecuteWithOneArg() {
$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');
Expand Down

0 comments on commit 508b3d2

Please sign in to comment.