Skip to content

Commit 5811a28

Browse files
Initial work on making code of the HTML reporting functionality reusable (and testable).
1 parent 014a116 commit 5811a28

File tree

7 files changed

+1294
-8
lines changed

7 files changed

+1294
-8
lines changed

PHP/CodeCoverage/Autoload.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,24 @@ function php_codecoverage_autoload($class) {
5353

5454
if ($classes === NULL) {
5555
$classes = array(
56-
'php_codecoverage' => '/CodeCoverage.php',
5756
'php_codecoverage_driver_xdebug' => '/CodeCoverage/Driver/Xdebug.php',
58-
'php_codecoverage_util' => '/CodeCoverage/Util.php',
59-
'php_codecoverage_driver' => '/CodeCoverage/Driver.php',
60-
'php_codecoverage_filter' => '/CodeCoverage/Filter.php',
6157
'php_codecoverage_textui_command' => '/CodeCoverage/TextUI/Command.php',
62-
'php_codecoverage_report_php' => '/CodeCoverage/Report/PHP.php',
63-
'php_codecoverage_report_html' => '/CodeCoverage/Report/HTML.php',
6458
'php_codecoverage_report_clover' => '/CodeCoverage/Report/Clover.php',
59+
'php_codecoverage_report_factory' => '/CodeCoverage/Report/Factory.php',
60+
'php_codecoverage_report_php' => '/CodeCoverage/Report/PHP.php',
6561
'php_codecoverage_report_html_node' => '/CodeCoverage/Report/HTML/Node.php',
66-
'php_codecoverage_report_html_node_iterator' => '/CodeCoverage/Report/HTML/Node/Iterator.php',
6762
'php_codecoverage_report_html_node_file' => '/CodeCoverage/Report/HTML/Node/File.php',
68-
'php_codecoverage_report_html_node_directory' => '/CodeCoverage/Report/HTML/Node/Directory.php'
63+
'php_codecoverage_report_html_node_iterator' => '/CodeCoverage/Report/HTML/Node/Iterator.php',
64+
'php_codecoverage_report_html_node_directory' => '/CodeCoverage/Report/HTML/Node/Directory.php',
65+
'php_codecoverage_report_html' => '/CodeCoverage/Report/HTML.php',
66+
'php_codecoverage_report_node' => '/CodeCoverage/Report/Node.php',
67+
'php_codecoverage_report_node_file' => '/CodeCoverage/Report/Node/File.php',
68+
'php_codecoverage_report_node_iterator' => '/CodeCoverage/Report/Node/Iterator.php',
69+
'php_codecoverage_report_node_directory' => '/CodeCoverage/Report/Node/Directory.php',
70+
'php_codecoverage_util' => '/CodeCoverage/Util.php',
71+
'php_codecoverage_filter' => '/CodeCoverage/Filter.php',
72+
'php_codecoverage_driver' => '/CodeCoverage/Driver.php',
73+
'php_codecoverage' => '/CodeCoverage.php'
6974
);
7075

7176
$path = dirname(dirname(__FILE__));

PHP/CodeCoverage/Report/Factory.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* PHP_CodeCoverage
4+
*
5+
* Copyright (c) 2009-2011, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @category PHP
38+
* @package CodeCoverage
39+
* @author Sebastian Bergmann <[email protected]>
40+
* @copyright 2009-2011 Sebastian Bergmann <[email protected]>
41+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42+
* @link http://github.com/sebastianbergmann/php-code-coverage
43+
* @since File available since Release 1.1.0
44+
*/
45+
46+
/**
47+
* Factory for PHP_CodeCoverage_Report_Node_* object graphs.
48+
*
49+
* @category PHP
50+
* @package CodeCoverage
51+
* @author Sebastian Bergmann <[email protected]>
52+
* @copyright 2009-2011 Sebastian Bergmann <[email protected]>
53+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
54+
* @version Release: @package_version@
55+
* @link http://github.com/sebastianbergmann/php-code-coverage
56+
* @since Class available since Release 1.1.0
57+
*/
58+
class PHP_CodeCoverage_Report_Factory
59+
{
60+
/**
61+
* @param PHP_CodeCoverage $coverage
62+
*/
63+
public static function create(PHP_CodeCoverage $coverage)
64+
{
65+
$files = $coverage->getData();
66+
$commonPath = PHP_CodeCoverage_Util::reducePaths($files);
67+
$root = new PHP_CodeCoverage_Report_Node_Directory(
68+
$commonPath, NULL
69+
);
70+
71+
self::addItems(
72+
$root,
73+
PHP_CodeCoverage_Util::buildDirectoryStructure($files),
74+
$coverage->getTests(),
75+
$commonPath
76+
);
77+
78+
return $root;
79+
}
80+
81+
/**
82+
* @param PHP_CodeCoverage_Report_Node_Directory $root
83+
* @param array $items
84+
* @param array $tests
85+
* @param string $commonPath
86+
*/
87+
protected static function addItems(PHP_CodeCoverage_Report_Node_Directory $root, array $items, array $tests, $commonPath)
88+
{
89+
foreach ($items as $key => $value) {
90+
if (substr($key, -2) == '/f') {
91+
$key = substr($key, 0, -2);
92+
$root->addFile($key, $commonPath . $key, $value, $tests);
93+
} else {
94+
$child = $root->addDirectory($key, $commonPath . $key);
95+
self::addItems($child, $value, $tests, $commonPath);
96+
}
97+
}
98+
}
99+
}

PHP/CodeCoverage/Report/Node.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
/**
3+
* PHP_CodeCoverage
4+
*
5+
* Copyright (c) 2009-2011, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @category PHP
38+
* @package CodeCoverage
39+
* @author Sebastian Bergmann <[email protected]>
40+
* @copyright 2009-2011 Sebastian Bergmann <[email protected]>
41+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42+
* @link http://github.com/sebastianbergmann/php-code-coverage
43+
* @since File available since Release 1.1.0
44+
*/
45+
46+
/**
47+
* Base class for nodes in the code coverage information tree.
48+
*
49+
* @category PHP
50+
* @package CodeCoverage
51+
* @author Sebastian Bergmann <[email protected]>
52+
* @copyright 2009-2011 Sebastian Bergmann <[email protected]>
53+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
54+
* @version Release: @package_version@
55+
* @link http://github.com/sebastianbergmann/php-code-coverage
56+
* @since Class available since Release 1.1.0
57+
*/
58+
abstract class PHP_CodeCoverage_Report_Node
59+
{
60+
/**
61+
* @var string
62+
*/
63+
protected $name;
64+
65+
/**
66+
* @var string
67+
*/
68+
protected $path;
69+
70+
/**
71+
* @var PHP_CodeCoverage_Report_Node
72+
*/
73+
protected $parent;
74+
75+
/**
76+
* Constructor.
77+
*
78+
* @param string $name
79+
* @param string $path
80+
* @param PHP_CodeCoverage_Report_Node $parent
81+
*/
82+
public function __construct($name, $path, PHP_CodeCoverage_Report_Node $parent = NULL)
83+
{
84+
$this->name = $name;
85+
$this->path = $path;
86+
$this->parent = $parent;
87+
}
88+
89+
/**
90+
* @return string
91+
*/
92+
public function getName()
93+
{
94+
return $this->name;
95+
}
96+
97+
/**
98+
* @return string
99+
*/
100+
public function getPath()
101+
{
102+
return $this->path;
103+
}
104+
105+
/**
106+
* @return PHP_CodeCoverage_Report_Node
107+
*/
108+
public function getParent()
109+
{
110+
return $this->parent;
111+
}
112+
113+
/**
114+
* Returns the percentage of classes that has been tested.
115+
*
116+
* @return integer
117+
*/
118+
public function getTestedClassesPercent()
119+
{
120+
return PHP_CodeCoverage_Util::percent(
121+
$this->getNumTestedClasses(),
122+
$this->getNumClasses(),
123+
TRUE
124+
);
125+
}
126+
127+
/**
128+
* Returns the percentage of methods that has been tested.
129+
*
130+
* @return integer
131+
*/
132+
public function getTestedMethodsPercent()
133+
{
134+
return PHP_CodeCoverage_Util::percent(
135+
$this->getNumTestedMethods(),
136+
$this->getNumMethods(),
137+
TRUE
138+
);
139+
}
140+
141+
/**
142+
* Returns the percentage of executed lines.
143+
*
144+
* @return integer
145+
*/
146+
public function getLineExecutedPercent()
147+
{
148+
return PHP_CodeCoverage_Util::percent(
149+
$this->getNumExecutedLines(),
150+
$this->getNumExecutableLines(),
151+
TRUE
152+
);
153+
}
154+
155+
/**
156+
* Returns the classes of this node.
157+
*
158+
* @return array
159+
*/
160+
abstract public function getClasses();
161+
162+
/**
163+
* Returns the number of executable lines.
164+
*
165+
* @return integer
166+
*/
167+
abstract public function getNumExecutableLines();
168+
169+
/**
170+
* Returns the number of executed lines.
171+
*
172+
* @return integer
173+
*/
174+
abstract public function getNumExecutedLines();
175+
176+
/**
177+
* Returns the number of classes.
178+
*
179+
* @return integer
180+
*/
181+
abstract public function getNumClasses();
182+
183+
/**
184+
* Returns the number of tested classes.
185+
*
186+
* @return integer
187+
*/
188+
abstract public function getNumTestedClasses();
189+
190+
/**
191+
* Returns the number of methods.
192+
*
193+
* @return integer
194+
*/
195+
abstract public function getNumMethods();
196+
197+
/**
198+
* Returns the number of tested methods.
199+
*
200+
* @return integer
201+
*/
202+
abstract public function getNumTestedMethods();
203+
}

0 commit comments

Comments
 (0)