Menu

[r1]: / src / remote / RemoteCoverageRecorder.php  Maximize  Restore  History

Download this file

296 lines (266 with data), 10.5 kB

  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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/*
* $Id: RemoteCoverageRecorder.php 14665 2005-03-23 19:37:50Z npac $
*
* Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Software License version 2.1
* (See http://www.spikesource.com/license.html)
*/
?>
<?php
if(!defined("__PHPCOVERAGE_HOME")) {
define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__)));
}
require_once __PHPCOVERAGE_HOME . "/util/Utility.php";
require_once __PHPCOVERAGE_HOME . "/CoverageRecorder.php";
require_once __PHPCOVERAGE_HOME . "/remote/XdebugTraceReader.php";
require_once __PHPCOVERAGE_HOME . "/parser/CoverageXmlParser.php";
/**
* A Coverage recorder extension for remote Coverage measurement.
*
* @author Nimish Pachapurkar <npac@spikesource.com>
* @version $Revision: $
* @package SpikePHPCoverage_Remote
*/
class RemoteCoverageRecorder extends CoverageRecorder {
/*{{{ Members */
protected $traceFilePath;
protected $xdebugTraceReader;
protected $tmpDir;
protected $tmpTraceFilename = "phpcoverage.xdebug.trace";
protected $coverageFileName = "phpcoverage.coverage.xml";
protected $xmlStart = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><spike-phpcoverage>";
protected $xmlEnd = "</spike-phpcoverage>";
/*}}}*/
/*{{{ public function __construct() */
/**
* Constructor
*
* @access public
*/
public function __construct(
$includePaths=array("."),
$excludePaths=array(),
$reporter="new HtmlCoverageReporter()"
) {
global $util;
parent::__construct($includePaths, $excludePaths, $reporter);
$this->isRemote = true;
$this->phpCoverageFiles[] = "phpcoverage.remote.inc.php";
$this->phpCoverageFiles[] = "phpcoverage.remote.top.inc.php";
$this->phpCoverageFiles[] = "phpcoverage.remote.bottom.inc.php";
// configuration
$this->tmpDir = $util->getTmpDir();
}
/*}}}*/
/*{{{ Getters and Setters */
public function getTraceFilePath() {
return $this->traceFilePath;
}
public function setTraceFilePath($traceFilePath) {
$this->traceFilePath = $traceFilePath;
}
public function getTmpDir() {
return $this->tmpDir;
}
public function setTmpDir($tmpTraceDir) {
$this->tmpDir = $tmpTraceDir;
}
public function getCoverageFileName() {
return $this->coverageFileName;
}
public function setCoverageFileName($covFileName) {
$this->coverageFileName = $covFileName;
}
/*}}}*/
/*{{{ public function cleanCoverageFile() */
/**
* Deletes a coverage data file if one exists.
*
* @return Boolean True on success, False on failure.
* @access public
*/
public function cleanCoverageFile() {
$filepath = $this->tmpDir . "/" . $this->coverageFileName;
if(file_exists($filepath)) {
if(is_writable($filepath)) {
unlink($filepath);
}
else {
$this->logger->error("[RemoteCoverageRecorder::cleanCoverageFile()] "
. "ERROR: Cannot delete $filepath.", __FILE__, __LINE__);
return false;
}
}
return true;
}
/*}}}*/
/*{{{ protected function prepareCoverageXml() */
/**
* Convert the Coverage data into an XML.
*
* @return String XML generated from Coverage data
* @access protected
*/
protected function prepareCoverageXml() {
global $util;
$xmlString = "";
$xmlBody = "";
if(!empty($this->coverageData)) {
foreach($this->coverageData as $file => &$lines) {
$xmlBody .= "<file path=\"". $util->replaceBackslashes($file) . "\">";
foreach($lines as $linenum => &$frequency) {
$xmlBody .= "<line line-number=\"" . $linenum . "\"";
$xmlBody .= " frequency=\"" . $frequency . "\"/>";
}
$xmlBody .= "</file>\n";
}
unset($this->coverageData);
}
else {
$this->logger->info("[RemoteCoverageRecorder::prepareCoverageXml()] Coverage data is empty.",
__FILE__, __LINE__);
}
$xmlString .= $xmlBody;
$this->logger->debug("[RemoteCoverageRecorder::prepareCoverageXml()] Xml: " . $xmlString, __FILE__, __LINE__);
return $xmlString;
}
/*}}}*/
/*{{{ protected function parseCoverageXml() */
/**
* Parse coverage XML to regenerate the Coverage data array.
*
* @param $xml XML String or URL of the coverage data
* @param $stream=false Is the input a stream?
* @return
* @access protected
*/
protected function parseCoverageXml(&$xml, $stream=false) {
$xmlParser = new CoverageXmlParser();
if($stream) {
$xmlParser->setInput($xml);
}
else {
$xmlParser->setInputString($xml);
}
$xmlParser->parse();
$this->coverageData =& $xmlParser->getCoverageData();
}
/*}}}*/
/*{{{ public function getCoverageXml() */
/**
* Dumps the coverage data in XML format
*
* @access public
*/
public function getCoverageXml() {
$filepath = $this->tmpDir . "/" . $this->coverageFileName;
if(file_exists($filepath) && is_readable($filepath)) {
$fp = fopen($filepath, "r");
if($fp) {
while(!feof($fp)) {
$xml = fread($fp, 4096);
echo $xml;
}
fclose($fp);
return true;
}
else {
$this->logger->error("Could not read coverage data file.",
__FILE__, __LINE__);
}
}
else {
$this->logger->error("[RemoteCoverageRecorder::getCoverageXml()] "
. "ERROR: Cannot read file " . $filepath, __FILE__, __LINE__);
}
return false;
}
/*}}} */
/*{{{ protected function appendDataToFile() */
/**
* Append coverage data to xml file
*
* @param $newXml New xml recorded
* @return True on success; false otherwise
* @access protected
*/
protected function appendDataToFile($newXml) {
$filepath = $this->tmpDir . "/" . $this->coverageFileName;
if(!file_exists($filepath)) {
// If new file, write the xml start and end tags
$bytes = file_put_contents($filepath, $this->xmlStart . "\n" . $this->xmlEnd);
if(!$bytes) {
$this->logger->critical("[RemoteCoverageRecorder::appendDataToFile()] Could not create file: " . $filepath, __FILE__, __LINE__);
return false;
}
}
if(file_exists($filepath) && is_readable($filepath)) {
$res = fopen($filepath, "r+");
if($res) {
fseek($res, -1 * strlen($this->xmlEnd), SEEK_END);
$ret = fwrite($res, $newXml);
if(!$ret) {
$this->logger->error("[RemoteCoverageRecorder::appendDataToFile()] Could not append data to file.",
__FILE__, __LINE__);
fclose($res);
return false;
}
fwrite($res, $this->xmlEnd);
fclose($res);
}
else {
$this->logger->error("[RemoteCoverageRecorder::appendDataToFile()] Error opening file for writing: " . $filepath,
__FILE__, __LINE__);
return false;
}
}
return true;
}
/*}}}*/
/*{{{ public function saveCoverageXml() */
/**
* Append coverage xml to a xml data file.
*
* @return Boolean True on success, False on error
* @access public
*/
public function saveCoverageXml() {
$filepath = $this->tmpDir . "/" . $this->coverageFileName;
if($this->stopInstrumentation()) {
$xml = $this->prepareCoverageXml();
$ret = $this->appendDataToFile($xml);
if(!$ret) {
$this->logger->warn("[RemoteCoverageRecorder::saveCoverageXml()] "
. "ERROR: Nothing was written to " . $filepath,
__FILE__, __LINE__);
return false;
}
$this->logger->info("[RemoteCoverageRecorder::saveCoverageXml()] "
. "Saved XML to $filepath; size: [" . filesize($filepath)
. "]", __FILE__, __LINE__);
return true;
}
return false;
}
/*}}}*/
/*{{{ public function generateReport() */
/**
* Generate report from the xml coverage data
* The preferred method for usage of this function is
* passing a stream of the XML data in. This is much more
* efficient and consumes less memory.
*
* @param $xmlUrl Url where XML data is available or string
* @param $stream=false Is the xml available as stream?
* @access public
*/
public function generateReport($xmlUrl, $stream=false) {
$this->logger->debug("XML Url: " . $xmlUrl, __FILE__, __LINE__);
$this->parseCoverageXml($xmlUrl, true);
$this->logger->debug("Coverage Data final: " . print_r($this->coverageData, true));
parent::generateReport();
}
/*}}}*/
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.