Skip to content

Commit e15b716

Browse files
authored
Merge pull request #104 from Codeception/revert-91-feature/stacked-stub
Revert "Add stacked stub support"
2 parents 0a0077a + 720fd55 commit e15b716

File tree

3 files changed

+15
-68
lines changed

3 files changed

+15
-68
lines changed

src/AspectMock/Core/Mocker.php

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

145-
return $this->stubIteratively($name, $params, $invocation);
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());
146155
}
147156

148157
protected function stubMagicMethod(MethodInvocation $invocation, $params)
149158
{
150159
$args = $invocation->getArguments();
151160
$name = array_shift($args);
152161

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-
{
162+
$replacedMethod = $params[$name];
174163
$replacedMethod = $this->turnToClosure($replacedMethod);
175164

176165
if ($invocation->isStatic()) {
177-
$replacedMethod = \Closure::bind($replacedMethod, null, $invocation->getThis());
166+
\Closure::bind($replacedMethod, null, $invocation->getThis());
178167
} else {
179168
$replacedMethod = $replacedMethod->bindTo($invocation->getThis(), get_class($invocation->getThis()));
180169
}
181-
return call_user_func_array($replacedMethod, $invocation->getArguments());
170+
return call_user_func_array($replacedMethod, $args);
182171
}
183172

184173

@@ -194,7 +183,7 @@ public function registerClass($class, $params = array())
194183
{
195184
$class = ltrim($class,'\\');
196185
if (isset($this->classMap[$class])) {
197-
$params = array_merge_recursive($this->classMap[$class], $params);
186+
$params = array_merge($this->classMap[$class], $params);
198187
}
199188
$this->methodMap = array_merge($this->methodMap, array_keys($params));
200189
$this->classMap[$class] = $params;
@@ -204,7 +193,7 @@ public function registerObject($object, $params = array())
204193
{
205194
$hash = spl_object_hash($object);
206195
if (isset($this->objectMap[$hash])) {
207-
$params = array_merge_recursive($this->objectMap[$hash], $params);
196+
$params = array_merge($this->objectMap[$hash], $params);
208197
}
209198
$this->objectMap[$hash] = $params;
210199
$this->methodMap = array_merge($this->methodMap, array_keys($params));

src/AspectMock/Test.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ 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-
*
9484
* ?>
9585
* ```
9686
*

tests/unit/StubTest.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,45 +42,13 @@ 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-
6145
public function testObjectInstance()
6246
{
6347
$user = new UserModel(['name' => 'davert']);
6448
double::registerObject($user,['save' => null]);
6549
$user->save();
6650
}
6751

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-
8452
public function testStaticAccess()
8553
{
8654
$this->assertEquals('users', UserModel::tableName());

0 commit comments

Comments
 (0)