Browse Source

Added multiple tests for regular expressions for the Unload Task

Bob 11 years ago
parent
commit
dde22d7ab4
2 changed files with 97 additions and 3 deletions
  1. 2 2
      src/Shell/Task/UnloadTask.php
  2. 95 1
      tests/TestCase/Shell/Task/UnloadTaskTest.php

+ 2 - 2
src/Shell/Task/UnloadTask.php

@@ -64,8 +64,8 @@ class UnloadTask extends Shell
      */
     protected function _modifyBootstrap($plugin)
     {
-        $finder = "/\nPlugin::load\('$plugin'(.|.\n|)+\);\n/";
-
+        $finder = "/\nPlugin::load\((.|.\n|\n\s\s|\n\t|)+'$plugin'(.|.\n|)+\);\n/";
+        
         $bootstrap = new File($this->bootstrap, false);
         $contents = $bootstrap->read();
 

+ 95 - 1
tests/TestCase/Shell/Task/UnloadTaskTest.php

@@ -86,6 +86,86 @@ class UnloadTaskTest extends TestCase
     }
 
     /**
+     * testRegularExpressions
+     *
+     * This method will tests multiple notations of plugin loading.
+     */
+    public function testRegularExpressions()
+    {
+        $bootstrap = new File($this->bootstrap, false);
+
+        //  Plugin::load('TestPlugin', [
+        //      'boostrap' => false
+        //  ]);
+        $bootstrap->append("\nPlugin::load('TestPlugin', [\n\t'boostrap' => false\n]);\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load('TestPlugin', [\n\t'boostrap' => false\n]);", $bootstrap->read());
+        $this->_clearBootstrap();
+
+        //  Plugin::load(
+        //      'TestPlugin',
+        //      [ 'boostrap' => false]
+        //  );
+        $bootstrap->append("\nPlugin::load(\n\t'TestPlugin',\n\t[ 'boostrap' => false]\n);\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load(\n\t'TestPlugin',\n\t[ 'boostrap' => false]\n);", $bootstrap->read());
+        $this->_clearBootstrap();
+
+        //  Plugin::load(
+        //      'Foo',
+        //      [
+        //          'boostrap' => false
+        //      ]
+        //  );
+        $bootstrap->append("\nPlugin::load(\n\t'TestPlugin',\n\t[\n\t\t'boostrap' => false\n\t]\n);\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load(\n\t'TestPlugin',\n\t[\n\t\t'boostrap' => false\n\t]\n);", $bootstrap->read());
+        $this->_clearBootstrap();
+
+        //  Plugin::load('Test', [
+        //      'autoload' => false,
+        //      'bootstrap' => true,
+        //      'routes' => true
+        //  ]);
+        $bootstrap->append("\nPlugin::load('TestPlugin', [\n\t'autoload' => false,\n\t'bootstrap' => true,\n\t'routes' => true\n]);\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load('TestPlugin', [\n\t'autoload' => false,\n\t'bootstrap' => true,\n\t'routes' => true\n]);", $bootstrap->read());
+        $this->_clearBootstrap();
+
+        //  Plugin::load('Test',
+        //      [
+        //          'bootstrap' => true,
+        //          'routes' => true
+        //      ]
+        //  );
+        $bootstrap->append("\nPlugin::load('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load('TestPlugin',\n\t[\n\t\t'bootstrap' => true,\n\t\t'routes' => true\n\t]\n);", $bootstrap->read());
+        $this->_clearBootstrap();
+
+        //  Plugin::load('Test',
+        //      [
+        //
+        //      ]
+        //  );
+        $bootstrap->append("\nPlugin::load('TestPlugin',\n\t[\n\t\n\t]\n);\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load('TestPlugin',\n\t[\n\t\n\t]\n);", $bootstrap->read());
+        $this->_clearBootstrap();
+
+        //  Plugin::load('Test');
+        $bootstrap->append("\nPlugin::load('TestPlugin');\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load('TestPlugin');", $bootstrap->read());
+        $this->_clearBootstrap();
+
+        //  Plugin::load('Test', ['bootstrap' => true, 'route' => false]);
+        $bootstrap->append("\nPlugin::load('TestPlugin', ['bootstrap' => true, 'route' => false]);\n");
+        $this->Task->main('TestPlugin');
+        $this->assertNotContains("Plugin::load('TestPlugin', ['bootstrap' => true, 'route' => false]);", $bootstrap->read());
+    }
+
+    /**
      * _addPluginToBootstrap
      *
      * Quick method to add a plugin to the bootstrap file.
@@ -96,6 +176,20 @@ class UnloadTaskTest extends TestCase
     protected function _addPluginToBootstrap($name)
     {
         $bootstrap = new File($this->bootstrap, false);
-        $bootstrap->append("\nPlugin::load('$name', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);");
+        $bootstrap->append("\n\nPlugin::load('$name', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);\n");
+    }
+
+    /**
+     * clearBootstrap
+     *
+     * Helper to clear the bootstrap file.
+     *
+     * @return void
+     */
+    protected function _clearBootstrap()
+    {
+        $bootstrap = new File($this->bootstrap, false);
+
+        $bootstrap->write($this->originalBootstrapContent);
     }
 }