test.ical.ly | getting the web by the balls

Jul/10

7

Wrapping symfonys functional tests in a PHPUnit test case

Yesterday I wrote a lot of functional tests for a symfony plugin that provides and XML web service interface.

Using symfonys functional testing classes this is actually quite easy to do.

But wouldn’t it be cool if you could integrate these tests into your continuous integration service just like PHPUnit tests? Wouldn’t it be cool to be able to generate PHPUnit coverage reports?

Well I thought so too that’s why I decided to write a very simple PHPUnit test case that executes the functional tests (using lime) and runs asserts on the output of those.

assertLimeReport('Index');
  }
  public function testShow()
  {
    $this->assertLimeReport('Show');
  }
  private function assertLimeReport($action)
  {
    ob_start();
    include(dirname(__FILE__).'/functional/yourModuleActions'.$action.'Test.php');
    $output = ob_get_clean();
    $lines = explode("\n", $output);
    foreach($lines as $line)
    {
      if(trim($line) && '#' != $line[0] && '>' != $line[0])
      {
        $this->assertEquals('ok', substr($line, 0, 2), $line);
      }
    }
  }
}

You can see that I used a simple convention how to name the functional tests. There is actually one file per action with the action name encoded in the file name. In the above example I run the tests for executeIndex() and executeShow().

The rest is simple as well (and can for sure be done far more elegant). I just iterate over the lime output and check for result lines that start with ok.

I also wrote a small phpunit.functional.xml configuration file so the coverage reports only considers my module code (all other code is unit tested of course).



  
    
      ./modules/yourModule/actions/
      ./modules/yourModule/lib/
    
  

Running this test case is easy.

$ phpunit --configuration=phpunit.functional.xml --coverage-html=/path/to/your/report test/yourPluginFunctionalTests.php

The coverage report comes in quite handy as I can now see which methods are not executed and are probably not needed any longer (I did some heavy refactoring on this).

The only thin I can not get rid of is that lime will only have the last word telling me “# Looks like everything went fine.“.

This is due to limes use of register_shutdown_function() which unfortunately does not have a counter part to unregister..

· · · · · · ·



<<

>>

Theme Design by devolux.nh2.me