|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespaceSymfony\Bridge\PhpUnit; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Roland Franssen <franssen.roland@gmail.com> |
| 16 | + */ |
| 17 | +class ClassExistsMock |
| 18 | +{ |
| 19 | +privatestatic$classes =array(); |
| 20 | + |
| 21 | +/** |
| 22 | + * Configures the classes to be checked upon existence. |
| 23 | + * |
| 24 | + * @param array $classes Mocked class names as keys (case sensitive, without leading root namespace slash) and booleans as values |
| 25 | + */ |
| 26 | +publicstaticfunctionwithMockedClasses(array$classes) |
| 27 | + { |
| 28 | +self::$classes =$classes; |
| 29 | + } |
| 30 | + |
| 31 | +publicstaticfunctionclass_exists($name,$autoload =true) |
| 32 | + { |
| 33 | +return (bool) (self::$classes[ltrim($name,'\\')] ??\class_exists($name,$autoload)); |
| 34 | + } |
| 35 | + |
| 36 | +publicstaticfunctioninterface_exists($name,$autoload =true) |
| 37 | + { |
| 38 | +return (bool) (self::$classes[ltrim($name,'\\')] ??\interface_exists($name,$autoload)); |
| 39 | + } |
| 40 | + |
| 41 | +publicstaticfunctiontrait_exists($name,$autoload =true) |
| 42 | + { |
| 43 | +return (bool) (self::$classes[ltrim($name,'\\')] ??\trait_exists($name,$autoload)); |
| 44 | + } |
| 45 | + |
| 46 | +publicstaticfunctionregister($class) |
| 47 | + { |
| 48 | +$self =\get_called_class(); |
| 49 | + |
| 50 | +$mockedNs =array(substr($class,0,strrpos($class,'\\'))); |
| 51 | +if (0 <strpos($class,'\\Tests\\')) { |
| 52 | +$ns =str_replace('\\Tests\\','\\',$class); |
| 53 | +$mockedNs[] =substr($ns,0,strrpos($ns,'\\')); |
| 54 | + }elseif (0 ===strpos($class,'Tests\\')) { |
| 55 | +$mockedNs[] =substr($class,6,strrpos($class,'\\') -6); |
| 56 | + } |
| 57 | +foreach ($mockedNsas$ns) { |
| 58 | +foreach (array('class','interface','trait')as$type) { |
| 59 | +if (\function_exists($ns.'\\'.$type.'_exists')) { |
| 60 | +continue; |
| 61 | + } |
| 62 | +eval(<<<EOPHP |
| 63 | +namespace$ns; |
| 64 | +
|
| 65 | +function{$type}_exists(\$name,\$autoload = true) |
| 66 | +{ |
| 67 | + return \\$self::{$type}_exists(\$name,\$autoload); |
| 68 | +} |
| 69 | +
|
| 70 | +EOPHP |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |