| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
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 Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
40 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
41 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
42 |
* @link http://www.phpunit.de/
|
|
|
43 |
* @since File available since Release 3.1.0
|
|
|
44 |
*/
|
|
|
45 |
|
|
|
46 |
if (!defined('T_NAMESPACE')) {
|
|
|
47 |
define('T_NAMESPACE', 377);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
51 |
require_once 'PHPUnit/Util/InvalidArgumentHelper.php';
|
|
|
52 |
|
|
|
53 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Class helpers.
|
|
|
57 |
*
|
|
|
58 |
* @category Testing
|
|
|
59 |
* @package PHPUnit
|
|
|
60 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
61 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
62 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
63 |
* @version Release: 3.4.15
|
|
|
64 |
* @link http://www.phpunit.de/
|
|
|
65 |
* @since Class available since Release 3.1.0
|
|
|
66 |
*/
|
|
|
67 |
class PHPUnit_Util_Class
|
|
|
68 |
{
|
|
|
69 |
protected static $buffer = array();
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Starts the collection of loaded classes.
|
|
|
73 |
*
|
|
|
74 |
*/
|
|
|
75 |
public static function collectStart()
|
|
|
76 |
{
|
|
|
77 |
self::$buffer = get_declared_classes();
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Stops the collection of loaded classes and
|
|
|
82 |
* returns the names of the loaded classes.
|
|
|
83 |
*
|
|
|
84 |
* @return array
|
|
|
85 |
*/
|
|
|
86 |
public static function collectEnd()
|
|
|
87 |
{
|
|
|
88 |
return array_values(
|
|
|
89 |
array_diff(get_declared_classes(), self::$buffer)
|
|
|
90 |
);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Stops the collection of loaded classes and
|
|
|
95 |
* returns the names of the files that declare the loaded classes.
|
|
|
96 |
*
|
|
|
97 |
* @return array
|
|
|
98 |
*/
|
|
|
99 |
public static function collectEndAsFiles()
|
|
|
100 |
{
|
|
|
101 |
$result = self::collectEnd();
|
|
|
102 |
$count = count($result);
|
|
|
103 |
|
|
|
104 |
for ($i = 0; $i < $count; $i++) {
|
|
|
105 |
$class = new ReflectionClass($result[$i]);
|
|
|
106 |
|
|
|
107 |
if ($class->isUserDefined()) {
|
|
|
108 |
$file = $class->getFileName();
|
|
|
109 |
|
|
|
110 |
if (file_exists($file)) {
|
|
|
111 |
$result[$i] = $file;
|
|
|
112 |
} else {
|
|
|
113 |
unset($result[$i]);
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return $result;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Returns the class hierarchy for a given class.
|
|
|
123 |
*
|
|
|
124 |
* @param string $className
|
|
|
125 |
* @param boolean $asReflectionObjects
|
|
|
126 |
* @return array
|
|
|
127 |
*/
|
|
|
128 |
public static function getHierarchy($className, $asReflectionObjects = FALSE)
|
|
|
129 |
{
|
|
|
130 |
if ($asReflectionObjects) {
|
|
|
131 |
$classes = array(new ReflectionClass($className));
|
|
|
132 |
} else {
|
|
|
133 |
$classes = array($className);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
$done = FALSE;
|
|
|
137 |
|
|
|
138 |
while (!$done) {
|
|
|
139 |
if ($asReflectionObjects) {
|
|
|
140 |
$class = new ReflectionClass(
|
|
|
141 |
$classes[count($classes)-1]->getName()
|
|
|
142 |
);
|
|
|
143 |
} else {
|
|
|
144 |
$class = new ReflectionClass($classes[count($classes)-1]);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
$parent = $class->getParentClass();
|
|
|
148 |
|
|
|
149 |
if ($parent !== FALSE) {
|
|
|
150 |
if ($asReflectionObjects) {
|
|
|
151 |
$classes[] = $parent;
|
|
|
152 |
} else {
|
|
|
153 |
$classes[] = $parent->getName();
|
|
|
154 |
}
|
|
|
155 |
} else {
|
|
|
156 |
$done = TRUE;
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
return $classes;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Returns the parameters of a function or method.
|
|
|
165 |
*
|
|
|
166 |
* @param ReflectionFunction|ReflectionMethod $method
|
|
|
167 |
* @return string
|
|
|
168 |
* @since Method available since Release 3.2.0
|
|
|
169 |
*/
|
|
|
170 |
public static function getMethodParameters($method)
|
|
|
171 |
{
|
|
|
172 |
$parameters = array();
|
|
|
173 |
|
|
|
174 |
foreach ($method->getParameters() as $i => $parameter) {
|
|
|
175 |
$name = '$' . $parameter->getName();
|
|
|
176 |
|
|
|
177 |
if ($name === '$') {
|
|
|
178 |
$name .= 'arg' . $i;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
$typeHint = '';
|
|
|
182 |
|
|
|
183 |
if ($parameter->isArray()) {
|
|
|
184 |
$typeHint = 'array ';
|
|
|
185 |
} else {
|
|
|
186 |
try {
|
|
|
187 |
$class = $parameter->getClass();
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
catch (ReflectionException $e) {
|
|
|
191 |
$class = FALSE;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
if ($class) {
|
|
|
195 |
$typeHint = $class->getName() . ' ';
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
$default = '';
|
|
|
200 |
|
|
|
201 |
if ($parameter->isDefaultValueAvailable()) {
|
|
|
202 |
$value = $parameter->getDefaultValue();
|
|
|
203 |
$default = ' = ' . var_export($value, TRUE);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
else if ($parameter->isOptional()) {
|
|
|
207 |
$default = ' = null';
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
$ref = '';
|
|
|
211 |
|
|
|
212 |
if ($parameter->isPassedByReference()) {
|
|
|
213 |
$ref = '&';
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
$parameters[] = $typeHint . $ref . $name . $default;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
return join(', ', $parameters);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Returns the sourcecode of a user-defined class.
|
|
|
224 |
*
|
|
|
225 |
* @param string $className
|
|
|
226 |
* @param string $methodName
|
|
|
227 |
* @return mixed
|
|
|
228 |
*/
|
|
|
229 |
public static function getMethodSource($className, $methodName)
|
|
|
230 |
{
|
|
|
231 |
if ($className != 'global') {
|
|
|
232 |
$function = new ReflectionMethod($className, $methodName);
|
|
|
233 |
} else {
|
|
|
234 |
$function = new ReflectionFunction($methodName);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
$filename = $function->getFileName();
|
|
|
238 |
|
|
|
239 |
if (file_exists($filename)) {
|
|
|
240 |
$file = file($filename);
|
|
|
241 |
$result = '';
|
|
|
242 |
$start = $function->getStartLine() - 1;
|
|
|
243 |
$end = $function->getEndLine() - 1;
|
|
|
244 |
|
|
|
245 |
for ($line = $start; $line <= $end; $line++) {
|
|
|
246 |
$result .= $file[$line];
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
return $result;
|
|
|
250 |
} else {
|
|
|
251 |
return FALSE;
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Returns the package information of a user-defined class.
|
|
|
257 |
*
|
|
|
258 |
* @param string $className
|
|
|
259 |
* @param string $docComment
|
|
|
260 |
* @return array
|
|
|
261 |
*/
|
|
|
262 |
public static function getPackageInformation($className, $docComment)
|
|
|
263 |
{
|
|
|
264 |
$result = array(
|
|
|
265 |
'namespace' => '',
|
|
|
266 |
'fullPackage' => '',
|
|
|
267 |
'category' => '',
|
|
|
268 |
'package' => '',
|
|
|
269 |
'subpackage' => ''
|
|
|
270 |
);
|
|
|
271 |
|
|
|
272 |
if (strpos($className, '\\') !== FALSE) {
|
|
|
273 |
$result['namespace'] = self::arrayToName(
|
|
|
274 |
explode('\\', $className)
|
|
|
275 |
);
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
if (preg_match('/@category[\s]+([\.\w]+)/', $docComment, $matches)) {
|
|
|
279 |
$result['category'] = $matches[1];
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
if (preg_match('/@package[\s]+([\.\w]+)/', $docComment, $matches)) {
|
|
|
283 |
$result['package'] = $matches[1];
|
|
|
284 |
$result['fullPackage'] = $matches[1];
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
if (preg_match('/@subpackage[\s]+([\.\w]+)/', $docComment, $matches)) {
|
|
|
288 |
$result['subpackage'] = $matches[1];
|
|
|
289 |
$result['fullPackage'] .= '.' . $matches[1];
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
if (empty($result['fullPackage'])) {
|
|
|
293 |
$result['fullPackage'] = self::arrayToName(
|
|
|
294 |
explode('_', str_replace('\\', '_', $className)), '.'
|
|
|
295 |
);
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
return $result;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* Returns the value of a static attribute.
|
|
|
303 |
* This also works for attributes that are declared protected or private.
|
|
|
304 |
*
|
|
|
305 |
* @param string $className
|
|
|
306 |
* @param string $attributeName
|
|
|
307 |
* @return mixed
|
|
|
308 |
* @throws InvalidArgumentException
|
|
|
309 |
* @since Method available since Release 3.4.0
|
|
|
310 |
*/
|
|
|
311 |
public static function getStaticAttribute($className, $attributeName)
|
|
|
312 |
{
|
|
|
313 |
if (!is_string($className)) {
|
|
|
314 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
if (!class_exists($className)) {
|
|
|
318 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name');
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
if (!is_string($attributeName)) {
|
|
|
322 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
$class = new ReflectionClass($className);
|
|
|
326 |
|
|
|
327 |
while ($class) {
|
|
|
328 |
$attributes = $class->getStaticProperties();
|
|
|
329 |
|
|
|
330 |
$key = $attributeName;
|
|
|
331 |
|
|
|
332 |
if (isset($attributes[$key])) {
|
|
|
333 |
return $attributes[$key];
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
$key = "\0*\0" . $attributeName;
|
|
|
337 |
|
|
|
338 |
if (isset($attributes[$key])) {
|
|
|
339 |
return $attributes[$key];
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
$key = "\0" . $class->getName() . "\0" . $attributeName;
|
|
|
343 |
|
|
|
344 |
if (isset($attributes[$key])) {
|
|
|
345 |
return $attributes[$key];
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
$class = $class->getParentClass();
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
throw new PHPUnit_Framework_Exception(
|
|
|
352 |
sprintf(
|
|
|
353 |
'Attribute "%s" not found in class.',
|
|
|
354 |
|
|
|
355 |
$attributeName
|
|
|
356 |
)
|
|
|
357 |
);
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
/**
|
|
|
361 |
* Returns the value of an object's attribute.
|
|
|
362 |
* This also works for attributes that are declared protected or private.
|
|
|
363 |
*
|
|
|
364 |
* @param object $object
|
|
|
365 |
* @param string $attributeName
|
|
|
366 |
* @return mixed
|
|
|
367 |
* @throws InvalidArgumentException
|
|
|
368 |
* @since Method available since Release 3.4.0
|
|
|
369 |
*/
|
|
|
370 |
public static function getObjectAttribute($object, $attributeName)
|
|
|
371 |
{
|
|
|
372 |
if (!is_object($object)) {
|
|
|
373 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'object');
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
if (!is_string($attributeName)) {
|
|
|
377 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
PHPUnit_Framework_Assert::assertObjectHasAttribute(
|
|
|
381 |
$attributeName, $object
|
|
|
382 |
);
|
|
|
383 |
|
|
|
384 |
try {
|
|
|
385 |
$attribute = new ReflectionProperty($object, $attributeName);
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
catch (ReflectionException $e) {
|
|
|
389 |
// Workaround for http://bugs.php.net/46064
|
|
|
390 |
if (version_compare(PHP_VERSION, '5.2.7', '<')) {
|
|
|
391 |
$reflector = new ReflectionObject($object);
|
|
|
392 |
$attributes = $reflector->getProperties();
|
|
|
393 |
|
|
|
394 |
foreach ($attributes as $_attribute) {
|
|
|
395 |
if ($_attribute->getName() == $attributeName) {
|
|
|
396 |
$attribute = $_attribute;
|
|
|
397 |
break;
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
$reflector = new ReflectionObject($object);
|
|
|
403 |
|
|
|
404 |
while ($reflector = $reflector->getParentClass()) {
|
|
|
405 |
try {
|
|
|
406 |
$attribute = $reflector->getProperty($attributeName);
|
|
|
407 |
break;
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
catch(ReflectionException $e) {
|
|
|
411 |
}
|
|
|
412 |
}
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
if ($attribute->isPublic()) {
|
|
|
416 |
return $object->$attributeName;
|
|
|
417 |
} else {
|
|
|
418 |
$array = (array)$object;
|
|
|
419 |
$protectedName = "\0*\0" . $attributeName;
|
|
|
420 |
|
|
|
421 |
if (array_key_exists($protectedName, $array)) {
|
|
|
422 |
return $array[$protectedName];
|
|
|
423 |
} else {
|
|
|
424 |
$classes = self::getHierarchy(get_class($object));
|
|
|
425 |
|
|
|
426 |
foreach ($classes as $class) {
|
|
|
427 |
$privateName = sprintf(
|
|
|
428 |
"\0%s\0%s",
|
|
|
429 |
|
|
|
430 |
$class,
|
|
|
431 |
$attributeName
|
|
|
432 |
);
|
|
|
433 |
|
|
|
434 |
if (array_key_exists($privateName, $array)) {
|
|
|
435 |
return $array[$privateName];
|
|
|
436 |
}
|
|
|
437 |
}
|
|
|
438 |
}
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
throw new PHPUnit_Framework_Exception(
|
|
|
442 |
sprintf(
|
|
|
443 |
'Attribute "%s" not found in object.',
|
|
|
444 |
|
|
|
445 |
$attributeName
|
|
|
446 |
)
|
|
|
447 |
);
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
/**
|
|
|
451 |
*
|
|
|
452 |
*
|
|
|
453 |
* @param string $className
|
|
|
454 |
* @return array
|
|
|
455 |
* @since Method available since Release 3.4.0
|
|
|
456 |
*/
|
|
|
457 |
public static function parseFullyQualifiedClassName($className)
|
|
|
458 |
{
|
|
|
459 |
$result = array(
|
|
|
460 |
'namespace' => '',
|
|
|
461 |
'className' => $className,
|
|
|
462 |
'fullyQualifiedClassName' => $className
|
|
|
463 |
);
|
|
|
464 |
|
|
|
465 |
if (strpos($className, '\\') !== FALSE) {
|
|
|
466 |
$tmp = explode('\\', $className);
|
|
|
467 |
$result['className'] = $tmp[count($tmp)-1];
|
|
|
468 |
$result['namespace'] = self::arrayToName($tmp);
|
|
|
469 |
}
|
|
|
470 |
|
|
|
471 |
return $result;
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
/**
|
|
|
475 |
* Returns the package information of a user-defined class.
|
|
|
476 |
*
|
|
|
477 |
* @param array $parts
|
|
|
478 |
* @param string $join
|
|
|
479 |
* @return string
|
|
|
480 |
* @since Method available since Release 3.2.12
|
|
|
481 |
*/
|
|
|
482 |
protected static function arrayToName(array $parts, $join = '\\')
|
|
|
483 |
{
|
|
|
484 |
$result = '';
|
|
|
485 |
|
|
|
486 |
if (count($parts) > 1) {
|
|
|
487 |
array_pop($parts);
|
|
|
488 |
|
|
|
489 |
$result = join($join, $parts);
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
return $result;
|
|
|
493 |
}
|
|
|
494 |
}
|
|
|
495 |
?>
|