Creating Mock object of Interface with type-hint in method fails on PHPUnit

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-01-31T11:02:39Z Indexed on 2010/03/17 16:11 UTC
Read the original article Hit count: 360

Filed under:
|
|
|

I created the following interface:

<?php
interface Action
{
    public function execute(\requests\Request $request, array $params);
}

Then I try to make a Mock object of this interface with PHPUnit 3.4, but I get the following error:

Fatal error: Declaration of Mock_Action_b389c0b1::execute() must be compatible with that of Action::execute() in D:\Xampp\xampp\php\PEAR\PHPUnit\Framework\TestCase.php(1121) : eval()'d code on line 2

I looked through the stack trace I got from PHPUnit and found that it creates a Mock object that implements the interface Action, but creates the execute method in the following way:

<?php
public function execute($request, array $params)

As you can see, PHPUnit takes over the array type-hint, but forgets about \requests\Request. Which obviously leads to an error. Does anyone knows a workaround for this error?

I also tried it without namespaces, but I still get the same error.

© Stack Overflow or respective owner

Related posts about phpunit

Related posts about mock