Cloak is a library that takes a code coverage.
This library works with PHP5.5 or more.
- xdebug >= 2.2.2
-
Install the composer.
-
Install the cloak.
composer require cloak/cloak --dev
Setup is required to take a code coverage.
You can use the ConfigurationBuilder, and to apply the settings to the analyzer.
<?php
use cloak\CoverageAnalyzer;
use cloak\configuration\ConfigurationBuilder;
$builder = new ConfigurationBuilder();
$builder->includeFile('/example/src')
->excludeFile('/spec');
$analyzer = new CoverageAnalyzer( $builder->build() );
Run the start / stop at the place where want to take the code coverage.
After you can get the report, you need to run the getResult method.
$analyzer->start();
//I write code here want to take code coverage
example\example1();
$analyzer->stop();
$files = $analyzer->getResult()->getFiles();
foreach ($files as $file) {
$result = sprintf("%s > %6.2f%% (%d/%d)",
$file->getName(),
$file->getCodeCoverage()->value(),
$file->getExecutedLineCount(),
$file->getExecutableLineCount()
);
echo $result . "\n";
}
You can use at the same time more than one reporter.
Reporter that are supported by default are as follows.
- TextReporter
- ProcessingTimeReporter
- LcovReporter
- MarkdownReporter
- TreeReporter
Usage is as follows.
$reporter = new CompositeReporter([
new TextReporter(),
new ProcessingTimeReporter()
]);
$builder = new ConfigurationBuilder();
$builder->includeFile('/example/src')
->excludeFile('/spec')
->reporter($reporter);
$analyzer = new CoverageAnalyzer( $builder->build() );
Code Coverage Started: 1 July 2014 at 12:00
100.00% (19/19) src/Analyzer.php
100.00% (27/27) src/Reporter/TextReporter.php
85.71% ( 6/ 7) src/Reporter/Reportable.php
81.25% (13/16) src/Configuration.php
58.33% (14/24) src/ConfigurationBuilder.php
Code Coverage: 96.70%
Code Coverage Finished in 1.44294 seconds
If you use the configuration file, you can code simple.
use cloak\CoverageAnalyzer;
use cloak\configuration\ConfigurationLoader;
$loader = new ConfigurationLoader();
$configuration = $loader->loadConfiguration('cloak.toml');
$analyzer = new CoverageAnalyzer($configuration);
$analyzer->start();
$analyzer->stop();
composer test
composer coverage
composer example