|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of ci-phpunit-test |
|
4
|
|
|
* |
|
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2015 Kenji Suzuki |
|
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Kenjis\MonkeyPatch; |
|
12
|
|
|
|
|
13
|
|
|
use Kenjis\MonkeyPatch\Patcher\FunctionPatcher\Proxy; |
|
14
|
|
|
use Kenjis\MonkeyPatch\Patcher\ConstantPatcher\Proxy as ConstProxy; |
|
15
|
|
|
use Kenjis\MonkeyPatch\Patcher\MethodPatcher\PatchManager; |
|
16
|
|
|
|
|
17
|
|
|
class MonkeyPatch |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Patch on function |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $function function name |
|
23
|
|
|
* @param mixed $return_value return value |
|
24
|
|
|
* @param string $class_name class::method to apply this patch |
|
|
|
|
|
|
25
|
|
|
*/ |
|
26
|
|
|
public static function patchFunction($function, $return_value, $class_method = null) |
|
27
|
|
|
{ |
|
28
|
|
|
Proxy::patch__($function, $return_value, $class_method); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Reset all patched fuctions |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function resetFunctions() |
|
35
|
|
|
{ |
|
36
|
|
|
Proxy::reset__(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Patch on constant |
|
41
|
|
|
* |
|
42
|
|
|
* @param type $constant |
|
43
|
|
|
* @param type $value |
|
44
|
|
|
* @param type $class_method |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function patchConstant($constant, $value, $class_method = null) |
|
47
|
|
|
{ |
|
48
|
|
|
ConstProxy::patch($constant, $value, $class_method); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Reset all patched constants |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function resetConstants() |
|
55
|
|
|
{ |
|
56
|
|
|
ConstProxy::reset(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Patch on class method |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $class |
|
63
|
|
|
* @param array $params [method_name => return_value] |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function patchMethod($class, $params) |
|
66
|
|
|
{ |
|
67
|
|
|
PatchManager::set($class, $params); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Reset all patched class method |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function resetMethods() |
|
74
|
|
|
{ |
|
75
|
|
|
PatchManager::clear(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected static function getClassname($class_method) |
|
79
|
|
|
{ |
|
80
|
|
|
if (strpos($class_method, '::') === false) |
|
81
|
|
|
{ |
|
82
|
|
|
return 'Kenjis\MonkeyPatch\Patcher\FunctionPatcher\Proxy'; |
|
83
|
|
|
} |
|
84
|
|
|
else |
|
85
|
|
|
{ |
|
86
|
|
|
return 'Kenjis\MonkeyPatch\Patcher\MethodPatcher\PatchManager'; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $class_method class::method or function name |
|
92
|
|
|
* @param int $times times |
|
93
|
|
|
* @param array $params parameters |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function verifyInvokedMultipleTimes( |
|
96
|
|
|
$class_method, $times, array $params = null |
|
97
|
|
|
) |
|
98
|
|
|
{ |
|
99
|
|
|
$classname = self::getClassname($class_method); |
|
100
|
|
|
$classname::setExpectedInvocations( |
|
101
|
|
|
$class_method, $times, $params |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $class_method class::method or function name |
|
107
|
|
|
* @param array $params parameters |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function verifyInvoked($class_method, array $params = null) |
|
110
|
|
|
{ |
|
111
|
|
|
$classname = self::getClassname($class_method); |
|
112
|
|
|
$classname::setExpectedInvocations( |
|
113
|
|
|
$class_method, '+', $params |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $class_method class::method or function name |
|
119
|
|
|
* @param array $params parameters |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function verifyInvokedOnce($class_method, array $params = null) |
|
122
|
|
|
{ |
|
123
|
|
|
$classname = self::getClassname($class_method); |
|
124
|
|
|
$classname::setExpectedInvocations( |
|
125
|
|
|
$class_method, 1, $params |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $class_method class::method or function name |
|
131
|
|
|
* @param array $params parameters |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function verifyNeverInvoked($class_method, array $params = null) |
|
134
|
|
|
{ |
|
135
|
|
|
$classname = self::getClassname($class_method); |
|
136
|
|
|
$classname::setExpectedInvocations( |
|
137
|
|
|
$class_method, 0, $params |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Run function verifcations |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function verifyFunctionInvocations() |
|
145
|
|
|
{ |
|
146
|
|
|
Proxy::verifyInvocations(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Run method verifcations |
|
151
|
|
|
*/ |
|
152
|
|
|
public static function verifyMethodInvocations() |
|
153
|
|
|
{ |
|
154
|
|
|
PatchManager::verifyInvocations(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.