Skip to content

Commit d62d598

Browse files
Split LiveCodeCoverage
1 parent ecdd788 commit d62d598

File tree

4 files changed

+74
-53
lines changed

4 files changed

+74
-53
lines changed

src/LiveCodeCoverage/CodeCoverageFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function createFromPhpUnitConfiguration($phpunitFilePath)
1616
$codeCoverage = self::createDefault();
1717

1818
// Accomodate for PHPUnit 5.7
19-
if (class_exists('PHPUnit_Util_Configuration')) {
19+
if (!class_exists('PHPUnit\Util\Configuration')) {
2020
class_alias('PHPUnit_Util_Configuration', 'PHPUnit\Util\Configuration');
2121
}
2222

src/LiveCodeCoverage/LiveCodeCoverage.php

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,68 +59,22 @@ public static function bootstrap($collectCodeCoverage, $storageDirectory, $phpun
5959
return [$liveCodeCoverage, 'stopAndSave'];
6060
}
6161

62-
/**
63-
* @param bool $coverageEnabled
64-
* @param string $storageDirectory
65-
* @param null $phpunitConfigFilePath
66-
* @return callable
67-
*/
68-
public static function bootstrapRemoteCoverage($coverageEnabled, $storageDirectory, $phpunitConfigFilePath = null)
69-
{
70-
Assert::boolean($coverageEnabled);
71-
if (!$coverageEnabled) {
72-
return function () {
73-
// do nothing - code coverage is not enabled
74-
};
75-
}
76-
77-
$coverageGroup = isset($_GET['coverage_group']) ? $_GET['coverage_group'] :
78-
(isset($_COOKIE['coverage_group']) ? $_COOKIE['coverage_group'] : null);
79-
80-
$storageDirectory .= ($coverageGroup ? '/' . $coverageGroup : '');
81-
82-
if (isset($_GET['export_code_coverage'])) {
83-
self::exportCoverageData($storageDirectory);
84-
exit;
85-
}
86-
87-
$coverageId = isset($_GET['coverage_id']) ? $_GET['coverage_id'] :
88-
(isset($_COOKIE['coverage_id']) ? $_COOKIE['coverage_id'] : 'live-coverage');
89-
90-
return self::bootstrap(
91-
isset($_COOKIE['collect_code_coverage']) && (bool)$_COOKIE['collect_code_coverage'],
92-
$storageDirectory,
93-
$phpunitConfigFilePath,
94-
$coverageId
95-
);
96-
}
97-
9862
private function start()
9963
{
10064
$this->codeCoverage->start($this->coverageId);
10165
}
10266

10367
/**
104-
* @param $coverageDirectory
105-
* @return void
68+
* Stop collecting code coverage data and save it.
10669
*/
107-
public static function exportCoverageData($coverageDirectory)
108-
{
109-
$codeCoverage = Storage::loadFromDirectory($coverageDirectory);
110-
111-
header('Content-Type: text/plain');
112-
echo serialize($codeCoverage);
113-
}
114-
11570
public function stopAndSave()
11671
{
11772
$this->codeCoverage->stop();
11873

119-
Storage::storeCodeCoverage($this->codeCoverage, $this->storageDirectory, $this->covFileName());
120-
}
121-
122-
private function covFileName()
123-
{
124-
return uniqid(date('YmdHis'), true);
74+
Storage::storeCodeCoverage(
75+
$this->codeCoverage,
76+
$this->storageDirectory,
77+
uniqid(date('YmdHis'), true)
78+
);
12579
}
12680
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace LiveCodeCoverage;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
final class RemoteCodeCoverage
8+
{
9+
const COVERAGE_ID_KEY = 'coverage_id';
10+
const COLLECT_CODE_COVERAGE_KEY = 'collect_code_coverage';
11+
const COVERAGE_GROUP_KEY = 'coverage_group';
12+
const EXPORT_CODE_COVERAGE_KEY = 'export_code_coverage';
13+
14+
/**
15+
* Enable remote code coverage.
16+
*
17+
* @param bool $coverageEnabled Whether or not code coverage should be enabled
18+
* @param string $storageDirectory Where to store the generated coverage data files
19+
* @param string $phpunitConfigFilePath The path to the PHPUnit XML file containing the coverage filter configuration
20+
* @return callable Call this value at the end of the request life cycle.
21+
*/
22+
public static function bootstrap($coverageEnabled, $storageDirectory, $phpunitConfigFilePath = null)
23+
{
24+
Assert::boolean($coverageEnabled);
25+
if (!$coverageEnabled) {
26+
return function () {
27+
// do nothing - code coverage is not enabled
28+
};
29+
}
30+
31+
$coverageGroup = isset($_GET[self::COVERAGE_GROUP_KEY]) ? $_GET[self::COVERAGE_GROUP_KEY] :
32+
(isset($_COOKIE[self::COVERAGE_GROUP_KEY]) ? $_COOKIE[self::COVERAGE_GROUP_KEY] : null);
33+
34+
$storageDirectory .= ($coverageGroup ? '/' . $coverageGroup : '');
35+
36+
if (isset($_GET[self::EXPORT_CODE_COVERAGE_KEY])) {
37+
header('Content-Type: text/plain');
38+
echo self::exportCoverageData($storageDirectory);
39+
exit;
40+
}
41+
42+
$coverageId = isset($_GET[self::COVERAGE_ID_KEY]) ? $_GET[self::COVERAGE_ID_KEY] :
43+
(isset($_COOKIE[self::COVERAGE_ID_KEY]) ? $_COOKIE[self::COVERAGE_ID_KEY] : 'live-coverage');
44+
45+
return LiveCodeCoverage::bootstrap(
46+
isset($_COOKIE[self::COLLECT_CODE_COVERAGE_KEY]) && (bool)$_COOKIE[self::COLLECT_CODE_COVERAGE_KEY],
47+
$storageDirectory,
48+
$phpunitConfigFilePath,
49+
$coverageId
50+
);
51+
}
52+
53+
/**
54+
* Get previously collected coverage data (combines all coverage data stored in the given directory, merges and serializes it).
55+
*
56+
* @param string $coverageDirectory
57+
* @return string
58+
*/
59+
public static function exportCoverageData($coverageDirectory)
60+
{
61+
$codeCoverage = Storage::loadFromDirectory($coverageDirectory);
62+
63+
return serialize($codeCoverage);
64+
}
65+
}

src/LiveCodeCoverage/Storage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static function storeCodeCoverage(CodeCoverage $coverage, $storageDirecto
3636
*/
3737
public static function loadFromDirectory($storageDirectory)
3838
{
39+
Assert::string($storageDirectory);
40+
3941
$coverage = new CodeCoverage();
4042

4143
if (!is_dir($storageDirectory)) {

0 commit comments

Comments
 (0)