Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * $Id: CoverageSetupTask.php 426 2008-10-28 19:29:49Z mrook $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://phing.info>.
20
 */
21
 
22
require_once 'phing/Task.php';
23
require_once 'phing/system/io/PhingFile.php';
24
require_once 'phing/system/io/Writer.php';
25
require_once 'phing/system/util/Properties.php';
26
require_once 'phing/tasks/ext/coverage/CoverageMerger.php';
27
 
28
/**
29
 * Initializes a code coverage database
30
 *
31
 * @author Michiel Rook <michiel.rook@gmail.com>
32
 * @version $Id: CoverageSetupTask.php 426 2008-10-28 19:29:49Z mrook $
33
 * @package phing.tasks.ext.coverage
34
 * @since 2.1.0
35
 */
36
class CoverageSetupTask extends Task
37
{
38
	/** the list of filesets containing the .php filename rules */
39
	private $filesets = array();
40
 
41
	/** the filename of the coverage database */
42
	private $database = "coverage.db";
43
 
44
	/** the classpath to use (optional) */
45
	private $classpath = NULL;
46
 
47
	/**
48
	 * Add a new fileset containing the .php files to process
49
	 *
50
	 * @param FileSet the new fileset containing .php files
51
	 */
52
	function addFileSet(FileSet $fileset)
53
	{
54
		$this->filesets[] = $fileset;
55
	}
56
 
57
	/**
58
	 * Sets the filename of the coverage database to use
59
	 *
60
	 * @param string the filename of the database
61
	 */
62
	function setDatabase($database)
63
	{
64
		$this->database = $database;
65
	}
66
 
67
	function setClasspath(Path $classpath)
68
	{
69
		if ($this->classpath === null)
70
		{
71
			$this->classpath = $classpath;
72
		}
73
		else
74
		{
75
			$this->classpath->append($classpath);
76
		}
77
	}
78
 
79
	function createClasspath()
80
	{
81
		$this->classpath = new Path();
82
		return $this->classpath;
83
	}
84
 
85
	/**
86
	 * Iterate over all filesets and return the filename of all files.
87
	 *
88
	 * @return array an array of (basedir, filenames) pairs
89
	 */
90
	private function getFilenames()
91
	{
92
		$files = array();
93
 
94
		foreach ($this->filesets as $fileset)
95
		{
96
			$ds = $fileset->getDirectoryScanner($this->project);
97
			$ds->scan();
98
 
99
			$includedFiles = $ds->getIncludedFiles();
100
 
101
			foreach ($includedFiles as $file)
102
			{
103
				$fs = new PhingFile(realpath($ds->getBaseDir()), $file);
104
 
105
				$files[] = array('key' => strtolower($fs->getAbsolutePath()), 'fullname' => $fs->getAbsolutePath());
106
			}
107
		}
108
 
109
		return $files;
110
	}
111
 
112
	function init()
113
	{
114
		if (!extension_loaded('xdebug'))
115
		{
116
			throw new Exception("CoverageSetupTask depends on Xdebug being installed.");
117
		}
118
	}
119
 
120
	function main()
121
	{
122
		$files = $this->getFilenames();
123
 
124
		$this->log("Setting up coverage database for " . count($files) . " files");
125
 
126
		$props = new Properties();
127
 
128
		foreach ($files as $file)
129
		{
130
			$fullname = $file['fullname'];
131
			$filename = $file['key'];
132
 
133
			$props->setProperty($filename, serialize(array('fullname' => $fullname, 'coverage' => array())));
134
		}
135
 
136
		$dbfile = new PhingFile($this->database);
137
 
138
		$props->store($dbfile);
139
 
140
		$this->project->setProperty('coverage.database', $dbfile->getAbsolutePath());
141
 
142
		foreach ($files as $file)
143
		{
144
			$fullname = $file['fullname'];
145
 
146
			xdebug_start_code_coverage(XDEBUG_CC_DEAD_CODE | XDEBUG_CC_UNUSED);
147
 
148
			Phing::__import($fullname, $this->classpath);
149
 
150
			$coverage = xdebug_get_code_coverage();
151
 
152
			xdebug_stop_code_coverage();
153
 
154
			CoverageMerger::merge($this->project, array($coverage));
155
		}
156
	}
157
}
158