mirror of
https://github.com/deployphp/deployer.git
synced 2025-02-24 17:22:41 +01:00
The E2E docker-compose stack has been modified to include code coverage for all scripts that are kept in /project and are not in the exclusion list. The start command for `deployer` container has been replaced with a `start-e2e-test.sh` script, which will execute E2E tests, generate a Clover report and then return the exit code of E2E test process to make sure that CI is aware of the test result. Whole idea for collecting E2E coverage has been inspired by the [this][1] article. All of scripts in `deployer` container will now start with additional wrapper-like PHP code, which checks if the `PHP_CCOV_START_FILE` variable is present. If not, then nothing happens, but if the variable points to a PHP file, then that file is being included. The `docker-compose.yml` file has `PHP_CCOV_START_FILE` configured to point to a file, which sets up code coverage, that starts counting which instructions in `/project` directory have been called. Once `php` interpreter starts shutting down, the coverage report is being dumped into temporary files. Once all tests have been processed, a script for merging partial coverage reports is executed and finally a Clover code coverage file is stored in the path defined in the `PHP_CCOV_OUTPUT_FILE` env var. [1]: https://tarunlalwani.com/post/php-code-coverage-web-selenium/
61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../../../vendor/autoload.php';
|
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Selector;
|
|
use SebastianBergmann\CodeCoverage\Filter;
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage;
|
|
use SebastianBergmann\CodeCoverage\Report\PHP as PHPReport;
|
|
|
|
$filter = new Filter();
|
|
$filter->includeDirectory('/project');
|
|
$filter->excludeDirectory('/project/vendor');
|
|
$filter->excludeDirectory('/project/tests');
|
|
$report = new PHPReport();
|
|
|
|
$coverage = new CodeCoverage(
|
|
(new Selector)->forLineCoverage($filter),
|
|
$filter
|
|
);
|
|
|
|
$outputDir = '/tmp/ccov';
|
|
if (!is_dir($outputDir)) {
|
|
mkdir($outputDir);
|
|
}
|
|
|
|
// use anonymous class as we don't really want to pollute class space with this stuff
|
|
(new class ($coverage, $report, $outputDir) {
|
|
/** @var CodeCoverage */
|
|
private $coverage;
|
|
/** @var PHPReport */
|
|
private $report;
|
|
/** @var string */
|
|
private $outputDir;
|
|
/** @var string|null */
|
|
private $coverageName;
|
|
|
|
public function __construct(CodeCoverage $coverage, PHPReport $report, string $outputDir) {
|
|
$this->coverage = $coverage;
|
|
$this->report = $report;
|
|
$this->outputDir = $outputDir;
|
|
}
|
|
|
|
public function start():void {
|
|
register_shutdown_function([$this, 'stop']);
|
|
|
|
$coverageName = uniqid('coverage_');
|
|
$this->coverageName = $coverageName;
|
|
$this->coverage->start($this->coverageName);
|
|
}
|
|
|
|
public function stop():void {
|
|
$this->coverage->stop();
|
|
|
|
$outputFile = $this->outputDir . "/{$this->coverageName}.php";
|
|
$this->report->process($this->coverage, $outputFile);
|
|
}
|
|
})->start();
|
|
|
|
|
|
|