|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the LGPL. For more information please see |
|
17
|
|
|
* <http://phing.info>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
use Phing\Phing; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Various utility functions |
|
24
|
|
|
* |
|
25
|
|
|
* @author Michiel Rook <[email protected]> |
|
26
|
|
|
* @package phing.tasks.ext.phpunit |
|
27
|
|
|
* @since 2.1.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class PHPUnitUtil |
|
30
|
|
|
{ |
|
31
|
|
|
protected static $definedClasses = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Returns the package of a class as defined in the docblock of the class using {@package} |
|
35
|
|
|
* |
|
36
|
|
|
* @param string the name of the class |
|
37
|
|
|
* @return string the name of the package |
|
38
|
|
|
* @throws ReflectionException |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getPackageName($classname) |
|
41
|
|
|
{ |
|
42
|
|
|
$reflect = new ReflectionClass($classname); |
|
43
|
|
|
|
|
44
|
|
|
if (method_exists($reflect, 'getNamespaceName')) { |
|
45
|
|
|
$namespace = $reflect->getNamespaceName(); |
|
46
|
|
|
|
|
47
|
|
|
if ($namespace != '') { |
|
48
|
|
|
return $namespace; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (preg_match('/@package[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) { |
|
53
|
|
|
return $matches[1]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return "default"; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the subpackage of a class as defined in the docblock of the class |
|
61
|
|
|
* using {@subpackage} |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $classname the name of the class |
|
64
|
|
|
* |
|
65
|
|
|
* @return string|null the name of the subpackage |
|
66
|
|
|
* @throws ReflectionException |
|
67
|
|
|
* @author Benjamin Schultz <[email protected]> |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function getSubpackageName($classname) |
|
70
|
|
|
{ |
|
71
|
|
|
$reflect = new ReflectionClass($classname); |
|
72
|
|
|
|
|
73
|
|
|
if (preg_match('/@subpackage[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) { |
|
74
|
|
|
return $matches[1]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $filename |
|
82
|
|
|
* @param null $classpath |
|
|
|
|
|
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
* @internal param the $string filename |
|
85
|
|
|
* @internal param optional $Path classpath |
|
86
|
|
|
* @return array list of classes defined in the file |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public static function getDefinedClasses($filename, $classpath = null) |
|
89
|
|
|
{ |
|
90
|
4 |
|
$filename = realpath($filename); |
|
91
|
|
|
|
|
92
|
4 |
|
if (!file_exists($filename)) { |
|
93
|
|
|
throw new Exception("File '" . $filename . "' does not exist"); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
if (isset(self::$definedClasses[$filename])) { |
|
97
|
2 |
|
return self::$definedClasses[$filename]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
Phing::importFile($filename, $classpath); |
|
101
|
|
|
|
|
102
|
3 |
|
$declaredClasses = get_declared_classes(); |
|
103
|
|
|
|
|
104
|
3 |
|
foreach ($declaredClasses as $classname) { |
|
105
|
3 |
|
$reflect = new ReflectionClass($classname); |
|
106
|
|
|
|
|
107
|
3 |
|
self::$definedClasses[$reflect->getFilename()][] = $classname; |
|
108
|
|
|
|
|
109
|
3 |
|
if (is_array(self::$definedClasses[$reflect->getFilename()])) { |
|
110
|
3 |
|
self::$definedClasses[$reflect->getFilename()] = array_unique( |
|
111
|
3 |
|
self::$definedClasses[$reflect->getFilename()] |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
return self::$definedClasses[$filename] ?? []; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|