Skip to content

Commit 3748db2

Browse files
committed
Added @Covers support for files (anything with a . in it).
1 parent aef6c7d commit 3748db2

File tree

8 files changed

+227
-0
lines changed

8 files changed

+227
-0
lines changed

PHP/CodeCoverage/Autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function php_codecoverage_autoload($class = NULL) {
5858
'php_codecoverage_driver_xdebug' => '/CodeCoverage/Driver/Xdebug.php',
5959
'php_codecoverage_exception' => '/CodeCoverage/Exception.php',
6060
'php_codecoverage_filter' => '/CodeCoverage/Filter.php',
61+
'php_codecoverage_reflectionfile' => '/CodeCoverage/ReflectionFile.php',
6162
'php_codecoverage_report_clover' => '/CodeCoverage/Report/Clover.php',
6263
'php_codecoverage_report_factory' => '/CodeCoverage/Report/Factory.php',
6364
'php_codecoverage_report_html' => '/CodeCoverage/Report/HTML.php',

PHP/CodeCoverage/ReflectionFile.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
/**
3+
* PHP_CodeCoverage
4+
*
5+
* Copyright (c) 2009-2010, 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 David Harkness <[email protected]>
40+
* @copyright 2009-2010 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.0.0
44+
*/
45+
46+
/**
47+
* Represents a file that contains PHP code.
48+
*
49+
* @category PHP
50+
* @package CodeCoverage
51+
* @author David Harkness <[email protected]>
52+
* @copyright 2009-2010 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.0.0
57+
*/
58+
class PHP_CodeCoverage_ReflectionFile implements Reflector
59+
{
60+
/**
61+
* Path to file.
62+
*
63+
* @var string
64+
*/
65+
private $file;
66+
67+
/**
68+
* Number of lines in the file or null if not yet counted.
69+
*
70+
* @var int|null
71+
*/
72+
private $numLines = null;
73+
74+
/**
75+
* Stores the path to the file to be reflected.
76+
*
77+
* @param string $file
78+
* @throws PHPUnit_Framework_Exception if the path isn't a valid file
79+
*/
80+
public function __construct($file)
81+
{
82+
if (!is_file($file)) {
83+
throw new InvalidArgumentException($file . ' does not exist');
84+
}
85+
86+
$this->file = $file;
87+
}
88+
89+
/**
90+
* Returns the path to the file.
91+
*
92+
* @return string
93+
*/
94+
public function getName()
95+
{
96+
return $this->file;
97+
}
98+
99+
/**
100+
* Returns the base name of the file.
101+
*
102+
* @return string
103+
*/
104+
public function getShortName()
105+
{
106+
return basename($this->file);
107+
}
108+
109+
110+
/**
111+
* Returns the path to the file.
112+
*
113+
* @return string
114+
*/
115+
public function getFileName()
116+
{
117+
return $this->file;
118+
}
119+
120+
/**
121+
* Returns the first line of the file which is always 1.
122+
*
123+
* @return int
124+
*/
125+
public function getStartLine()
126+
{
127+
return 1;
128+
}
129+
130+
/**
131+
* Returns the last line of the file.
132+
*
133+
* If the number of lines hasn't been counted yet, it is counted
134+
* and stored for future calls.
135+
*
136+
* @return int
137+
*/
138+
public function getEndLine()
139+
{
140+
if (is_null($this->numLines)) {
141+
$this->numLines = count(file($this->file));
142+
}
143+
return $this->numLines;
144+
}
145+
146+
/**
147+
* Returns a simple string representation of this reflector.
148+
*
149+
* @return string contains the path to the file
150+
*/
151+
public function __toString()
152+
{
153+
return 'File [ ' . $this->file . ' ]';
154+
}
155+
156+
157+
/**
158+
* Exports the given reflector.
159+
*
160+
* As this is invalid for files, this method does nothing.
161+
*
162+
* @param PHPUnit_Util_ReflectionFile $reflector
163+
*/
164+
public static function export($reflector)
165+
{
166+
/* cannot export files */
167+
}
168+
}

PHP/CodeCoverage/Util.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,18 @@ trait_exists($className)) &&
390390
}
391391
}
392392
}
393+
} elseif (strpos($coveredElement, '.') !== FALSE) {
394+
$file = stream_resolve_include_path($coveredElement);
395+
if (!$file) {
396+
throw new PHP_CodeCoverage_Exception(
397+
sprintf(
398+
'Trying to @cover not existing file "%s".',
399+
$coveredElement
400+
)
401+
);
402+
}
403+
404+
$codeToCoverList[] = new PHP_CodeCoverage_ReflectionFile($file);
393405
} else {
394406
$extended = FALSE;
395407

Tests/PHP/CodeCoverage/FilterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected function setUp()
7979
TEST_FILES_PATH . 'BankAccountTest.php',
8080
TEST_FILES_PATH . 'CoverageClassExtendedTest.php',
8181
TEST_FILES_PATH . 'CoverageClassTest.php',
82+
TEST_FILES_PATH . 'CoverageFileTest.php',
8283
TEST_FILES_PATH . 'CoverageFunctionTest.php',
8384
TEST_FILES_PATH . 'CoverageMethodTest.php',
8485
TEST_FILES_PATH . 'CoverageNoneTest.php',
@@ -89,6 +90,7 @@ protected function setUp()
8990
TEST_FILES_PATH . 'CoverageProtectedTest.php',
9091
TEST_FILES_PATH . 'CoveragePublicTest.php',
9192
TEST_FILES_PATH . 'CoveredClass.php',
93+
TEST_FILES_PATH . 'CoveredFile.php',
9294
TEST_FILES_PATH . 'CoveredFunction.php',
9395
TEST_FILES_PATH . 'NamespaceCoverageClassExtendedTest.php',
9496
TEST_FILES_PATH . 'NamespaceCoverageClassTest.php',

Tests/PHP/CodeCoverage/UtilTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
require_once TEST_FILES_PATH . 'CoverageClassExtendedTest.php';
5858
require_once TEST_FILES_PATH . 'CoverageClassTest.php';
59+
require_once TEST_FILES_PATH . 'CoverageFileTest.php';
5960
require_once TEST_FILES_PATH . 'CoverageFunctionTest.php';
6061
require_once TEST_FILES_PATH . 'CoverageMethodTest.php';
6162
require_once TEST_FILES_PATH . 'CoverageNoneTest.php';
@@ -136,6 +137,12 @@ public function testGetLinesToBeCovered($test, $lines)
136137
);
137138
}
138139

140+
else if ($test === 'CoverageFileTest') {
141+
$expected = array(
142+
TEST_FILES_PATH . 'CoveredFile.php' => $lines
143+
);
144+
}
145+
139146
else {
140147
$expected = array(TEST_FILES_PATH . 'CoveredClass.php' => $lines);
141148
}
@@ -184,6 +191,18 @@ public function testGetLinesToBeCovered4()
184191
);
185192
}
186193

194+
/**
195+
* @covers PHP_CodeCoverage_Util::getLinesToBeCovered
196+
* @covers PHP_CodeCoverage_Util::resolveCoversToReflectionObjects
197+
* @expectedException PHP_CodeCoverage_Exception
198+
*/
199+
public function testGetLinesToBeCovered5()
200+
{
201+
PHP_CodeCoverage_Util::getLinesToBeCovered(
202+
'NotExistingCoveredElementTest', 'testFour'
203+
);
204+
}
205+
187206
/**
188207
* @covers PHP_CodeCoverage_Util::getLinesToBeIgnored
189208
*/
@@ -334,6 +353,10 @@ public function getLinesToBeCoveredProvider()
334353
'CoverageFunctionTest',
335354
range(2, 4)
336355
),
356+
array(
357+
'CoverageFileTest',
358+
range(1, 3)
359+
),
337360
array(
338361
'NamespaceCoverageClassExtendedTest',
339362
array_merge(range(21, 38), range(4, 19))

Tests/_files/CoverageFileTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
class CoverageFileTest extends PHPUnit_Framework_TestCase
3+
{
4+
/**
5+
* @covers Tests/_files/CoveredFile.php
6+
*/
7+
public function testSomething()
8+
{
9+
globalFunction();
10+
}
11+
}

Tests/_files/CoveredFile.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hello,
2+
<?php echo $name; ?>
3+
.

Tests/_files/NotExistingCoveredElementTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ public function testTwo()
2121
public function testThree()
2222
{
2323
}
24+
25+
/**
26+
* @covers NotExistingFile.phtml
27+
*/
28+
public function testFour()
29+
{
30+
}
2431
}

0 commit comments

Comments
 (0)