]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/Class.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / Class.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.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  * @package    PHPUnit
38  * @subpackage Util
39  * @author     Sebastian Bergmann <sebastian@phpunit.de>
40  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.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 /**
51  * Class helpers.
52  *
53  * @package    PHPUnit
54  * @subpackage Util
55  * @author     Sebastian Bergmann <sebastian@phpunit.de>
56  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
57  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58  * @version    Release: 3.5.13
59  * @link       http://www.phpunit.de/
60  * @since      Class available since Release 3.1.0
61  */
62 class PHPUnit_Util_Class
63 {
64     protected static $buffer = array();
65
66     /**
67      * Starts the collection of loaded classes.
68      *
69      */
70     public static function collectStart()
71     {
72         self::$buffer = get_declared_classes();
73     }
74
75     /**
76      * Stops the collection of loaded classes and
77      * returns the names of the loaded classes.
78      *
79      * @return array
80      */
81     public static function collectEnd()
82     {
83         return array_values(
84           array_diff(get_declared_classes(), self::$buffer)
85         );
86     }
87
88     /**
89      * Returns the class hierarchy for a given class.
90      *
91      * @param  string  $className
92      * @param  boolean $asReflectionObjects
93      * @return array
94      */
95     public static function getHierarchy($className, $asReflectionObjects = FALSE)
96     {
97         if ($asReflectionObjects) {
98             $classes = array(new ReflectionClass($className));
99         } else {
100             $classes = array($className);
101         }
102
103         $done = FALSE;
104
105         while (!$done) {
106             if ($asReflectionObjects) {
107                 $class = new ReflectionClass(
108                   $classes[count($classes)-1]->getName()
109                 );
110             } else {
111                 $class = new ReflectionClass($classes[count($classes)-1]);
112             }
113
114             $parent = $class->getParentClass();
115
116             if ($parent !== FALSE) {
117                 if ($asReflectionObjects) {
118                     $classes[] = $parent;
119                 } else {
120                     $classes[] = $parent->getName();
121                 }
122             } else {
123                 $done = TRUE;
124             }
125         }
126
127         return $classes;
128     }
129
130     /**
131      * Returns the parameters of a function or method.
132      *
133      * @param  ReflectionFunction|ReflectionMethod $method
134      * @param  boolean                             $forCall
135      * @return string
136      * @since  Method available since Release 3.2.0
137      */
138     public static function getMethodParameters($method, $forCall = FALSE)
139     {
140         $parameters = array();
141
142         foreach ($method->getParameters() as $i => $parameter) {
143             $name = '$' . $parameter->getName();
144
145             if ($name === '$') {
146                 $name .= 'arg' . $i;
147             }
148
149             $default  = '';
150             $typeHint = '';
151
152             if (!$forCall) {
153                 if ($parameter->isArray()) {
154                     $typeHint = 'array ';
155                 } else {
156                     try {
157                         $class = $parameter->getClass();
158                     }
159
160                     catch (ReflectionException $e) {
161                         $class = FALSE;
162                     }
163
164                     if ($class) {
165                         $typeHint = $class->getName() . ' ';
166                     }
167                 }
168
169                 if ($parameter->isDefaultValueAvailable()) {
170                     $value   = $parameter->getDefaultValue();
171                     $default = ' = ' . var_export($value, TRUE);
172                 }
173
174                 else if ($parameter->isOptional()) {
175                     $default = ' = null';
176                 }
177             }
178
179             $ref = '';
180
181             if ($parameter->isPassedByReference()) {
182                 $ref = '&';
183             }
184
185             $parameters[] = $typeHint . $ref . $name . $default;
186         }
187
188         return join(', ', $parameters);
189     }
190
191     /**
192      * Returns the package information of a user-defined class.
193      *
194      * @param  string $className
195      * @param  string $docComment
196      * @return array
197      */
198     public static function getPackageInformation($className, $docComment)
199     {
200         $result = array(
201           'namespace'   => '',
202           'fullPackage' => '',
203           'category'    => '',
204           'package'     => '',
205           'subpackage'  => ''
206         );
207
208         if (strpos($className, '\\') !== FALSE) {
209             $result['namespace'] = self::arrayToName(
210               explode('\\', $className)
211             );
212         }
213
214         if (preg_match('/@category[\s]+([\.\w]+)/', $docComment, $matches)) {
215             $result['category'] = $matches[1];
216         }
217
218         if (preg_match('/@package[\s]+([\.\w]+)/', $docComment, $matches)) {
219             $result['package']     = $matches[1];
220             $result['fullPackage'] = $matches[1];
221         }
222
223         if (preg_match('/@subpackage[\s]+([\.\w]+)/', $docComment, $matches)) {
224             $result['subpackage']   = $matches[1];
225             $result['fullPackage'] .= '.' . $matches[1];
226         }
227
228         if (empty($result['fullPackage'])) {
229             $result['fullPackage'] = self::arrayToName(
230               explode('_', str_replace('\\', '_', $className)), '.'
231             );
232         }
233
234         return $result;
235     }
236
237     /**
238      * Returns the value of a static attribute.
239      * This also works for attributes that are declared protected or private.
240      *
241      * @param  string  $className
242      * @param  string  $attributeName
243      * @return mixed
244      * @throws InvalidArgumentException
245      * @since  Method available since Release 3.4.0
246      */
247     public static function getStaticAttribute($className, $attributeName)
248     {
249         if (!is_string($className)) {
250             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
251         }
252
253         if (!class_exists($className)) {
254             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name');
255         }
256
257         if (!is_string($attributeName)) {
258             throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
259         }
260
261         $class = new ReflectionClass($className);
262
263         while ($class) {
264             $attributes = $class->getStaticProperties();
265
266             if (array_key_exists($attributeName, $attributes)) {
267                 return $attributes[$attributeName];
268             }
269
270             $class = $class->getParentClass();
271         }
272
273         throw new PHPUnit_Framework_Exception(
274           sprintf(
275             'Attribute "%s" not found in class.',
276
277             $attributeName
278           )
279         );
280     }
281
282     /**
283      * Returns the value of an object's attribute.
284      * This also works for attributes that are declared protected or private.
285      *
286      * @param  object  $object
287      * @param  string  $attributeName
288      * @return mixed
289      * @throws InvalidArgumentException
290      * @since  Method available since Release 3.4.0
291      */
292     public static function getObjectAttribute($object, $attributeName)
293     {
294         if (!is_object($object)) {
295             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'object');
296         }
297
298         if (!is_string($attributeName)) {
299             throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
300         }
301
302         PHPUnit_Framework_Assert::assertObjectHasAttribute(
303           $attributeName, $object
304         );
305
306         try {
307             $attribute = new ReflectionProperty($object, $attributeName);
308         }
309
310         catch (ReflectionException $e) {
311             $reflector = new ReflectionObject($object);
312
313             while ($reflector = $reflector->getParentClass()) {
314                 try {
315                     $attribute = $reflector->getProperty($attributeName);
316                     break;
317                 }
318
319                 catch(ReflectionException $e) {
320                 }
321             }
322         }
323
324         if ($attribute == NULL || $attribute->isPublic()) {
325             return $object->$attributeName;
326         } else {
327             $array         = (array)$object;
328             $protectedName = "\0*\0" . $attributeName;
329
330             if (array_key_exists($protectedName, $array)) {
331                 return $array[$protectedName];
332             } else {
333                 $classes = self::getHierarchy(get_class($object));
334
335                 foreach ($classes as $class) {
336                     $privateName = sprintf(
337                       "\0%s\0%s",
338
339                       $class,
340                       $attributeName
341                     );
342
343                     if (array_key_exists($privateName, $array)) {
344                         return $array[$privateName];
345                     }
346                 }
347             }
348         }
349
350         throw new PHPUnit_Framework_Exception(
351           sprintf(
352             'Attribute "%s" not found in object.',
353
354             $attributeName
355           )
356         );
357     }
358
359     /**
360      *
361      *
362      * @param  string $className
363      * @return array
364      * @since  Method available since Release 3.4.0
365      */
366     public static function parseFullyQualifiedClassName($className)
367     {
368         $result = array(
369           'namespace'               => '',
370           'className'               => $className,
371           'fullyQualifiedClassName' => $className
372         );
373
374         if (strpos($className, '\\') !== FALSE) {
375             $tmp                 = explode('\\', $className);
376             $result['className'] = $tmp[count($tmp)-1];
377             $result['namespace'] = self::arrayToName($tmp);
378         }
379
380         return $result;
381     }
382
383     /**
384      * Returns the package information of a user-defined class.
385      *
386      * @param  array  $parts
387      * @param  string $join
388      * @return string
389      * @since  Method available since Release 3.2.12
390      */
391     protected static function arrayToName(array $parts, $join = '\\')
392     {
393         $result = '';
394
395         if (count($parts) > 1) {
396             array_pop($parts);
397
398             $result = join($join, $parts);
399         }
400
401         return $result;
402     }
403 }