Skip to content

Instantly share code, notes, and snippets.

@henrikbjorn
Created November 7, 2012 11:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrikbjorn/4031055 to your computer and use it in GitHub Desktop.
Save henrikbjorn/4031055 to your computer and use it in GitHub Desktop.
CodeCoverage for PHPSpec2
<?php
namespace Vandpibe\PHPSpec\Extension;
use PHPSpec2\ServiceContainer;
use PHP_CodeCoverage;
use PHP_CodeCoverage_Report_HTML;
class CodeCoverage implements \PHPSpec2\Extension\ExtensionInterface
{
public function initialize(ServiceContainer $container)
{
$c = $container;
$c->extend('event_dispatcher.listeners', function($c) {
return new CoverageListener($c->get('code_coverage'), $c->get('code_coverage_writer'));
});
$c->set('code_coverage', $c->share(function($c) {
return new PHP_CodeCoverage;
}));
$c->set('code_coverage_writer', $c->share(function ($c) {
return new PHP_CodeCoverage_Report_HTML;
}));
}
}
<?php
namespace Vandpibe\PHPSpec\Extension;
use PHP_CodeCoverage;
use PHP_CodeCoverage_Report_HTML;
use PHPSpec2\Event\SpecificationEvent;
use PHPSpec2\Event\SuiteEvent;
class CoverageListener implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
protected $coverage;
protected $report;
public function __construct(PHP_CodeCoverage $coverage, PHP_CodeCoverage_Report_HTML $report)
{
$this->coverage = $coverage;
$this->report = $report;
}
public function onBeforeSpecification(SpecificationEvent $event)
{
$this->coverage->start($event->getSpecification()->getTitle());
}
public function onAfterSpecification(SpecificationEvent $event)
{
$this->coverage->stop();
}
public function onAfterSuite(SuiteEvent $event)
{
$path = getcwd() . '/coverage';
// This needs to be automatic and added to the run command somehow.
$this->report->process($this->coverage, $path);
}
public static function getSubscribedEvents()
{
return array(
'beforeSpecification' => 'onBeforeSpecification',
'afterSpecification' => 'onAfterSpecification',
'afterSuite' => 'onAfterSuite',
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment