Skip to content

Commit 86703e4

Browse files
Wrap the concept of being enabled
1 parent 9127eb3 commit 86703e4

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ In your front controller (e.g. `index.php`), add the following:
1919

2020
use LiveCodeCoverage\LiveCodeCoverage;
2121

22-
$liveCodeCoverage = LiveCodeCoverage::bootstrap(
22+
$shutDownCodeCoverage = LiveCodeCoverage::bootstrap(
23+
function () { return (bool)getenv('CODE_COVERAGE_ENABLED'); },
2324
__DIR__ . '/../var/coverage',
2425
__DIR__ . '/../phpunit.xml.dist'
2526
);
2627

27-
// Run your web application now
28+
// Run your web application now...
2829

29-
$liveCodeCoverage->stopAndSave();
30+
// This will save and store collected coverage data:
31+
$shutDownCodeCoverage();
3032
```
3133

32-
- The first argument is the directory where all the collected coverage data will be stored (`*.cov` files). This directory should already exist and be writable.
34+
- The first argument passed to `LiveCodeCoverage::bootstrap()` is a callable that will be used to determine if code coverage is enabled at all. The example shows how you can use an environment variable for that. Make
35+
- The second argument is the directory where all the collected coverage data will be stored (`*.cov` files). If this directory doesn't exist yet, it will be created.
3336
- The second argument is the path to a PHPUnit configuration file. Its `<filter>` section will be used to configure the code coverage whitelist. For example, this `phpunit.xml.dist` file might look something like this:
3437

3538
```xml
@@ -50,12 +53,12 @@ If you don't provide a PHPUnit configuration file, no filters will be applied, s
5053
If your application is a legacy application which `exit()`s or `die()`s before execution reaches the end of your front controller, the bootstrap should be slightly different:
5154

5255
```php
53-
$liveCodeCoverage = LiveCodeCoverage::bootstrap(
56+
$shutDownCodeCoverage = LiveCodeCoverage::bootstrap(
5457
// ...
5558
);
56-
$liveCodeCoverage->stopAndSaveOnExit();
59+
register_shutdown_function($shutDownCodeCoverage);
5760

58-
// Run your web application now
61+
// Run your web application now...
5962
```
6063

6164
## Generating code coverage reports (HTML, Clover, etc.)

src/LiveCodeCoverage/LiveCodeCoverage.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,22 @@ private function __construct(CodeCoverage $codeCoverage, $storageDirectory, $cov
2929
$this->storageDirectory = $storageDirectory;
3030
}
3131

32-
public static function bootstrap($storageDirectory, $phpunitConfigFilePath = null, $coverageId = 'live-coverage')
32+
/**
33+
* @param callable $coverageEnabled
34+
* @param string $storageDirectory
35+
* @param string|null $phpunitConfigFilePath
36+
* @param string $coverageId
37+
* @return callable
38+
*/
39+
public static function bootstrap($coverageEnabled, $storageDirectory, $phpunitConfigFilePath = null, $coverageId = 'live-coverage')
3340
{
41+
Assert::isCallable($coverageEnabled);
42+
if (!$coverageEnabled()) {
43+
return function () {
44+
// do nothing - code coverage is not enabled
45+
};
46+
}
47+
3448
if ($phpunitConfigFilePath !== null) {
3549
Assert::file($phpunitConfigFilePath);
3650
$codeCoverage = CodeCoverageFactory::createFromPhpUnitConfiguration($phpunitConfigFilePath);
@@ -42,25 +56,38 @@ public static function bootstrap($storageDirectory, $phpunitConfigFilePath = nul
4256

4357
$liveCodeCoverage->start();
4458

45-
return $liveCodeCoverage;
59+
return [$liveCodeCoverage, 'stopAndSave'];
4660
}
4761

48-
public static function bootstrapRemoteCoverage($storageDirectory, $phpunitConfigFilePath = null)
62+
/**
63+
* @param callable $coverageEnabled
64+
* @param string $storageDirectory
65+
* @param null $phpunitConfigFilePath
66+
* @return callable
67+
*/
68+
public static function bootstrapRemoteCoverage($coverageEnabled, $storageDirectory, $phpunitConfigFilePath = null)
4969
{
70+
Assert::isCallable($coverageEnabled);
71+
if (!$coverageEnabled()) {
72+
return function () {
73+
// do nothing - code coverage is not enabled
74+
};
75+
}
76+
5077
$coverageGroup = isset($_GET['coverage_group']) ? $_GET['coverage_group'] :
5178
(isset($_COOKIE['coverage_group']) ? $_COOKIE['coverage_group'] : null);
5279

53-
$coverageId = isset($_GET['coverage_id']) ? $_GET['coverage_id'] :
54-
(isset($_COOKIE['coverage_id']) ? $_COOKIE['coverage_id'] : 'live-coverage');
55-
5680
$storageDirectory .= ($coverageGroup ? '/' . $coverageGroup : '');
5781

5882
if (isset($_GET['export_code_coverage'])) {
5983
self::exportCoverageData($storageDirectory);
6084
exit;
6185
}
6286

63-
return self::bootstrap($storageDirectory, $phpunitConfigFilePath, $coverageId);
87+
$coverageId = isset($_GET['coverage_id']) ? $_GET['coverage_id'] :
88+
(isset($_COOKIE['coverage_id']) ? $_COOKIE['coverage_id'] : 'live-coverage');
89+
90+
return self::bootstrap($coverageEnabled, $storageDirectory, $phpunitConfigFilePath, $coverageId);
6491
}
6592

6693
private function start()
@@ -80,11 +107,6 @@ public static function exportCoverageData($coverageDirectory)
80107
echo serialize($codeCoverage);
81108
}
82109

83-
public function stopAndSaveOnExit()
84-
{
85-
register_shutdown_function([$this, 'stopAndSave']);
86-
}
87-
88110
public function stopAndSave()
89111
{
90112
$this->codeCoverage->stop();

test/functional/prepend.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
use LiveCodeCoverage\LiveCodeCoverage;
44

5-
$liveCodeCoverage = LiveCodeCoverage::bootstrap(__DIR__ . '/coverage', __DIR__ . '/phpunit.xml.dist');
6-
$liveCodeCoverage->stopAndSaveOnExit();
5+
$shutDownCodeCoverage = LiveCodeCoverage::bootstrap(
6+
function() { return true; },
7+
__DIR__ . '/coverage',
8+
__DIR__ . '/phpunit.xml.dist'
9+
);
10+
register_shutdown_function($shutDownCodeCoverage);

0 commit comments

Comments
 (0)