Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 456
Mark Avatar asked Jan 31 '10 11:01

Mark


1 Answers

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.

like image 164
John Kleijn Avatar answered Sep 23 '22 04:09

John Kleijn