Skip to content

Commit e3a5279

Browse files
committed
Merge branch 'master' of github.com:Codeception/AspectMock
2 parents 6390216 + 8dd5f53 commit e3a5279

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/AspectMock/Core/Mocker.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,43 @@ protected function stub(MethodInvocation $invocation, $params)
142142
{
143143
$name = $invocation->getMethod();
144144

145-
$replacedMethod = $params[$name];
146-
147-
$replacedMethod = $this->turnToClosure($replacedMethod);
148-
149-
if ($invocation->isStatic()) {
150-
$replacedMethod = \Closure::bind($replacedMethod, null, $invocation->getThis());
151-
} else {
152-
$replacedMethod = $replacedMethod->bindTo($invocation->getThis(), get_class($invocation->getThis()));
153-
}
154-
return call_user_func_array($replacedMethod, $invocation->getArguments());
145+
return $this->stubIteratively($name, $params, $invocation);
155146
}
156147

157148
protected function stubMagicMethod(MethodInvocation $invocation, $params)
158149
{
159150
$args = $invocation->getArguments();
160151
$name = array_shift($args);
161152

162-
$replacedMethod = $params[$name];
153+
return $this->stubIteratively($name, $params, $invocation);
154+
}
155+
156+
protected function stubIteratively($name, $params, MethodInvocation $invocation)
157+
{
158+
$replacedMethods = $params[$name];
159+
160+
if (!is_array($replacedMethods)) $replacedMethods = [$replacedMethods];
161+
162+
$result = __AM_CONTINUE__;
163+
164+
while ($result === __AM_CONTINUE__ && count($replacedMethods) > 0) {
165+
$replacedMethod = array_pop($replacedMethods);
166+
$result = $this->doStub($invocation, $replacedMethod);
167+
}
168+
169+
return $result;
170+
}
171+
172+
protected function doStub(MethodInvocation $invocation, $replacedMethod)
173+
{
163174
$replacedMethod = $this->turnToClosure($replacedMethod);
164175

165176
if ($invocation->isStatic()) {
166-
\Closure::bind($replacedMethod, null, $invocation->getThis());
177+
$replacedMethod = \Closure::bind($replacedMethod, null, $invocation->getThis());
167178
} else {
168179
$replacedMethod = $replacedMethod->bindTo($invocation->getThis(), get_class($invocation->getThis()));
169180
}
170-
return call_user_func_array($replacedMethod, $args);
181+
return call_user_func_array($replacedMethod, $invocation->getArguments());
171182
}
172183

173184

@@ -183,7 +194,7 @@ public function registerClass($class, $params = array())
183194
{
184195
$class = ltrim($class,'\\');
185196
if (isset($this->classMap[$class])) {
186-
$params = array_merge($this->classMap[$class], $params);
197+
$params = array_merge_recursive($this->classMap[$class], $params);
187198
}
188199
$this->methodMap = array_merge($this->methodMap, array_keys($params));
189200
$this->classMap[$class] = $params;
@@ -193,7 +204,7 @@ public function registerObject($object, $params = array())
193204
{
194205
$hash = spl_object_hash($object);
195206
if (isset($this->objectMap[$hash])) {
196-
$params = array_merge($this->objectMap[$hash], $params);
207+
$params = array_merge_recursive($this->objectMap[$hash], $params);
197208
}
198209
$this->objectMap[$hash] = $params;
199210
$this->methodMap = array_merge($this->methodMap, array_keys($params));

src/AspectMock/Test.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ class Test {
8181
* $user = new User(['name' => 'davert']);
8282
* $user->save(); // false
8383
*
84+
* # on conflicted stubs
85+
* test::double('User', ['getGroup' => function () { return $this->group; }]);
86+
* $user = new User;
87+
* $user->group = 'guest';
88+
* $user->getGroup(); // => guest
89+
* test::double('User', ['getGroup' => function ($supersede = false) { return $supersede ? 'root' : __AM_CONTINUE__; }]);
90+
* $user->group = 'user';
91+
* $user->getGroup(true); // => root
92+
* $user->getGroup(); // => user
93+
*
8494
* ?>
8595
* ```
8696
*

tests/unit/StubTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,45 @@ public function testBindSelfCallback()
4242
$this->assertEquals('awesome', $topSecret);
4343
}
4444

45+
public function testStackedStub()
46+
{
47+
double::registerClass(\demo\UserModel::class, ['getName' => function () {
48+
return 'tsui';
49+
}]);
50+
double::registerClass(\demo\UserModel::class, ['getName' => function () {
51+
return 'tt';
52+
}]);
53+
double::registerClass(\demo\UserModel::class, ['getName' => function () {
54+
return __AM_CONTINUE__;
55+
}]);
56+
$user = new AdminUserModel(['name' => 'torreytsui']);
57+
$name = $user->getName();
58+
$this->assertEquals('tt', $name);
59+
}
60+
4561
public function testObjectInstance()
4662
{
4763
$user = new UserModel(['name' => 'davert']);
4864
double::registerObject($user,['save' => null]);
4965
$user->save();
5066
}
5167

68+
public function testObjectInstanceStackedStub()
69+
{
70+
$user = new AdminUserModel(['name' => 'torreytsui']);
71+
double::registerObject($user, ['getName' => function () {
72+
return 'tsui';
73+
}]);
74+
double::registerObject($user, ['getName' => function () {
75+
return 'tt';
76+
}]);
77+
double::registerObject($user, ['getName' => function () {
78+
return __AM_CONTINUE__;
79+
}]);
80+
$name = $user->getName();
81+
$this->assertEquals('tt', $name);
82+
}
83+
5284
public function testStaticAccess()
5385
{
5486
$this->assertEquals('users', UserModel::tableName());

0 commit comments

Comments
 (0)