Category Archives: tests

Collecting CodeCoverage from symfony lime Tests

symfony-plugins-coverage

Since i use PHPUnit to test my symfony Applications and doing all automated stuff with Jenkins, i lost control over my lime test (e.g. in my plugins).
My Plugins stay in some kind of blackbox (lime isnt very useful with CI tools like Jenkins)

I love the Coverage Reports from PHPUnit (which is using PHP_CodeCoverage for that), which outputs me HTML or Clover, just as i need them.

To achieve the same with all my lime tests, i wrote a simple symfony plugin which, replaces the defaults “test:” tasks of symfony with my own modifed tasks.
Internally i extended the LimeHarness to use PHP_CodeCoverage for its reports:

Here is the Plugin https://github.com/digitalkaoz/rsPHPUnitLimePlugin

simply clone it or install it via pear or use svn.

Git: https://github.com/digitalkaoz/rsPHPUnitLimePlugin.git

Svn: https://svn.github.com/digitalkaoz/rsPHPUnitLimePlugin.git

PEAR: symfony plugins:install rsPHPUnitLimePlugin

After that you can use the tasks the same way (they have i slightly other name and some more options)

Usage

unit test coverage

  • symfony test:phpunit-unit –coverage-clover=log/clover-unit.xml
  • symfony test:phpunit-unit –coverage-html=log/coverage-unit
  • symfony test:phpunit-unit –coverage-folder=lib/
  • symfony test:phpunit-unit –xml=log/junit-unit.xml

functional test coverage

  • symfony test:phpunit-functional –coverage-clover=log/clover-functional.xml frontend
  • symfony test:phpunit-functional –coverage-html=log/coverage-functional frontend
  • symfony test:phpunit-functional –coverage-folder=apps/ frontend
  • symfony test:phpunit-functional –xml=log/junit-functional.xml frontend

plugin test coverage

  • symfony test:phpunit-plugin –coverage-clover=log/clover-functional.xml sfGuardPlugin
  • symfony test:phpunit-plugin –coverage-html=log/coverage-functional sfGuardPlugin
  • symfony test:phpunit-plugin –coverage-folder=plugin/sfGuardPlugin sfGuardPlugin
  • symfony test:phpunit-plugin –xml=log/junit-functional.xml sfGuardPlugin

all test coverage

  • symfony test:phpunit-all –coverage-clover=log/clover-functional.xml
  • symfony test:phpunit-all –coverage-html=log/coverage-functional sfGuardPlugin
  • symfony test:phpunit-all –coverage-folder=./
  • symfony test:phpunit-all –xml=log/junit-functional.xml

They work the same way as before, but they use PHP_CodeCoverage. You get nice HTML Reports and or Clover Reports.

To connect all your Plugin Tests with your default lime tests, simply modify your ProjectConfiguration:

 

  /**
   * reconfigure plugins
   */
  public function setupPlugins()
  {
    if(sfConfig::get('env') == 'prod')
    {
      return;
    }
    $plugins = sfFinder::type('directory')->name('*Plugin*')->maxdepth(0)->in(dirname(__FILE__).'/../plugins');
    $blackList = array('swFunctionalTestGenerationPlugin');
    foreach($plugins as $plugin)
    {
      $plugin = substr($plugin, strripos($plugin, DIRECTORY_SEPARATOR)+1);
      if(isset($this->pluginConfigurations[$plugin]) && !in_array($plugin,$blackList))
      {
        //connect the tests
        $this->pluginConfigurations[$plugin]->connectTests();
      }
    }
  }

To integrate this tests into your Jenkins Jobs simply use following build-step (as allways) :

export SYMFONY=$WORKSPACE/lib/vendor/symfony/lib
cd $WORKSPACE
./symfony test:phpunit-unit --xml=log/junit-results-plugin.xml --coverage-html=doc/coverage-plugin/ --coverage-clover=log/clover-report-plugin.xml --coverage-folder=plugins/

This will test all your unit tests, and writes the coverage to html and clover
in your post-build-actions simply publish your coverage data

Voila, now you can monitor your oldskool lime tests, or your plugins. It works with unit,functional and all test tasks

make PHPUnit functional Tests extendable

Currently im switching from lime tests to phpunit tests.
Therefore i installed the sfPHPUnit2Plugin.

Its working well, except the fact the the sfTestFunctional class is not extendable, for easier extending the browser (e.g. write a signin function to signin the user)

To solve this i’ve hacked a bit.

At first we extend the sfTestFunctional class:

class sfGuardTestFunctional extends sfTestFunctional {
  public function signin($username, $password) {
    $module = sfConfig::get('sf_login_module','sfGuardAuth');
    $action = sfConfig::get('sf_login_action','signin');
    return $this->
      get('/login')->
      with('request')->begin()->
        isForwardedTo($module, $action)->
      end()->
      setField('signin[username]', $username)->
      setField('signin[password]', $password)->
      click('sign in')->
      with('user')->isAuthenticated()->
      followRedirect();
  }
  public function signout() {
    $signout_url = '/logout';
    return $this->
      get($signout_url)->
      with('request')->begin()->
        isForwardedTo('sfGuardAuth', 'signout')->
      end()->
      with('response')->begin()->
        isStatusCode(302)->
      end()->
      followRedirect()
    ;
  }
}

This piece of code can be used im lime tests too, simply instanciate this class instead of the default sfTestFunctional:

include(dirname(__FILE__).'/../../bootstrap/functional.php');
$browser = new sfGuardTestFunctional(new sfBrowser());
$browser->get('/')->
  signin('user','password')
;

To get this new Subclass into the phpunit Tests we have to extend the default sfPHPUnitBaseFunctionalTestCase class:

class sfGuardPHPUnitFunctionalTest extends sfPHPUnitBaseFunctionalTestCase
{
  public function getBrowser(){
    if(get_class($this->testBrowser) != 'sfGuardTestFunctional')
    {
      $this->testBrowser = new sfGuardTestFunctional(new sfBrowser(), $this->getTest());
    }
    return $this->testBrowser;
  }
  public function _start()
  {
    new sfDatabaseManager(ProjectConfiguration::getApplicationConfiguration($this->getApplication(), 'test', true));
    $loader = new sfPropelData();
    $loader->loadData(sfConfig::get('sf_data_dir').'/fixtures');
  }
}

This is the PHPunit Testclass were extending from in our functional php unit tests:

require_once dirname(__FILE__).'/../../bootstrap/functional.php';
class functional_admin_dashboardActionsTest extends sfGuardPHPUnitFunctionalTest
{
  protected function getApplication()
  {
    return 'admin';
  }
  public function testDashBoardLinks()
  {
    $browser = $this->getBrowser();
    $browser->get('/')->
      signin('user','password')
    ;
  }
}

now both parts are extendable well.
You can add new functions to sfGuardTestFunctional to have additional browser functions, and you can move general code for every functional test to the new sfGuardPHPUnitFunctionalTest class.

now phpunit functional tests are well extendable, and integrated well.