Created
November 7, 2012 11:52
-
-
Save henrikbjorn/4031055 to your computer and use it in GitHub Desktop.
CodeCoverage for PHPSpec2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
})); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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