There has been some discussion on Twitter about the quality vs. quantity of symfony plugins out there. This is a good opportunity to touch on one of the new features introduced in symfony 1.3 that allows you to hook tests from plugins into the symfony test:* commands with just a few lines of code.

The sfPluginConfiguration class, introduced in symfony 1.2, now includes a connectTests() method. This method, as you can tell by its name, connects tests from the plugin to the current project. You can call this method from your ProjectConfiguration class:

[php]
// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins('myPlugin');
  }

  public function setupPlugins()
  {
    $this->pluginConfigurations['myPlugin']->connectTests();
  }
}

With this code in place, tests from myPlugin will be included when any of symfony's test:* tasks are run:

$ php symfony test:all
$ php symfony test:unit
$ php symfony test:unit myPluginClass
$ php symfony test:functional frontend
$ php symfony test:functional frontend myPluginModuleActions

You can add your own logic to this connecting code. For example, if you use prefixed plugins as a means to organize project code, you can run a quick loop to connect all of your project-plugins' tests:

[php]
public function setupPlugins()
{
  foreach ($this->plugins as $plugin)
  {
    // check for your project's prefix
    if (0 === strpos($plugin, 'my'))
    {
      $this->pluginConfigurations[$plugin]->connectTests();
    }
  }
}

Something Extra

If your project uses sfTaskExtraPlugin, a plugin maintained by the core team, you have access to the test:plugin command. This is the best way to run a particular plugin's entire test suite.

$ php symfony test:plugin myPlugin

NOTE While the core test:* commands require you to call sfPluginConfiguration::connectTests() from your project configuration in order to include tests from a plugin, the test:plugin command does not. It is still required that the plugin be enabled.

The task-extra plugin also includes a generate:test task, which makes it easy to get a new unit test started quickly:

$ php symfony generate:test myPluginClass --editor-cmd=mate

This command will create a stub unit test in a myPluginClassTest.php file nested in the plugin's test/unit/ directory in such a way to replicate the directory structure of lib/. Having the test directory organized as a mirror of the lib directory makes it possible to run the test:coverage command if you have Xdebug installed:

$ php symfony test:coverage test/unit/util lib/util --detailed

Step-by-step

Let's assume you're considering sfFormExtraPlugin for your project. One way to evaluate this plugin is by evaluating the quality of its test suite. This can be done in three easy steps:

  1. Install the plugin:

    $ php symfony plugin:install sfFormExtraPlugin
    
  2. Run the plugin's test suite:

    $ php symfony test:plugin sfFormExtraPlugin
    
  3. Check the plugin's test coverage:

    $ php symfony test:coverage plugins/sfFormExtraPlugin/test \
      plugins/sfFormExtraPlugin/lib
    

Go forth and test!

Symfony has always advocated for writing automated tests and provided the tools necessary to do so in your project, but support for testing plugins has been limited. Hopefully, with these enhancements introduced in symfony 1.3 and sfTaskExtraPlugin, more plugin developers will include test suites with their plugins, and more users will run them.