leanphp/phpspec-code-coverage
Installation Add the package via Composer:
composer require --dev leanphp/phpspec-code-coverage
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
// ...
LeanPhp\PhpSpecCodeCoverage\PhpSpecCodeCoverageServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="LeanPhp\PhpSpecCodeCoverage\PhpSpecCodeCoverageServiceProvider" --tag="config"
Update config/phpspec-code-coverage.php to match your project’s needs (e.g., coverage thresholds, report paths).
First Run Execute PhpSpec with coverage:
./vendor/bin/phpspec run --format=leanphp
Reports will generate in the configured output directory (default: storage/coverage).
./vendor/bin/phpspec run --format=leanphp --min=80
(Replace 80 with your desired threshold.)Integrating with Laravel’s Testing
phpunit coverage tools for consistency../vendor/bin/phpspec run --format=leanphp --min=70 && ./vendor/bin/phpunit --coverage-text
Custom Report Generation
// app/Console/Commands/GenerateCoverageReport.php
use LeanPhp\PhpSpecCodeCoverage\Formatters\CliFormatter;
class GenerateCoverageReport extends Command {
protected function handle() {
$formatter = new CliFormatter();
$coverage = $formatter->format($this->getPhpSpecCoverageData());
// Custom logic (e.g., save to DB, Slack notification)
}
}
Dynamic Thresholds
# phpspec.yml
extensions:
LeanPhp\PhpSpecCodeCoverage\PhpSpecCodeCoverageExtension:
min: 85
files:
spec/FeatureSpec.php: 90
coverage_runs).False Positives/Negatives
make:model).config/phpspec-code-coverage.php:
'exclude' => [
'app/Traits/*',
'app/Interfaces/*',
],
Performance Overhead
--parallel or run coverage only on changed files:
./vendor/bin/phpspec run --format=leanphp --files=spec/ChangedSpec.php
Config Overrides
--min) override phpspec.yml settings.Verbose Output: Enable debug mode in phpspec.yml:
extensions:
LeanPhp\PhpSpecCodeCoverage\PhpSpecCodeCoverageExtension:
debug: true
Logs will appear in storage/logs/phpspec-coverage.log.
Coverage Artifacts: Clear old reports before running:
rm -rf storage/coverage/*
Custom Formatters
LeanPhp\PhpSpecCodeCoverage\FormatterInterface to create new report types (e.g., JUnit XML for CI tools):
class JUnitFormatter implements FormatterInterface {
public function format(array $coverageData): string {
// Generate JUnit-compatible XML
}
}
phpspec.yml:
formatters:
leanphp-junit: LeanPhp\PhpSpecCodeCoverage\Formatters\JUnitFormatter
Hooks
events to trigger actions on coverage changes:
// app/Providers/EventServiceProvider.php
public function boot() {
event(new \LeanPhp\PhpSpecCodeCoverage\Events\CoverageGenerated(
$coverageData
));
}
Laravel Service Container
$this->app->bind(\LeanPhp\PhpSpecCodeCoverage\CoverageAnalyzer::class, function () {
return new \LeanPhp\PhpSpecCodeCoverage\CoverageAnalyzer(
config('phpspec-code-coverage')
);
});
How can I help you explore Laravel packages today?