From 7db036e95660792fede61b1fce05dc9c423b4cd5 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sat, 20 Jan 2018 16:32:18 +0700 Subject: [PATCH] Added code coverage report for PHPUnit plugin. Issue #86. --- docs/en/plugins/php_unit.md | 1 + .../Plugin/Option/PhpUnitOptions.php | 25 ++++++++++++--- src/PHPCensor/Plugin/PhpUnit.php | 32 +++++++++++++++++-- .../Plugin/Option/PhpUnitOptionsTest.php | 25 +++++++-------- 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/docs/en/plugins/php_unit.md b/docs/en/plugins/php_unit.md index 4699a4cd..ee0ff69a 100644 --- a/docs/en/plugins/php_unit.md +++ b/docs/en/plugins/php_unit.md @@ -35,6 +35,7 @@ test: config: - "path/to/phpunit.xml" path: "app/tests/" + coverage: true ``` Troubleshooting diff --git a/src/PHPCensor/Plugin/Option/PhpUnitOptions.php b/src/PHPCensor/Plugin/Option/PhpUnitOptions.php index bb7cf6dc..5ca869de 100644 --- a/src/PHPCensor/Plugin/Option/PhpUnitOptions.php +++ b/src/PHPCensor/Plugin/Option/PhpUnitOptions.php @@ -9,12 +9,29 @@ namespace PHPCensor\Plugin\Option; */ class PhpUnitOptions { + /** + * @var array + */ protected $options; + + /** + * @var string + */ + protected $location; + + /** + * @var array + */ protected $arguments = []; - public function __construct($options) + /** + * @param array $options + * @param string $location + */ + public function __construct($options, $location) { - $this->options = $options; + $this->options = $options; + $this->location = $location; } /** @@ -102,8 +119,8 @@ class PhpUnitOptions /* * Handles command aliases outside of the args option */ - if (isset($this->options['coverage'])) { - $this->addArgument('coverage-html', $this->options['coverage']); + if (isset($this->options['coverage']) && $this->options['coverage']) { + $this->addArgument('coverage-html', $this->location); } /* diff --git a/src/PHPCensor/Plugin/PhpUnit.php b/src/PHPCensor/Plugin/PhpUnit.php index 72e362b4..77e3c8f2 100644 --- a/src/PHPCensor/Plugin/PhpUnit.php +++ b/src/PHPCensor/Plugin/PhpUnit.php @@ -20,6 +20,16 @@ use PHPCensor\ZeroConfigPluginInterface; */ class PhpUnit extends Plugin implements ZeroConfigPluginInterface { + /** + * @var string + */ + protected $buildDirectory; + + /** + * @var string + */ + protected $location; + /** @var string[] Raw options from the config file */ protected $options = []; @@ -30,7 +40,7 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface { return 'php_unit'; } - + /** * Standard Constructor * $options['config'] Path to a PHPUnit XML configuration file. @@ -47,7 +57,10 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface { parent::__construct($builder, $build, $options); - $this->options = new PhpUnitOptions($options); + $this->buildDirectory = $this->build->getProjectId() . '/' . $this->build->getId(); + $this->location = PUBLIC_DIR . 'artifacts/phpunit/' . $this->buildDirectory . '/coverage'; + + $this->options = new PhpUnitOptions($options, $this->location); } /** @@ -134,12 +147,27 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface $options->addArgument('configuration', $buildPath . $configFile); } + if (!file_exists($this->location) && $options->getOption('coverage')) { + mkdir($this->location, 0777, true); + } + $arguments = $this->builder->interpolate($options->buildArgumentString()); $cmd = $this->findBinary('phpunit') . ' %s %s'; $success = $this->builder->executeCommand($cmd, $arguments, $directory); $this->processResults($logFile, $logFormat); + $config = $this->builder->getSystemConfig('php-censor'); + + if ($options->getOption('coverage')) { + $this->builder->logSuccess( + sprintf( + "\nPHPUnit successful.\nYou can use coverage report: %s", + $config['url'] . '/artifacts/phpunit/' . $this->buildDirectory . '/coverage/index.html' + ) + ); + } + return $success; } diff --git a/tests/PHPCensor/Plugin/Option/PhpUnitOptionsTest.php b/tests/PHPCensor/Plugin/Option/PhpUnitOptionsTest.php index 1bb696a0..378cbee2 100644 --- a/tests/PHPCensor/Plugin/Option/PhpUnitOptionsTest.php +++ b/tests/PHPCensor/Plugin/Option/PhpUnitOptionsTest.php @@ -25,32 +25,30 @@ class PhpUnitOptionsTest extends \PHPUnit\Framework\TestCase 'configuration' => 'tests/phpunit.xml', ], ], + [ + [ + 'coverage' => '', + ], + [], + ], [ [ 'coverage' => '/path/to/coverage2/', - 'args' => [ - 'coverage-html' => '/path/to/coverage1/', - ], ], [ - 'coverage-html' => [ - '/path/to/coverage1/', - '/path/to/coverage2/', - ], + 'coverage-html' => '/location', ], ], [ [ + 'coverage' => true, 'directory' => [ '/path/to/test1/', '/path/to/test2/', ], - 'args' => [ - 'coverage-html' => '/path/to/coverage1/', - ], ], [ - 'coverage-html' => '/path/to/coverage1/', + 'coverage-html' => '/location', ], ], [ @@ -97,7 +95,7 @@ class PhpUnitOptionsTest extends \PHPUnit\Framework\TestCase */ public function testCommandArguments($rawOptions, $parsedArguments) { - $options = new PhpUnitOptions($rawOptions); + $options = new PhpUnitOptions($rawOptions, '/location'); $this->assertSame($parsedArguments, $options->getCommandArguments()); } @@ -107,7 +105,8 @@ class PhpUnitOptionsTest extends \PHPUnit\Framework\TestCase [ 'run_from' => '/path/to/run/from', 'path' => 'subTest', - ] + ], + '/location' ); $this->assertEquals('/path/to/run/from', $options->getRunFrom());