-
Notifications
You must be signed in to change notification settings - Fork 3
/
BuiltinMock.php
91 lines (82 loc) · 2.58 KB
/
BuiltinMock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Encapsulates and controls mocking of PHP builtin methods.
*
* This requires the APD extension to be installed.
*
* Example usage:
* $sOriginalFunction = BuiltinMock::override('time', new BuiltinMock_Time_SomeOverride());
* $iTimeMock = time(); // returns the value of the time() mock
* $iTimeOrig = $sOriginalFunction(); // returns the value of the builtin time()
*
* BuiltinMock::restore('time');
* $iTime = time(); // returns the value of the builtin time()
*/
class BuiltinMock
{
/**
* @var array
*/
protected static $aOverrides = array();
/////////////////////////////////////////////////////////////////////////////
// STATIC //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/**
* Call the overriden function
*
* @param string $sFunction required=true
* @param array $aArgs required=true
* @return mixed
*/
public static function callOverride($sFunction, $aArgs)
{
return call_user_func_array(array(self::$aOverrides[$sFunction], 'mock'), array($aArgs));
}
/**
* Return the renamed version of a function
*
* @param string $sFunction required=true
* @return string
*/
public static function getRenamedFunction($sFunction)
{
return "__original_{$sFunction}";
}
/**
* Override a given function with a mock
*
* @param string $sFunction required=true
* @param object $oOverrider required=true
* @return string name by which the original mocked function can be called
*/
public static function override($sFunction, $oOverrider)
{
$sDefinition = <<<DEFINITION
\$aArgs = func_get_args();
return call_user_func_array(array('BuiltinMock', 'callOverride'), array('$sFunction', \$aArgs));
DEFINITION;
self::restore($sFunction);
$sRenamedFunction = self::getRenamedFunction($sFunction);
rename_function($sFunction, $sRenamedFunction);
override_function($sFunction, '', $sDefinition);
rename_function("__overridden__", uniqid("__overridden__"));
$oOverrider->setOriginalName($sRenamedFunction);
self::$aOverrides[$sFunction] = $oOverrider;
return $sRenamedFunction;
}
/**
* Restore an overriden function to its builtin state
*
* @param string $sFunction required=true
*/
public static function restore($sFunction)
{
$sRenamedFunction = self::getRenamedFunction($sFunction);
if (function_exists($sRenamedFunction)) {
rename_function($sFunction, uniqid($sFunction));
rename_function($sRenamedFunction, $sFunction);
}
unset(self::$aOverrides[$sFunction]);
}
}
?>