-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocking DB object can't use getDouble? #403
Comments
I fixed it by overriding <?php
class TestCase extends CIPHPUnitTestCase {
/**
* Override to fix stub bug
*/
public function getDouble($classname, $params, $constructor_params = false) {
$mockBuilder = $this->getMockBuilder($classname);
if ($constructor_params === false) {
$mockBuilder->disableOriginalConstructor();
}
elseif (is_array($constructor_params)) {
$mockBuilder->setConstructorArgs($constructor_params);
}
$methods = [];
$onConsecutiveCalls = [];
$otherCalls = [];
foreach ($params as $key => $val) {
if (is_int($key)) {
$onConsecutiveCalls = array_merge($onConsecutiveCalls, $val);
$methods[] = array_keys($val)[0];
} else {
$otherCalls[$key] = $val;
$methods[] = $key;
}
}
$mock = $mockBuilder->setMethods($methods)->getMock();
foreach ($onConsecutiveCalls as $method => $returns) {
$mock->expects($this->any())->method($method)
->will(
call_user_func_array(
[$this->testCase, 'onConsecutiveCalls'],
$returns
)
);
}
foreach ($otherCalls as $method => $return) {
if (is_object($return) && ($return instanceof PHPUnit_Framework_MockObject_Stub || $return instanceof PHPUnit\Framework\MockObject\Stub\Stub)) {
$mock->expects($this->any())->method($method)
->will($return);
}
elseif (is_object($return) && $return instanceof Closure) {
$mock->expects($this->any())->method($method)
->willReturnCallback($return);
}
elseif ($return === ':void') {
$mock->expects($this->any())->method($method);
}
else {
$mock->expects($this->any())->method($method)
->willReturn($return);
}
}
return $mock;
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I tried mocking the
get()
request fordb
object.I followed this sample code and works fine:
But it's quite wordy, so I tried to shorten it with
getDouble()
:But I got this error:
I wonder what's causing it since I correctly followed this guide to use the helper.
Thanks
EDIT
So I tested mocking something else with
getDouble
and get the same error. So it's not limited to db.I tried changing the class to this but get the same error:
I'm on PHP 7.4.30.
The text was updated successfully, but these errors were encountered: