views:

258

answers:

1

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.

+1  A: 

Perhaps a bit late.

I had this exact same issue and it turned out the hinted type was not loaded and not loadable. As a result, PHP's ReflectionParameter doesn't return the class name, and PHPUnit's mock generator doesn't include it.

If you make sure the type hinted is either loaded or loadable using autoload, it should work.

John Kleijn