Author: Jonathan.Wage
Date: 2010-03-27 00:10:08 +0100 (Sat, 27 Mar 2010)
New Revision: 28821

Added:
   plugins/sfSympalPlugin/trunk/test/unit/sfSympalPluginEnabled.class.php
Modified:
   plugins/sfSympalPlugin/trunk/config/sfSympalPluginConfiguration.class.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/lib/theme/sfSympalThemeConfiguration.class.php
   plugins/sfSympalPlugin/trunk/lib/sfSympalPluginEnabler.class.php
   plugins/sfSympalPlugin/trunk/test/fixtures/project/config/app.yml
Log:
[sfSympalPlugin] Synchronizing git repository to Symfony Plugins SVN

Modified: 
plugins/sfSympalPlugin/trunk/config/sfSympalPluginConfiguration.class.php
===================================================================
--- plugins/sfSympalPlugin/trunk/config/sfSympalPluginConfiguration.class.php   
2010-03-26 21:30:14 UTC (rev 28820)
+++ plugins/sfSympalPlugin/trunk/config/sfSympalPluginConfiguration.class.php   
2010-03-26 23:10:08 UTC (rev 28821)
@@ -1,5 +1,16 @@
 <?php
 
+/**
+ * Main Plugin configuration class for sympal.
+ * 
+ * This is responsible for loading in plugins that are core to sympal
+ * 
+ * @package     sfSympalPlugin
+ * @subpackage  config
+ * @author      Jonathan H. Wage <[email protected]>
+ * @since       2010-03-26
+ * @version     svn:$Id$ $Author$
+ */
 class sfSympalPluginConfiguration extends sfPluginConfiguration
 {
   /**
@@ -9,6 +20,9 @@
 
   /**
    * Array of all the core Sympal plugins
+   * 
+   * A core plugin is one that lives in the lib/plugins directory of 
sfSympalPlugin.
+   * A core plugin will be enabled automatically
    */
   public static
     $corePlugins = array(
@@ -51,7 +65,7 @@
   }
 
   /**
-   * Shortcut method to enable all Sympal plugins for the given 
ProjectConfniguration
+   * Shortcut method to enable all Sympal plugins for the given 
ProjectConfiguration
    *
    * Returns an instance of sfSympalPluginEnabler which allows you to enable
    * disable and override any plugins with a convenient API.

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/lib/theme/sfSympalThemeConfiguration.class.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/lib/theme/sfSympalThemeConfiguration.class.php
     2010-03-26 21:30:14 UTC (rev 28820)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/lib/theme/sfSympalThemeConfiguration.class.php
     2010-03-26 23:10:08 UTC (rev 28821)
@@ -1,9 +1,29 @@
 <?php
 
+/**
+ * This class represents a "theme".
+ * 
+ * This wraps the configuration of a theme so it can be easily accessed.
+ * 
+ * @package     sfSympalRenderingPlugin
+ * @subpackage  theme
+ * @author      Jonathan H. Wage <[email protected]>
+ * @author      Ryan Weaver <[email protected]>
+ * @since       2010-03-25
+ * @version     svn:$Id$ $Author$
+ */
 class sfSympalThemeConfiguration
 {
+  /**
+   * @var array
+   */
   protected $_configuration;
 
+  /**
+   * Class constructor
+   * 
+   * @param array $configuration The raw array of configuration from app.yml
+   */
   public function __construct($configuration)
   {
     $this->_configuration = $configuration;

Modified: plugins/sfSympalPlugin/trunk/lib/sfSympalPluginEnabler.class.php
===================================================================
--- plugins/sfSympalPlugin/trunk/lib/sfSympalPluginEnabler.class.php    
2010-03-26 21:30:14 UTC (rev 28820)
+++ plugins/sfSympalPlugin/trunk/lib/sfSympalPluginEnabler.class.php    
2010-03-26 23:10:08 UTC (rev 28821)
@@ -3,8 +3,9 @@
 /**
  * Class responsible for enabling, disabling and overriding Sympal plugins
  *
- * @package sfSympalPlugin
- * @author Jonathan H. Wage <[email protected]>
+ * @package   sfSympalPlugin
+ * @author    Jonathan H. Wage <[email protected]>
+ * @author    Ryan Weaver <[email protected]>
  */
 class sfSympalPluginEnabler
 {
@@ -13,7 +14,12 @@
     $_isSympalEnabled = null,
     $_sympalPluginPath = null;
 
-  public function __construct(ProjectConfiguration $configuration)
+  /**
+   * Class Constructor
+   * 
+   * @param sfProjectConfiguration $configuration The project configuration 
onto which to enable the plugin
+   */
+  public function __construct(sfProjectConfiguration $configuration)
   {
     $this->_configuration = $configuration;
     $this->_sympalPluginPath = realpath(dirname(__FILE__).'/..');
@@ -21,6 +27,7 @@
 
   /**
    * Check whether or not Sympal is enabled for the current application
+   * (or "project" if there is no application in this scope)
    *
    * @return boolean
    */
@@ -29,8 +36,9 @@
     if ($this->_isSympalEnabled === null)
     {
       $this->_isSympalEnabled = true;
-      if ($application = sfConfig::get('sf_app'))
+      if ($this->_configuration instanceof sfApplicationConfiguration)
       {
+        $application = $this->_configuration->getApplication();
         $reflection = new ReflectionClass($application.'Configuration');
         if ($reflection->getConstant('disableSympal'))
         {
@@ -38,23 +46,31 @@
         }
       }
     }
+
     return $this->_isSympalEnabled;
   }
 
   /**
    * Enable all Sympal plugins
+   * 
+   * This will enable:
+   *   1) Any plugin inside the lib/plugins directory of sfSympalPlugin
+   *      that is in the sfSympalPluginConfiguration::corePlugins array
+   *   2) Any plugin living inside the project's plugins directory
    *
    * @return boolean Returns false if Sympal is not enabled
    */
   public function enableSympalPlugins()
   {
-    $this->_configuration->enablePlugins('sfDoctrinePlugin');
-
     if (!$this->isSympalEnabled())
     {
       return false;
     }
+    
+    // enable sfDoctrinePlugin
+    $this->_configuration->enablePlugins('sfDoctrinePlugin');
 
+    // enable sfSympalPlugin
     $this->_configuration->enablePlugins('sfSympalPlugin');
     $this->_configuration->setPluginPath('sfSympalPlugin', 
$this->_sympalPluginPath);
 
@@ -70,20 +86,34 @@
     $plugins = array_merge($plugins, $foundPlugins);
     $plugins = array_unique($plugins);
     $this->_configuration->setPlugins($plugins);
+    
+    return true;
   }
 
   /**
    * Enable an array of Sympal core plugins
+   * 
+   * A "core" plugin is any plugin that physically lives inside of the
+   * lib/plugins directory of sfSympalPlugin
    *
-   * @param array $plugins
+   * @param array $plugins The core plugins to enable
    * @return void
    */
   public function enableSympalCorePlugins($plugins)
   {
     foreach ((array) $plugins as $plugin)
     {
+      $path = $this->_sympalPluginPath.'/lib/plugins/'.$plugin;
+      if (!file_exists($path))
+      {
+        throw new sfException(sprintf(
+          'Cannot enable core plugin "%s" - it does not exist in %s',
+          $plugin,
+          dirname($path)
+        ));
+      }
       $this->_configuration->enablePlugins($plugin);
-      $this->_configuration->setPluginPath($plugin, 
$this->_sympalPluginPath.'/lib/plugins/'.$plugin);
+      $this->_configuration->setPluginPath($plugin, $path);
     }
   }
 
@@ -105,18 +135,20 @@
   /**
    * Override a Sympal plugin with a new plugin
    *
-   * @param string $plugin 
-   * @param string $newPlugin 
-   * @param string $newPluginPath 
+   * @param string $plugin          The plugin to override
+   * @param string $newPluginPath   The path of the new plugin (defaults to 
the plugins directory)
    * @return void
    */
-  public function overrideSympalPlugin($plugin, $newPlugin, $newPluginPath = 
null)
+  public function overrideSympalPlugin($plugin, $newPluginPath = null)
   {
     $this->_configuration->disablePlugins($plugin);
-    $this->_configuration->enablePlugins($newPlugin);
-    if ($newPluginPath)
+    $this->_configuration->enablePlugins($plugin);
+    
+    if ($newPluginPath === null)
     {
-      $this->_configuration->setPluginPath($newPlugin, $newPluginPath);
+      $newPluginPath = sfConfig::get('sf_plugins_dir').'/'.$plugin;
     }
+
+    $this->_configuration->setPluginPath($plugin, $newPluginPath);
   }
 }
\ No newline at end of file

Modified: plugins/sfSympalPlugin/trunk/test/fixtures/project/config/app.yml
===================================================================
--- plugins/sfSympalPlugin/trunk/test/fixtures/project/config/app.yml   
2010-03-26 21:30:14 UTC (rev 28820)
+++ plugins/sfSympalPlugin/trunk/test/fixtures/project/config/app.yml   
2010-03-26 23:10:08 UTC (rev 28821)
@@ -7,4 +7,6 @@
     sfSympalThemeTestPlugin:
       installed: true
     installed: true
-    current_version: 1.0.0-ALPHA3
+    current_version: 1.0.0-ALPHA1
+    upgrade_version_history:
+      - 0.7.0__2

Added: plugins/sfSympalPlugin/trunk/test/unit/sfSympalPluginEnabled.class.php
===================================================================
--- plugins/sfSympalPlugin/trunk/test/unit/sfSympalPluginEnabled.class.php      
                        (rev 0)
+++ plugins/sfSympalPlugin/trunk/test/unit/sfSympalPluginEnabled.class.php      
2010-03-26 23:10:08 UTC (rev 28821)
@@ -0,0 +1,119 @@
+<?php
+
+/**
+ * Unit test for sympal's plugin enabler
+ * 
+ * @package     sfSympalPlugin
+ * @subpackage  test
+ * @author      Ryan Weaver <[email protected]>
+ * @since       2010-03-26
+ * @version     svn:$Id$ $Author$
+ */
+
+require_once(dirname(__FILE__).'/../bootstrap/unit.php');
+
+$t = new lime_test(16);
+
+// test stub class
+class ProjectConfigurationStub extends sfProjectConfiguration
+{
+  public function loadPlugins()
+  {
+    parent::loadPlugins();
+    
+    $this->pluginsLoaded = false;
+  }
+}
+
+// test stub classes for application configuration
+class ApplicationStubConfiugration extends sfApplicationConfiguration
+{
+  // prevents the loading of plugins via ProjectConfiguration (to start fresh)
+  public function setup()
+  {
+  }
+  public function loadPlugins()
+  {
+    parent::loadPlugins();
+    $this->pluginsLoaded = false;
+  }
+}
+class enabledApplicationStubConfiguration extends ApplicationStubConfiugration
+{
+}
+class disabledApplicationStubConfiguration extends ApplicationStubConfiugration
+{
+  const disableSympal = true;
+}
+
+// setup some configurations
+$projConfiguration = new ProjectConfigurationStub();
+$appEnabledConfiguration = new enabledApplicationStubConfiguration('test', 
true);
+$appDisabledConfiguration = new disabledApplicationStubConfiguration('test', 
true);
+
+// setup some enablers
+$enabler = new sfSympalPluginEnabler($projConfiguration);
+$enablerApp1 = new sfSympalPluginEnabler($appEnabledConfiguration);
+$enablerApp2 = new sfSympalPluginEnabler($appDisabledConfiguration);
+
+$t->info('1 - Test the isSympalEnabled() functionality');
+$t->is($enabler->isSympalEnabled(), true, '->isSympalEnabled() returns true 
for all ProjectConfiguration instances');
+$t->is($enablerApp1->isSympalEnabled(), true, '->isSympalEnabled() returns 
true for an ApplicationConfiguration instance without the disableSympal 
constant');
+$t->is($enablerApp2->isSympalEnabled(), false, '->isSympalEnabled() returns 
false for an ApplicationConfiguration instance WITH the disableSympal constant 
equal to true');
+
+
+$t->info('2 - Test the enabling/disabling of plugins');
+$t->info('  2.1 - Sanity checks');
+$t->is(count($projConfiguration->getPlugins()), 0, 'Initially there are 0 
enabled plugins');
+$t->is(count($appEnabledConfiguration->getPlugins()), 0, 'Initially there are 
0 enabled plugins');
+$t->is(count($appDisabledConfiguration->getPlugins()), 0, 'Initially there are 
0 enabled plugins');
+
+$t->info('  2.2 - Using enableSympalCorePlugins() for a plugin that does not 
exist inside sympal throws an exception');
+try
+{
+  $enabler->enableSympalCorePlugins('fake');
+  $t->fail('Exception not thrown');
+}
+catch (sfException $e)
+{
+  $t->pass('Exception thrown');
+}
+
+$t->info('  2.3 - Enable a core plugin');
+$enabler->enableSympalCorePlugins('sfDoctrineGuardPlugin');
+$t->is(count($projConfiguration->getPlugins()), 1, 
'->enableSympalCorePlugins() enables the core plugin');
+$paths = $projConfiguration->getPluginPaths();
+$t->is($paths[0], 
realpath(dirname(__FILE__).'/../../lib/plugins/sfDoctrineGuardPlugin'), 'The 
plugin path of the core plugin is inside the lib/plugins dir of sympal');
+
+$t->info('  2.4 - Disable a core plugin');
+$enabler->disableSympalCorePlugins(array('sfDoctrineGuardPlugin'));
+$t->is(count($projConfiguration->getPlugins()), 0, 
'->disableSympalCorePlugins() disables the core plugin');
+
+$corePluginsCount = count(sfSympalPluginConfiguration::$corePlugins);
+$installedPluginsCount = 3; // in the test project's plugin directory
+$otherPluginsCount = 2; // sfSympalPlugin + sfDoctrinePlugin
+$totalPluginsCount = $corePluginsCount + $installedPluginsCount + 
$otherPluginsCount;
+
+$enabler->enableSympalPlugins();
+$enablerApp1->enableSympalPlugins();
+$enablerApp2->enableSympalPlugins();
+
+$t->is(count($projConfiguration->getPlugins()), $totalPluginsCount, 
'->enableSympalPlugins() enables core plugins + anything in the plugins 
directory');
+$t->is(count($appEnabledConfiguration->getPlugins()), $totalPluginsCount, 
'->enableSympalPlugins() enables core plugins + anything in the plugins 
directory');
+$t->is(count($appDisabledConfiguration->getPlugins()), 0, 
'->enableSympalPlugins() enables no plugins if sympal is disabled for the 
application');
+
+$t->info('  2.5 - Test overrideSympalPlugin()');
+$projConfiguration->disablePlugins($projConfiguration->getPlugins());
+$enabler->enableSympalCorePlugins(array('sfDoctrineGuardPlugin'));
+$t->is(count($projConfiguration->getPlugins()), 1, 'Sanity check');
+$enabler->overrideSympalPlugin('sfDoctrineGuardPlugin');
+$t->is(count($projConfiguration->getPlugins()), 1, 'After 
overrideSympalPlugin(), the sfDoctrineGuardPlugin is still enabled');
+$paths = $projConfiguration->getPluginPaths();
+
+/*
+ * The plugin path is false because it doesn't exist in the plugins directory.
+ * I can't think of a good way to test this short of creating an 
sfDoctrineGuardPlugin
+ * somewhere and pointing there. sfProjectConfiguration calls realpath, so
+ * my attempts to set it to a non-existent dir result in a path of "false"
+ */
+$t->is($paths[0], false, 'The plugin path to the plugin has changed, however');
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to