-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperidot.php
124 lines (108 loc) · 3.88 KB
/
peridot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
use Evenement\EventEmitterInterface;
use Peridot\Concurrency\ConcurrencyPlugin;
use Peridot\Console\Environment;
use Peridot\Plugin\Prophecy\ProphecyPlugin;
use Peridot\Reporter\Dot\DotReporterPlugin;
return function (EventEmitterInterface $emitter) {
$prophecy = new ProphecyPlugin($emitter);
$concurrency = new ConcurrencyPlugin($emitter);
$dot = new DotReporterPlugin($emitter);
$emitter->on('peridot.start', function (Environment $env) {
$definition = $env->getDefinition();
$definition->getArgument('path')->setDefault('specs');
});
$debug = getenv('DEBUG');
if ($debug) {
$emitter->on('error', function ($number, $message, $file, $line) {
print "Error: $number - $message:$file:$line\n";
});
}
/***********************************************
* Code Coverage - @todo extract lib for this
***********************************************/
$coverageType = getenv('CODE_COVERAGE');
$coverage = new \PHP_CodeCoverage();
$hhvm = defined('HHVM_VERSION'); //exclude coverage from hhvm because its pretty flawed at the moment
/**
* Execute a function if a concurrency token is
* available, and coverage is enabled. The token
* and coverage are passed to the callback.
*
* @param callable $func
*/
$covered = function (callable $func) use ($coverageType, $coverage, $hhvm) {
$token = getenv('PERIDOT_TEST_TOKEN'); //this is only present in a concurrent context
if ($coverageType && $token && !$hhvm) {
$func($token, $coverage);
}
};
/**
* Return all Peridot code coverage files.
*
* @return array
*/
$coverageFiles = function () {
$path = sys_get_temp_dir();
$coverageFiles = glob($path . '/PCC_*');
return $coverageFiles;
};
/**
* When a runner starts out of process, create a new coverage
* object.
*/
$emitter->on('runner.start', function () use ($covered) {
$covered(function ($token, \PHP_CodeCoverage $coverage) {
$coverage->filter()->addDirectoryToWhitelist(__DIR__ . '/src');
$coverage->start($token);
});
});
/**
* When a runner ends out of process, write it to a temp file.
*/
$emitter->on('runner.end', function () use ($covered) {
$covered(function ($token, \PHP_CodeCoverage $coverage) {
$coverage->stop();
$writer = new \PHP_CodeCoverage_Report_PHP();
$file = tempnam(sys_get_temp_dir(), "PCC_{$token}_");
$writer->process($coverage, $file);
});
});
/**
* When peridot ends in the main process, we aggregate all of our code coverage
* results.
*/
$emitter->on('peridot.end', function () use ($coverageType, $coverageFiles, $hhvm) {
if (! $coverageType || $hhvm) {
return;
}
$coverages = [];
$files = $coverageFiles();
foreach ($files as $file) {
$fileCoverage = file_get_contents($file);
if (substr($fileCoverage, 0, 5) === '<?php') {
$coverageObject = include $file;
$coverages[] = $coverageObject;
}
unlink($file);
}
/**
* Merge all of the results into a single coverage object.
*/
$coverage = array_reduce($coverages, function ($result, $obj) {
if (is_null($result)) {
return $obj;
}
$result->merge($obj);
return $result;
});
//output total coverage
$path = __DIR__ . '/tmp/report';
$writer = new \PHP_CodeCoverage_Report_HTML();
if ($coverageType == 'clover') {
$path = __DIR__ . '/build/logs/clover.xml';
$writer = new \PHP_CodeCoverage_Report_Clover();
}
$writer->process($coverage, $path);
});
};