-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMockFunction.php
More file actions
162 lines (141 loc) · 3.77 KB
/
MockFunction.php
File metadata and controls
162 lines (141 loc) · 3.77 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Mock Function
*/
class PHPUnit_Extensions_MockFunction
{
/**
* @var array
*/
static protected $mockObjects = array();
/**
* @var string
*/
protected $functionName;
/**
* @var string
*/
protected $functionAlias;
/**
* @var boolean
*/
protected $active = false;
/**
* Construct
*
* @param string $functionName
* @param PHPUnit_Framework_TestCase $testCase
*/
public function __construct($functionName, PHPUnit_Framework_TestCase $testCase)
{
$this->functionName = $functionName;
$this->functionAlias = uniqid("{$functionName}_");
if (!function_exists($functionName)) {
throw new InvalidArgumentException("Invalid function name '$functionName'");
}
if (array_key_exists($functionName, self::$mockObjects)) {
throw new RuntimeException("Can not create second function mock for '$functionName'");
}
self::$mockObjects[$functionName] = $testCase->getMock($this->functionAlias, array('call'));
$this->enable();
}
/**
* Enable
*
* @return \MockFunction
*/
public function enable()
{
if (!$this->active) {
$this->active = true;
runkit_function_rename($this->functionName, $this->functionAlias);
runkit_function_add($this->functionName, '', $this->getMockCode());
}
return $this;
}
/**
* Disable
*
* @return \MockFunction
*/
public function disable()
{
if ($this->active) {
$this->active = false;
runkit_function_remove($this->functionName);
runkit_function_rename($this->functionAlias, $this->functionName);
}
return $this;
}
/**
* Destruct
*/
public function __destruct()
{
unset(self::$mockObjects[$this->functionName]);
$this->disable();
}
/**
* Get original callback
*
* @return string
*/
public function getOriginalCallback()
{
if ($this->active) {
$functionName = $this->functionAlias;
} else {
$functionName = $this->functionName;
}
return $functionName;
}
/**
* Call original
*
* @return mixed
*/
public function callOriginal()
{
$callback = $this->getOriginalCallback();
return call_user_func_array($callback, func_get_args());
}
/**
* Get mock code
*
* @return string
*/
public function getMockCode()
{
$className = __CLASS__;
$functionName = var_export($this->functionName, true);
return "
\$callback = array($className::getMock($functionName), 'call');
return call_user_func_array(\$callback, func_get_args());
";
}
/**
* Registers a new expectation in the mock object and returns the match
* object which can be infused with further details.
*
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return self::getMock($this->functionName)->expects($matcher)->method('call');
}
/**
* Get mock
*
* @param string $functionName
* @return PHPUnit_Framework_MockObject_MockObject
* @throws \RuntimeException
*/
public static function getMock($functionName)
{
if (!array_key_exists($functionName, self::$mockObjects)) {
throw new \RuntimeException("Failed to get mock object for function '$functionName'");
}
return self::$mockObjects[$functionName];
}
}