Skip to content

Commit

Permalink
Add option to render HTML report (Nimut#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
IchHabRecht authored May 15, 2019
2 parents 9d9d409 + 52e6874 commit 048ef5e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 24 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ Composer will add the package as a dev requirement to your composer.json and ins
The coverage command merges files containing PHP_CodeCoverage objects into one file in Clover XML format.

```bash
$ vendor/bin/phpunit-merger coverage <directory> <file>
$ vendor/bin/phpunit-merger coverage <directory> [--html=<directory>] [<file>]
```

**Arguments**

- `directory`: Provides the directory containing one or multiple files with PHP_CodeCoverage objects
- `file`: File where the merged result should be stored
- `directory`: Directory containing one or multiple files with PHP_CodeCoverage objects
- `file`: File where the merged result should be stored. Default: Standard output

**Options**

- `html`: Directory where the HTML report should be stored

### Log

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"symfony/finder": "^3.0 || ^4.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0"
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0",
"symfony/filesystem": "^3.4"
},
"suggest": {
"friendsofphp/php-cs-fixer": "Tool to automatically fix PHP coding standards issues"
Expand Down
32 changes: 29 additions & 3 deletions src/PhpunitMerger/Command/CoverageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report\Clover;
use SebastianBergmann\CodeCoverage\Report\Html\Facade;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;

Expand All @@ -22,8 +24,14 @@ protected function configure()
)
->addArgument(
'file',
InputArgument::REQUIRED,
'The file where to write the merged result'
InputArgument::OPTIONAL,
'The file where to write the merged result. Default: Standard output'
)
->addOption(
'html',
null,
InputOption::VALUE_REQUIRED,
'The directory where to write the code coverage report in HTML format'
);
}

Expand All @@ -40,7 +48,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
$codeCoverage->merge($coverage);
}

$this->writeCodeCoverage($codeCoverage, $output, $input->getArgument('file'));
$html = $input->getOption('html');
if ($html !== null) {
$this->writeHtmlReport($codeCoverage, $html);
}
}

private function writeCodeCoverage(CodeCoverage $codeCoverage, OutputInterface $output, $file = null)
{
$writer = new Clover();
$writer->process($codeCoverage, $input->getArgument('file'));
$buffer = $writer->process($codeCoverage, $file);
if ($file === null) {
$output->write($buffer);
}
}

private function writeHtmlReport(CodeCoverage $codeCoverage, string $destination)
{
$writer = new Facade();
$writer->process($codeCoverage, $destination);
}
}
16 changes: 12 additions & 4 deletions tests/PhpunitMerger/Command/AbstractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Nimut\PhpunitMerger\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

abstract class AbstractCommandTest extends TestCase
{
Expand All @@ -15,12 +16,19 @@ abstract class AbstractCommandTest extends TestCase
*/
protected $outputFile = 'foo.bar';

public function testFileNotExists()
public function assertOutputFileNotExists()
{
if (file_exists($this->logDirectory . $this->outputFile)) {
unlink($this->logDirectory . $this->outputFile);
}
$filesystem = new Filesystem();
$filesystem->remove($this->logDirectory . $this->outputFile);

$this->assertFileNotExists($this->logDirectory . $this->outputFile);
}

public function assertOutputDirectoryNotExists()
{
$filesystem = new Filesystem();
$filesystem->remove($this->logDirectory . dirname($this->outputFile));

$this->assertDirectoryNotExists($this->logDirectory . dirname($this->outputFile));
}
}
77 changes: 70 additions & 7 deletions tests/PhpunitMerger/Command/CoverageCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
namespace Nimut\PhpunitMerger\Tests\Command;

use Nimut\PhpunitMerger\Command\CoverageCommand;
use Prophecy\Argument;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

class CoverageCommandTest extends AbstractCommandTest
{
Expand All @@ -12,23 +13,85 @@ class CoverageCommandTest extends AbstractCommandTest
*/
protected $outputFile = 'coverage.xml';

/**
* @depends testFileNotExists
*/
public function testRunMergesCoverage()
public function testCoverageWritesOutputFile()
{
$this->assertOutputFileNotExists();

$input = new ArgvInput(
[
'coverage',
$this->logDirectory . 'coverage/',
$this->logDirectory . $this->outputFile,
]
);
$output = $this->prophesize(OutputInterface::class);
$output->write(Argument::any())->shouldNotBeCalled();

$command = new CoverageCommand();
$command->run($input, $output->reveal());

$this->assertFileExists($this->logDirectory . $this->outputFile);
}

public function testCoverageWritesStandardOutput()
{
$this->assertOutputFileNotExists();

$input = new ArgvInput(
[
'coverage',
$this->logDirectory . 'coverage/',
]
);
$output = $this->prophesize(OutputInterface::class);
$output->write(Argument::type('string'))->shouldBeCalled();

$command = new CoverageCommand();
$command->run($input, $output->reveal());
}

public function testCoverageWritesHtmlReport()
{
$this->outputFile = 'html/index.html';
$this->assertOutputDirectoryNotExists();

$input = new ArgvInput(
[
'coverage',
$this->logDirectory . 'coverage/',
'--html=' . $this->logDirectory . dirname($this->outputFile),
]
);
$output = $this->prophesize(OutputInterface::class);
$output->write(Argument::type('string'))->shouldBeCalled();

$command = new CoverageCommand();
$command->run($input, $output->reveal());

$this->assertFileExists($this->logDirectory . $this->outputFile);
}

public function testCoverageWritesOutputFileAndHtmlReport()
{
$this->outputFile = 'html/coverage.xml';
$this->assertOutputFileNotExists();
$this->assertOutputDirectoryNotExists();

$input = new ArgvInput(
[
'coverage',
$this->logDirectory . 'coverage/',
'--html=' . $this->logDirectory . dirname($this->outputFile),
$this->logDirectory . $this->outputFile,
]
);
$output = new ConsoleOutput();
$output = $this->prophesize(OutputInterface::class);
$output->write(Argument::any())->shouldNotBeCalled();

$command = new CoverageCommand();
$command->run($input, $output);
$command->run($input, $output->reveal());

$this->assertFileExists($this->logDirectory . $this->outputFile);
$this->assertFileExists($this->logDirectory . dirname($this->outputFile) . '/index.html');
}
}
11 changes: 5 additions & 6 deletions tests/PhpunitMerger/Command/LogCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Nimut\PhpunitMerger\Command\LogCommand;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

class LogCommandTest extends AbstractCommandTest
{
Expand All @@ -12,22 +12,21 @@ class LogCommandTest extends AbstractCommandTest
*/
protected $outputFile = 'log.xml';

/**
* @depends testFileNotExists
*/
public function testRunMergesCoverage()
{
$this->assertOutputFileNotExists();

$input = new ArgvInput(
[
'log',
$this->logDirectory . 'log/',
$this->logDirectory . $this->outputFile,
]
);
$output = new ConsoleOutput();
$output = $this->prophesize(OutputInterface::class);

$command = new LogCommand();
$command->run($input, $output);
$command->run($input, $output->reveal());

$this->assertFileExists($this->logDirectory . $this->outputFile);
}
Expand Down

0 comments on commit 048ef5e

Please sign in to comment.