Skip to content

Commit 1ace020

Browse files
committed
Merge pull request #82 from 501st-alpha1/fix-dynamic-class-methods
Fix counting of dynamic class methods
2 parents 334c418 + 4225c7c commit 1ace020

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/AspectMock/Core/Mocker.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Mocker implements Aspect {
1010
protected $objectMap = [];
1111
protected $funcMap = [];
1212
protected $methodMap = ['__call', '__callStatic'];
13+
protected $dynamicMethods = ['__call', '__callStatic'];
1314

1415
public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $params, $static)
1516
{
@@ -27,6 +28,12 @@ public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $pa
2728
$result = $this->invokeFakedMethods($invocation);
2829
}
2930

31+
// Record actual method called, not faked method.
32+
if (in_array($method, $this->dynamicMethods)) {
33+
$method = array_shift($params);
34+
$params = array_shift($params);
35+
}
36+
3037
if (!$static) {
3138
if (isset($this->objectMap[spl_object_hash($class)])) Registry::registerInstanceCall($class, $method, $params);
3239
$class = get_class($class);

tests/_data/demo/UserService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ public function updateName(UserModel $user)
1515
$user->save();
1616
}
1717

18+
public function renameUser(UserModel $user, $name)
19+
{
20+
$user->renameUser($name);
21+
$user->save();
22+
}
23+
1824
public function __call($name, $args)
1925
{
2026
if ($name == 'rename') {
2127
return 'David Blane';
2228
}
2329
}
2430

25-
}
31+
}

tests/unit/VerifierTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,33 @@ public function testVerifyInvocationClosures()
4242
});
4343
}
4444

45+
public function testVerifyMagicMethods()
46+
{
47+
$this->specify('works for class proxy', function() {
48+
// Set up user object.
49+
double::registerClass("demo\UserModel",
50+
['renameUser'=>"Bob Jones", 'save'=>null]);
51+
$userProxy = new ClassProxy("demo\UserModel");
52+
$user = new UserModel(['name'=>"John Smith"]);
53+
54+
// Rename the user via magic method.
55+
UserService::renameUser($user, "Bob Jones");
56+
57+
// Assert rename was counted.
58+
$userProxy->verifyInvoked('renameUser');
59+
});
60+
61+
$this->specify('works for instance proxy', function() {
62+
// Set up user object.
63+
$user = new UserModel(['name'=>"John Smith"]);
64+
double::registerObject($user);
65+
$user = new InstanceProxy($user);
66+
67+
// Rename the user via magic method.
68+
$user->renameUser("Bob Jones");
69+
70+
// Assert rename was counted.
71+
$user->verifyInvoked('renameUser');
72+
});
73+
}
4574
}

0 commit comments

Comments
 (0)