Skip to content

Commit

Permalink
Merge pull request #9 from datashaman/feature/2-coverage
Browse files Browse the repository at this point in the history
Add coverage
  • Loading branch information
datashaman authored Apr 15, 2019
2 parents 9997b03 + c69af19 commit cbf5228
Show file tree
Hide file tree
Showing 27 changed files with 140 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build/
/composer.lock
/.phpcheck/
/php_errors.log
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
phpcheck:
@phpcheck

phpcheck-coverage-html:
@phpcheck --coverage-html build/coverage

phpcheck-coverage-text:
@phpcheck --coverage-text

phpcheck-no-defects:
@phpcheck -d

Expand Down
1 change: 0 additions & 1 deletion checks/GenCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Checks;

use Datashaman\PHPCheck\Check;
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpmd/phpmd": "^2.6"
"phpmd/phpmd": "^2.6",
"phpunit/php-code-coverage": "^7.0"
},
"config": {
"platform": {
Expand Down
1 change: 0 additions & 1 deletion src/ArgumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

use Exception;
Expand Down
1 change: 0 additions & 1 deletion src/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

class Check
Expand Down
9 changes: 5 additions & 4 deletions src/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

use Symfony\Component\Console\Command\Command;
Expand All @@ -27,11 +26,13 @@ class CheckCommand extends Command
protected function configure(): void
{
$this
->setDescription('Runs checks.')
->addOption('bootstrap', null, InputOption::VALUE_OPTIONAL, 'A PHP script that is included before the tests run')
->setDescription('Run checks.')
->addOption('bootstrap', null, InputOption::VALUE_OPTIONAL, 'A PHP script that is included before the checks run')
->addOption('coverage-html', null, InputOption::VALUE_OPTIONAL, 'Generate code coverage report in HTML', false)
->addOption('coverage-text', null, InputOption::VALUE_OPTIONAL, 'Generate code coverage report in text', false)
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Filter the checks that will be run')
->addOption('iterations', 'i', InputOption::VALUE_REQUIRED, 'How many times each check will be run', Runner::MAX_ITERATIONS)
->addOption('log-junit', 'j', InputOption::VALUE_OPTIONAL, 'Log test execution in JUnit XML format to file')
->addOption('log-junit', 'j', InputOption::VALUE_OPTIONAL, 'Log check execution in JUnit XML format to file')
->addOption('no-defects', 'd', InputOption::VALUE_OPTIONAL, 'Ignore previous defects', false)
->addOption('seed', 's', InputOption::VALUE_OPTIONAL, 'Seed the random number generator to get repeatable runs')
->addArgument('path', InputArgument::OPTIONAL, 'File or folder with checks', 'checks');
Expand Down
1 change: 0 additions & 1 deletion src/CheckEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

final class CheckEvents
Expand Down
1 change: 0 additions & 1 deletion src/Events/EndAllEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

class EndAllEvent extends Event
Expand Down
1 change: 0 additions & 1 deletion src/Events/EndEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

use ReflectionMethod;
Expand Down
1 change: 0 additions & 1 deletion src/Events/ErrorEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

class ErrorEvent extends ResultEvent
Expand Down
1 change: 0 additions & 1 deletion src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

abstract class Event extends \Symfony\Component\EventDispatcher\Event
Expand Down
1 change: 0 additions & 1 deletion src/Events/FailureEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

class FailureEvent extends ResultEvent
Expand Down
1 change: 0 additions & 1 deletion src/Events/ResultEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

use ReflectionMethod;
Expand Down
1 change: 0 additions & 1 deletion src/Events/StartAllEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

class StartAllEvent extends Event
Expand Down
1 change: 0 additions & 1 deletion src/Events/StartEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

use ReflectionMethod;
Expand Down
1 change: 0 additions & 1 deletion src/Events/SuccessEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Events;

class SuccessEvent extends ResultEvent
Expand Down
1 change: 0 additions & 1 deletion src/ExecutionError.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

use Exception;
Expand Down
1 change: 0 additions & 1 deletion src/ExecutionFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

use Exception;
Expand Down
1 change: 0 additions & 1 deletion src/Gen.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

use Faker\Factory;
Expand Down
1 change: 0 additions & 1 deletion src/Reporters/ConsoleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Reporters;

use Datashaman\PHPCheck\CheckCommand;
Expand Down
57 changes: 57 additions & 0 deletions src/Reporters/HtmlCoverageReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);
/*
* This file is part of the phpcheck package.
*
* ©Marlin Forbes <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Datashaman\PHPCheck\Reporters;

use Datashaman\PHPCheck\CheckEvents;
use Datashaman\PHPCheck\Events;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlFacade;

class HtmlCoverageReporter extends Reporter
{
protected $coverage;

public static function getSubscribedEvents(): array
{
return [
CheckEvents::END => 'onEnd',
CheckEvents::END_ALL => 'onEndAll',
CheckEvents::START => 'onStart',
CheckEvents::START_ALL => 'onStartAll',
];
}

public function onEnd(Events\EndEvent $event): void
{
$this->coverage->stop();
}

public function onEndAll(Events\EndAllEvent $event): void
{
$writer = new HtmlFacade();
$writer->process($this->coverage, $this->input->getOption('coverage-html'));
}

public function onStart(Events\StartEvent $event): void
{
$this->coverage->start($event->method->getName());
}

public function onStartAll(Events\StartAllEvent $event): void
{
if (!$this->input->getOption('coverage-html')) {
$this->output->writeln('<error>You must specify a folder with --coverage-html</error>');
exit(1);
}

$this->coverage = new CodeCoverage();
$this->coverage->filter()->addDirectoryToWhitelist('./src');
}
}
1 change: 0 additions & 1 deletion src/Reporters/JUnitReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Reporters;

use Datashaman\PHPCheck\CheckEvents;
Expand Down
1 change: 0 additions & 1 deletion src/Reporters/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck\Reporters;

use Datashaman\PHPCheck\Runner;
Expand Down
61 changes: 61 additions & 0 deletions src/Reporters/TextCoverageReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);
/*
* This file is part of the phpcheck package.
*
* ©Marlin Forbes <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Datashaman\PHPCheck\Reporters;

use Datashaman\PHPCheck\CheckEvents;
use Datashaman\PHPCheck\Events;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report\Text;

class TextCoverageReporter extends Reporter
{
protected $coverage;

public static function getSubscribedEvents(): array
{
return [
CheckEvents::END => 'onEnd',
CheckEvents::END_ALL => 'onEndAll',
CheckEvents::START => 'onStart',
CheckEvents::START_ALL => 'onStartAll',
];
}

public function onEnd(Events\EndEvent $event): void
{
$this->coverage->stop();
}

public function onEndAll(Events\EndAllEvent $event): void
{
$writer = new Text();


if ($this->input->getOption('coverage-text')) {
$output = $writer->process($this->coverage, false);
file_put_contents($this->input->getOption('coverage-text'), $output);

return;
}

print $writer->process($this->coverage, true);
}

public function onStart(Events\StartEvent $event): void
{
$this->coverage->start($event->method->getName());
}

public function onStartAll(Events\StartAllEvent $event): void
{
$this->coverage = new CodeCoverage();
$this->coverage->filter()->addDirectoryToWhitelist('./src');
}
}
1 change: 0 additions & 1 deletion src/RunState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

use ReflectionMethod;
Expand Down
9 changes: 8 additions & 1 deletion src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Datashaman\PHPCheck;

use InvalidArgumentException;
Expand Down Expand Up @@ -148,6 +147,14 @@ public function execute(InputInterface $input, OutputInterface $output): void

$this->dispatcher->addSubscriber(new Reporters\ConsoleReporter($this));

if ($input->getOption('coverage-html') !== false) {
$this->dispatcher->addSubscriber(new Reporters\HtmlCoverageReporter($this));
}

if ($input->getOption('coverage-text') !== false) {
$this->dispatcher->addSubscriber(new Reporters\TextCoverageReporter($this));
}

if ($input->getOption('log-junit')) {
$reporter = new Reporters\JUnitReporter($this);
$this->dispatcher->addSubscriber($reporter);
Expand Down

0 comments on commit cbf5228

Please sign in to comment.