| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvert\Tests; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class for exposing otherwise unaccessible methods of classes |
| 7 |
* - so they can be tested |
| 8 |
*/ |
| 9 |
class BaseExposer { |
| 10 |
|
| 11 |
//public static $extraOptions = []; |
| 12 |
|
| 13 |
public $objectToExposeFrom; |
| 14 |
public static $currentlyCalling; |
| 15 |
public static $currentlyStealing; |
| 16 |
|
| 17 |
public function __construct($objectToExposeFrom) |
| 18 |
{ |
| 19 |
$this->objectToExposeFrom = $objectToExposeFrom; |
| 20 |
} |
| 21 |
|
| 22 |
protected function bindDynamicFunctionToObjectAndCallIt($functionToBindToObject, $class = null, ...$args) |
| 23 |
{ |
| 24 |
if (is_null($class)) { |
| 25 |
$class = get_class($this->objectToExposeFrom); |
| 26 |
} |
| 27 |
//$functionNowBinded = $functionToBindToObject->bindTo($this->objectToExposeFrom, AbstractConverter::class); |
| 28 |
$functionNowBinded = $functionToBindToObject->bindTo($this->objectToExposeFrom, $class); |
| 29 |
//$functionNowBinded = $functionToBindToObject->bindTo($this->objectToExposeFrom, get_class($this->objectToExposeFrom)); |
| 30 |
return $functionNowBinded(...$args); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @param string $functionNameToCall |
| 35 |
* @param string $class The class to inject into, ie a base class of the object to expose from (optional). If none is specified, it will be the class of the exposed object |
| 36 |
*/ |
| 37 |
protected function callPrivateFunction($functionNameToCall, $class = null, ...$args) |
| 38 |
{ |
| 39 |
self::$currentlyCalling = $functionNameToCall; |
| 40 |
$cb = function() { |
| 41 |
return call_user_func_array( |
| 42 |
array($this, BaseExposer::$currentlyCalling), |
| 43 |
func_get_args() |
| 44 |
); |
| 45 |
}; |
| 46 |
return $this->bindDynamicFunctionToObjectAndCallIt($cb, $class, ...$args); |
| 47 |
} |
| 48 |
|
| 49 |
protected function callPrivateFunctionByRef($functionNameToCall, &$arg1) |
| 50 |
{ |
| 51 |
self::$currentlyCalling = $functionNameToCall; |
| 52 |
$cb = function(&$arg1) { |
| 53 |
//echo 'callback...' . gettype($arg1); |
| 54 |
return $this->{BaseExposer::$currentlyCalling}($arg1); |
| 55 |
/* |
| 56 |
return call_user_func_array( |
| 57 |
array($this, BaseExposer::$currentlyCalling), |
| 58 |
$arg1 |
| 59 |
);*/ |
| 60 |
}; |
| 61 |
$class = get_class($this->objectToExposeFrom); |
| 62 |
$functionNowBinded = $cb->bindTo($this->objectToExposeFrom, $class); |
| 63 |
|
| 64 |
return $functionNowBinded($arg1); |
| 65 |
//return $this->bindDynamicFunctionToObjectAndCallIt($cb, $class, $arg1); |
| 66 |
} |
| 67 |
|
| 68 |
/* work in progress |
| 69 |
protected function callPrivateStaticFunction($functionNameToCall, $class = null) |
| 70 |
{ |
| 71 |
self::$currentlyCalling = $functionNameToCall; |
| 72 |
|
| 73 |
$cb = function() { |
| 74 |
return call_user_func_array( |
| 75 |
array(self, BaseExposer::$currentlyCalling), |
| 76 |
func_get_args() |
| 77 |
); |
| 78 |
}; |
| 79 |
return $this->bindDynamicFunctionToObjectAndCallIt($cb, $class); |
| 80 |
}*/ |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* @param string $propertyToSteal |
| 85 |
*/ |
| 86 |
protected function getPrivateProperty($propertyToSteal, $class = null) |
| 87 |
{ |
| 88 |
self::$currentlyStealing = $propertyToSteal; |
| 89 |
|
| 90 |
$thief = function() { |
| 91 |
return $this->{BaseExposer::$currentlyStealing}; |
| 92 |
}; |
| 93 |
|
| 94 |
return $this->bindDynamicFunctionToObjectAndCallIt($thief, $class); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param string $propertyToSteal |
| 99 |
*/ |
| 100 |
protected function getPrivateStaticProperty($propertyToSteal, $class = null) |
| 101 |
{ |
| 102 |
self::$currentlyStealing = $propertyToSteal; |
| 103 |
|
| 104 |
$thief = function() { |
| 105 |
$propertyName = BaseExposer::$currentlyStealing; |
| 106 |
return static::$$propertyName; |
| 107 |
}; |
| 108 |
|
| 109 |
return $this->bindDynamicFunctionToObjectAndCallIt($thief, $class); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
} |
| 114 |
|