| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* $Id: PHPUnitUtil.php 325 2007-12-20 15:44:58Z hans $
|
|
|
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 |
/**
|
|
|
23 |
* Various utility functions
|
|
|
24 |
*
|
|
|
25 |
* @author Michiel Rook <michiel.rook@gmail.com>
|
|
|
26 |
* @version $Id: PHPUnitUtil.php 325 2007-12-20 15:44:58Z hans $
|
|
|
27 |
* @package phing.tasks.ext.phpunit
|
|
|
28 |
* @since 2.1.0
|
|
|
29 |
*/
|
|
|
30 |
class PHPUnitUtil
|
|
|
31 |
{
|
|
|
32 |
/**
|
|
|
33 |
* Installed PHPUnit major version
|
|
|
34 |
*/
|
|
|
35 |
public static $installedVersion = 2;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Installed PHPUnit minor version
|
|
|
39 |
*/
|
|
|
40 |
public static $installedMinorVersion = 0;
|
|
|
41 |
|
|
|
42 |
protected static $definedClasses = array();
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Returns the package of a class as defined in the docblock of the class using @package
|
|
|
46 |
*
|
|
|
47 |
* @param string the name of the class
|
|
|
48 |
* @return string the name of the package
|
|
|
49 |
*/
|
|
|
50 |
static function getPackageName($classname)
|
|
|
51 |
{
|
|
|
52 |
$reflect = new ReflectionClass($classname);
|
|
|
53 |
|
|
|
54 |
if (preg_match('/@package[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches))
|
|
|
55 |
{
|
|
|
56 |
return $matches[1];
|
|
|
57 |
}
|
|
|
58 |
else
|
|
|
59 |
{
|
|
|
60 |
return "default";
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Derives the classname from a filename.
|
|
|
66 |
* Assumes that there is only one class defined in that particular file, and that
|
|
|
67 |
* the naming follows the dot-path (Java) notation scheme.
|
|
|
68 |
*
|
|
|
69 |
* @param string the filename
|
|
|
70 |
* @return string the name fo the class
|
|
|
71 |
*/
|
|
|
72 |
static function getClassFromFileName($filename)
|
|
|
73 |
{
|
|
|
74 |
$filename = basename($filename);
|
|
|
75 |
|
|
|
76 |
$rpos = strrpos($filename, '.');
|
|
|
77 |
|
|
|
78 |
if ($rpos != -1)
|
|
|
79 |
{
|
|
|
80 |
$filename = substr($filename, 0, $rpos);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return $filename;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* @param string the filename
|
|
|
88 |
* @param Path optional classpath
|
|
|
89 |
* @return array list of classes defined in the file
|
|
|
90 |
*/
|
|
|
91 |
static function getDefinedClasses($filename, $classpath = NULL)
|
|
|
92 |
{
|
|
|
93 |
$filename = realpath($filename);
|
|
|
94 |
|
|
|
95 |
if (!file_exists($filename))
|
|
|
96 |
{
|
|
|
97 |
throw new Exception("File '" . $filename . "' does not exist");
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
if (isset(self::$definedClasses[$filename]))
|
|
|
101 |
{
|
|
|
102 |
return self::$definedClasses[$filename];
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
Phing::__import($filename, $classpath);
|
|
|
106 |
|
|
|
107 |
$declaredClasses = get_declared_classes();
|
|
|
108 |
|
|
|
109 |
foreach ($declaredClasses as $classname)
|
|
|
110 |
{
|
|
|
111 |
$reflect = new ReflectionClass($classname);
|
|
|
112 |
|
|
|
113 |
self::$definedClasses[$reflect->getFilename()][] = $classname;
|
|
|
114 |
|
|
|
115 |
if (is_array(self::$definedClasses[$reflect->getFilename()]))
|
|
|
116 |
{
|
|
|
117 |
self::$definedClasses[$reflect->getFilename()] = array_unique(self::$definedClasses[$reflect->getFilename()]);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
if (isset(self::$definedClasses[$filename]))
|
|
|
122 |
{
|
|
|
123 |
return self::$definedClasses[$filename];
|
|
|
124 |
}
|
|
|
125 |
else
|
|
|
126 |
{
|
|
|
127 |
return array();
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|