From aad265137910e4ef879a8bc09142c1cc24d16fab Mon Sep 17 00:00:00 2001 From: AD7six Date: Tue, 16 Dec 2014 23:42:36 +0000 Subject: [PATCH] Test fixes for the plugin task --- src/Shell/Task/PluginTask.php | 1 + .../Controller/AppController.php.ctp} | 2 +- tests/TestCase/Shell/Task/PluginTaskTest.php | 103 +++++++++--------- 3 files changed, 56 insertions(+), 50 deletions(-) rename src/Template/Bake/Plugin/{Controller/AppController.php => src/Controller/AppController.php.ctp} (74%) diff --git a/src/Shell/Task/PluginTask.php b/src/Shell/Task/PluginTask.php index bf4abc8ad..5e397959c 100644 --- a/src/Shell/Task/PluginTask.php +++ b/src/Shell/Task/PluginTask.php @@ -158,6 +158,7 @@ protected function _generateFiles($plugin, $path) { $templates = $templatesDir->findRecursive('.*\.ctp'); } while (!$templates); + sort($templates); foreach($templates as $template) { $template = substr($template, strrpos($template, 'Plugin') + 7, -4); $this->_generateFile($template, $root); diff --git a/src/Template/Bake/Plugin/Controller/AppController.php b/src/Template/Bake/Plugin/src/Controller/AppController.php.ctp similarity index 74% rename from src/Template/Bake/Plugin/Controller/AppController.php rename to src/Template/Bake/Plugin/src/Controller/AppController.php.ctp index 804f040a4..35edece3c 100644 --- a/src/Template/Bake/Plugin/Controller/AppController.php +++ b/src/Template/Bake/Plugin/src/Controller/AppController.php.ctp @@ -1,6 +1,6 @@ \Controller; +namespace <%= $plugin %>\Controller; use App\Controller\AppController as BaseController; diff --git a/tests/TestCase/Shell/Task/PluginTaskTest.php b/tests/TestCase/Shell/Task/PluginTaskTest.php index 107a1b731..08998ac70 100644 --- a/tests/TestCase/Shell/Task/PluginTaskTest.php +++ b/tests/TestCase/Shell/Task/PluginTaskTest.php @@ -14,6 +14,7 @@ */ namespace Bake\Test\TestCase\Shell\Task; +use Bake\Shell\Task\ProjectTask; use Bake\Shell\Task\TemplateTask; use Cake\Core\App; use Cake\Core\Configure; @@ -39,6 +40,9 @@ public function setUp() { array('in', 'err', 'createFile', '_stop', 'clear', 'callProcess'), array($this->io) ); + + $this->Task->Project = new ProjectTask($this->io); + $this->Task->Template = new TemplateTask($this->io); $this->Task->Template->interactive = false; @@ -75,36 +79,24 @@ public function testBakeFoldersAndFiles() { $path = $this->Task->path . 'BakeTestBake'; - $file = $path . DS . 'src' . DS . 'Controller' . DS . 'AppController.php'; - $this->Task->expects($this->at(1))->method('createFile') - ->with($file, $this->stringContains('namespace BakeTestBake\Controller;')); - - $this->Task->bake('BakeTestBake'); + $files = [ + 'README.md', + 'composer.json', + 'config/routes.php', + 'phpunit.xml.dist', + 'src/Controller/AppController.php', + 'tests/bootstrap.php', + 'webroot/empty' + ]; - $path = $this->Task->path . 'BakeTestBake'; - $this->assertTrue(is_dir($path), 'No plugin dir'); - - $directories = array( - 'config', - 'src/Model/Behavior', - 'src/Model/Table', - 'src/Model/Entity', - 'src/Shell/Task', - 'src/Controller/Component', - 'src/View/Helper', - 'tests/TestCase/Controller/Component', - 'tests/TestCase/View/Helper', - 'tests/TestCase/Model/Behavior', - 'tests/Fixture', - 'src/Template', - 'webroot' - ); - foreach ($directories as $dir) { - $this->assertTrue(is_dir($path . DS . $dir), 'Missing directory for ' . $dir); + $i = 1; + foreach($files as $file) { + $this->Task->expects($this->at($i++)) + ->method('createFile') + ->with($path . DS . $file); } - $Folder = new Folder($this->Task->path . 'BakeTestBake'); - $Folder->delete(); + $this->Task->bake('BakeTestBake'); } /** @@ -138,26 +130,24 @@ public function testExecuteWithOneArg() { ->will($this->returnValue('y')); $path = $this->Task->path . 'BakeTestBake'; - $file = $path . DS . 'src' . DS . 'Controller' . DS . 'AppController.php'; - $this->Task->expects($this->at(1))->method('createFile') - ->with($file, $this->stringContains('class AppController extends BaseController {')); - - $file = $path . DS . 'config' . DS . 'routes.php'; - $this->Task->expects($this->at(2))->method('createFile') - ->with($file, $this->stringContains("Router::plugin('BakeTestBake', function (\$routes)")); - - $file = $path . DS . 'phpunit.xml'; - $this->Task->expects($this->at(3))->method('createFile') - ->with($file, $this->anything()); + $files = [ + 'README.md', + 'composer.json', + 'config/routes.php', + 'phpunit.xml.dist', + 'src/Controller/AppController.php', + 'tests/bootstrap.php', + 'webroot/empty' + ]; - $file = $path . DS . 'tests' . DS . 'bootstrap.php'; - $this->Task->expects($this->at(4))->method('createFile') - ->with($file, $this->anything()); + $i = 1; + foreach($files as $file) { + $this->Task->expects($this->at($i++)) + ->method('createFile') + ->with($path . DS . $file); + } $this->Task->main('BakeTestBake'); - - $Folder = new Folder($this->Task->path . 'BakeTestBake'); - $Folder->delete(); } /** @@ -175,29 +165,44 @@ public function testExecuteUpdateComposer() { ->method('findComposer') ->will($this->returnValue('composer.phar')); - $path = dirname($this->Task->path); + $path = $this->Task->path . 'BakeTestBake'; + if (!is_dir($path)) { + mkdir($path); + } $file = $path . DS . 'composer.json'; file_put_contents($file, '{}'); $config = [ + 'name' => 'your-name-here/BakeTestBake', + 'description' => 'BakeTestBake plugin for CakePHP', + 'type' => 'cakephp-plugin', + 'require' => [ + 'php' => '>=5.4', + 'cakephp/plugin-installer' => '*', + 'cakephp/cakephp' => '3.0.x-dev' + ], + 'require-dev' => [ + 'phpunit/phpunit' => '*' + ], 'autoload' => [ 'psr-4' => [ - 'BakeTestBake\\' => './plugins/BakeTestBake/src', + 'BakeTestBake\\' => 'src', ], ], 'autoload-dev' => [ 'psr-4' => [ - 'BakeTestBake\\Test\\' => './plugins/BakeTestBake/tests', + 'BakeTestBake\\Test\\' => 'tests', + 'Cake\\Test\\' => './vendor/cakephp/cakephp/tests', ], ], ]; - $config = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + $config = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"; $this->Task->expects($this->at(2)) ->method('createFile') ->with($file, $config); - $this->Task->expects($this->at(3)) + $this->Task->expects($this->once()) ->method('callProcess') ->with('php ' . escapeshellarg('composer.phar') . ' dump-autoload');