Skip to content

Commit 475dedd

Browse files
committed
more consistent namespace handling
1 parent 3b7e8ed commit 475dedd

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/AspectMock/Core/Mocker.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected function getClassMethodStubParams($class_name, $method_name)
128128
if (!array_key_exists($method_name,$params)) return false;
129129
return $params;
130130
}
131-
131+
132132
protected function stub(MethodInvocation $invocation, $params)
133133
{
134134
$name = $invocation->getMethod();
@@ -192,6 +192,7 @@ public function registerObject($object, $params = array())
192192

193193
public function registerFunc($namespace, $func, $body)
194194
{
195+
$namespace = ltrim($namespace,'\\');
195196
if (!function_exists("$namespace\\$func")) {
196197
$injector = new FunctionInjector($namespace, $func);
197198
$injector->save();

src/AspectMock/Core/Registry.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static function registerFunc($namespace, $function, $resultOrClosure)
3737

3838
static function getClassCallsFor($class)
3939
{
40+
$class = ltrim($class,'\\');
4041
return isset(self::$classCalls[$class])
4142
? self::$classCalls[$class]
4243
: [];
@@ -52,6 +53,7 @@ static function getInstanceCallsFor($instance)
5253

5354
static function getFuncCallsFor($func)
5455
{
56+
$func = ltrim($func,'\\');
5557
return isset(self::$funcCalls[$func]) ? self::$funcCalls[$func] : [];
5658
}
5759

@@ -123,4 +125,4 @@ public static function setMocker(Mocker $mocker)
123125
self::$mocker = $mocker;
124126
}
125127

126-
}
128+
}

tests/unit/FunctionInjectorTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public function testVerifier()
5959
$func->verifyNeverInvoked(['hee']);
6060
}
6161

62+
public function testVerifierFullyQualifiedNamespace()
63+
{
64+
$func = test::func('\demo', 'strlen', 10);
65+
expect(strlen('hello'))->equals(10);
66+
$func->verifyInvoked();
67+
$func->verifyInvoked(['hello']);
68+
$func->verifyInvokedOnce();
69+
$func->verifyInvokedOnce(['hello']);
70+
$func->verifyInvokedMultipleTimes(1, ['hello']);
71+
$func->verifyNeverInvoked(['hee']);
72+
}
73+
6274
public function testFailedVerification()
6375
{
6476
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
@@ -68,4 +80,3 @@ public function testFailedVerification()
6880
}
6981

7082
}
71-

tests/unit/testDoubleTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ public function testDoubleClass()
2626
});
2727
}
2828

29+
public function testDoubleFullyQualifiedClass()
30+
{
31+
$user = test::double('\demo\UserModel', ['save' => null]);
32+
(new demo\UserModel())->save();
33+
$user->verifyInvoked('save');
34+
\demo\UserModel::tableName();
35+
\demo\UserModel::tableName();
36+
$user->verifyInvokedMultipleTimes('tableName',2);
37+
38+
$this->specify('disabling all methods', function() use ($user) {
39+
test::methods($user, []);
40+
verify(\demo\UserModel::tableName())->null();
41+
});
42+
}
43+
2944
public function testDoubleObject()
3045
{
3146
$user = new demo\UserModel();
@@ -68,7 +83,7 @@ public function testSpecUndefinedClass()
6883
$this->any->that = 'xxx';
6984
$this->assertInstanceOf('AspectMock\Proxy\Anything', $this->any->this->that->another);
7085
});
71-
86+
7287
$this->specify('can be used as array', function() {
7388
$this->any['has keys'];
7489
unset($this->any['this']);

0 commit comments

Comments
 (0)