1
1
<?php
2
+
2
3
namespace AspectMock \Core ;
4
+
3
5
use AspectMock \Intercept \FunctionInjector ;
4
6
use Go \Aop \Aspect ;
5
7
use AspectMock \Intercept \MethodInvocation ;
6
8
7
- class Mocker implements Aspect {
9
+ class Mocker implements Aspect
10
+ {
8
11
9
- protected $ classMap = [];
10
- protected $ objectMap = [];
11
- protected $ funcMap = [];
12
- protected $ methodMap = ['__call ' , '__callStatic ' ];
12
+ protected $ classMap = [];
13
+ protected $ objectMap = [];
14
+ protected $ funcMap = [];
15
+ protected $ methodMap = ['__call ' , '__callStatic ' ];
13
16
protected $ dynamicMethods = ['__call ' , '__callStatic ' ];
14
17
15
18
public function fakeMethodsAndRegisterCalls ($ class , $ declaredClass , $ method , $ params , $ static )
16
19
{
17
- // $method = $invocation->getMethod();
18
- // $obj = $invocation->getThis();
19
20
$ result = __AM_CONTINUE__ ;
20
21
21
22
if (in_array ($ method , $ this ->methodMap )) {
@@ -35,19 +36,25 @@ public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $pa
35
36
}
36
37
37
38
if (!$ static ) {
38
- if (isset ($ this ->objectMap [spl_object_hash ($ class )])) Registry::registerInstanceCall ($ class , $ method , $ params );
39
+ if (isset ($ this ->objectMap [spl_object_hash ($ class )])) {
40
+ Registry::registerInstanceCall ($ class , $ method , $ params );
41
+ }
39
42
$ class = get_class ($ class );
40
43
}
41
44
42
- if (isset ($ this ->classMap [$ class ])) Registry::registerClassCall ($ class , $ method , $ params );
43
- if ($ class != $ declaredClass && isset ($ this ->classMap [$ declaredClass ])) Registry::registerClassCall ($ declaredClass , $ method , $ params );
45
+ if (isset ($ this ->classMap [$ class ])) {
46
+ Registry::registerClassCall ($ class , $ method , $ params );
47
+ }
48
+ if ($ class != $ declaredClass && isset ($ this ->classMap [$ declaredClass ])) {
49
+ Registry::registerClassCall ($ declaredClass , $ method , $ params );
50
+ }
44
51
45
52
return $ result ;
46
53
}
47
54
48
55
public function fakeFunctionAndRegisterCalls ($ namespace , $ function , $ args )
49
56
{
50
- $ result = __AM_CONTINUE__ ;
57
+ $ result = __AM_CONTINUE__ ;
51
58
$ fullFuncName = "$ namespace \\$ function " ;
52
59
Registry::registerFunctionCall ($ fullFuncName , $ args );
53
60
@@ -65,57 +72,83 @@ public function fakeFunctionAndRegisterCalls($namespace, $function, $args)
65
72
protected function invokeFakedMethods (MethodInvocation $ invocation )
66
73
{
67
74
$ method = $ invocation ->getMethod ();
68
- if (!in_array ($ method , $ this ->methodMap )) return __AM_CONTINUE__ ;
75
+ if (!in_array ($ method , $ this ->methodMap )) {
76
+ return __AM_CONTINUE__ ;
77
+ }
69
78
70
79
$ obj = $ invocation ->getThis ();
71
80
72
81
if (is_object ($ obj )) {
73
82
// instance method
74
83
$ params = $ this ->getObjectMethodStubParams ($ obj , $ method );
75
- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
84
+ if ($ params !== false ) {
85
+ return $ this ->stub ($ invocation , $ params );
86
+ }
76
87
77
88
// class method
78
89
$ params = $ this ->getClassMethodStubParams (get_class ($ obj ), $ method );
79
- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
90
+ if ($ params !== false ) {
91
+ return $ this ->stub ($ invocation , $ params );
92
+ }
80
93
81
94
// inheritance
82
95
$ params = $ this ->getClassMethodStubParams ($ invocation ->getDeclaredClass (), $ method );
83
- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
96
+ if ($ params !== false ) {
97
+ return $ this ->stub ($ invocation , $ params );
98
+ }
84
99
85
100
// magic methods
86
101
if ($ method == '__call ' ) {
87
- $ args = $ invocation ->getArguments ();
102
+ $ args = $ invocation ->getArguments ();
88
103
$ method = array_shift ($ args );
89
104
90
105
$ params = $ this ->getObjectMethodStubParams ($ obj , $ method );
91
- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
106
+ if ($ params !== false ) {
107
+ return $ this ->stubMagicMethod ($ invocation , $ params );
108
+ }
92
109
93
110
// magic class method
94
111
$ params = $ this ->getClassMethodStubParams (get_class ($ obj ), $ method );
95
- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
112
+ if ($ params !== false ) {
113
+ return $ this ->stubMagicMethod ($ invocation , $ params );
114
+ }
96
115
97
116
// inheritance
98
117
$ calledClass = $ invocation ->getDeclaredClass ();
99
- $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
100
- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
118
+ $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
119
+ if ($ params !== false ) {
120
+ return $ this ->stubMagicMethod ($ invocation , $ params );
121
+ }
101
122
}
102
123
} else {
103
124
// static method
104
125
$ params = $ this ->getClassMethodStubParams ($ obj , $ method );
105
- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
126
+ if ($ params !== false ) {
127
+ return $ this ->stub ($ invocation , $ params );
128
+ }
129
+
130
+ // inheritance
131
+ $ params = $ this ->getClassMethodStubParams ($ invocation ->getDeclaredClass (), $ method );
132
+ if ($ params !== false ) {
133
+ return $ this ->stub ($ invocation , $ params );
134
+ }
106
135
107
136
// magic static method (facade)
108
137
if ($ method == '__callStatic ' ) {
109
- $ args = $ invocation ->getArguments ();
138
+ $ args = $ invocation ->getArguments ();
110
139
$ method = array_shift ($ args );
111
140
112
141
$ params = $ this ->getClassMethodStubParams ($ obj , $ method );
113
- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
142
+ if ($ params !== false ) {
143
+ return $ this ->stubMagicMethod ($ invocation , $ params );
144
+ }
114
145
115
146
// inheritance
116
147
$ calledClass = $ invocation ->getDeclaredClass ();
117
- $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
118
- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
148
+ $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
149
+ if ($ params !== false ) {
150
+ return $ this ->stubMagicMethod ($ invocation , $ params );
151
+ }
119
152
}
120
153
}
121
154
return __AM_CONTINUE__ ;
@@ -124,17 +157,25 @@ protected function invokeFakedMethods(MethodInvocation $invocation)
124
157
protected function getObjectMethodStubParams ($ obj , $ method_name )
125
158
{
126
159
$ oid = spl_object_hash ($ obj );
127
- if (!isset ($ this ->objectMap [$ oid ])) return false ;
160
+ if (!isset ($ this ->objectMap [$ oid ])) {
161
+ return false ;
162
+ }
128
163
$ params = $ this ->objectMap [$ oid ];
129
- if (!array_key_exists ($ method_name ,$ params )) return false ;
164
+ if (!array_key_exists ($ method_name , $ params )) {
165
+ return false ;
166
+ }
130
167
return $ params ;
131
168
}
132
169
133
170
protected function getClassMethodStubParams ($ class_name , $ method_name )
134
171
{
135
- if (!isset ($ this ->classMap [$ class_name ])) return false ;
172
+ if (!isset ($ this ->classMap [$ class_name ])) {
173
+ return false ;
174
+ }
136
175
$ params = $ this ->classMap [$ class_name ];
137
- if (!array_key_exists ($ method_name ,$ params )) return false ;
176
+ if (!array_key_exists ($ method_name , $ params )) {
177
+ return false ;
178
+ }
138
179
return $ params ;
139
180
}
140
181
@@ -173,35 +214,37 @@ protected function stubMagicMethod(MethodInvocation $invocation, $params)
173
214
174
215
protected function turnToClosure ($ returnValue )
175
216
{
176
- if ($ returnValue instanceof \Closure) return $ returnValue ;
177
- return function () use ($ returnValue ) {
217
+ if ($ returnValue instanceof \Closure) {
218
+ return $ returnValue ;
219
+ }
220
+ return function () use ($ returnValue ) {
178
221
return $ returnValue ;
179
222
};
180
223
}
181
224
182
- public function registerClass ($ class , $ params = array () )
225
+ public function registerClass ($ class , $ params = [] )
183
226
{
184
- $ class = ltrim ($ class ,'\\' );
227
+ $ class = ltrim ($ class , '\\' );
185
228
if (isset ($ this ->classMap [$ class ])) {
186
229
$ params = array_merge ($ this ->classMap [$ class ], $ params );
187
230
}
188
- $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
231
+ $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
189
232
$ this ->classMap [$ class ] = $ params ;
190
233
}
191
234
192
- public function registerObject ($ object , $ params = array () )
235
+ public function registerObject ($ object , $ params = [] )
193
236
{
194
237
$ hash = spl_object_hash ($ object );
195
238
if (isset ($ this ->objectMap [$ hash ])) {
196
239
$ params = array_merge ($ this ->objectMap [$ hash ], $ params );
197
240
}
198
241
$ this ->objectMap [$ hash ] = $ params ;
199
- $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
242
+ $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
200
243
}
201
244
202
245
public function registerFunc ($ namespace , $ func , $ body )
203
246
{
204
- $ namespace = ltrim ($ namespace ,'\\' );
247
+ $ namespace = ltrim ($ namespace , '\\' );
205
248
if (!function_exists ("$ namespace \\$ func " )) {
206
249
$ injector = new FunctionInjector ($ namespace , $ func );
207
250
$ injector ->save ();
@@ -213,10 +256,10 @@ public function registerFunc($namespace, $func, $body)
213
256
public function clean ($ objectOrClass = null )
214
257
{
215
258
if (!$ objectOrClass ) {
216
- $ this ->classMap = [];
259
+ $ this ->classMap = [];
217
260
$ this ->objectMap = [];
218
- $ this ->methodMap = ['__call ' ,'__callStatic ' ];
219
- $ this ->funcMap = [];
261
+ $ this ->methodMap = ['__call ' , '__callStatic ' ];
262
+ $ this ->funcMap = [];
220
263
} elseif (is_object ($ objectOrClass )) {
221
264
unset($ this ->objectMap [spl_object_hash ($ objectOrClass )]);
222
265
} else {
0 commit comments