sebastianbergmann/phpunit — Issue #510: PHP5.4 callable typehint raises E_NOTICE when object is mocked
Author: rdlowrey
Created: 2012-02-26 21:40:55+00:00
State: open

When mocking an object with a method (or methods) whose signature uses the new PHP5.4 `callable` typehint, the resulting mock object's method signatures are not compatible with those of the original object.

This raises an `E_NOTICE` generally and if the class in question implements an interface that uses the `callable` typehint an `E_FATAL` because the resulting mock contains methods whose signatures don't match the interface specifications.

The `callable` typehint seems to create issues for the current implementation of `PHPUnit_Framework_MockObject_Generator::getObject` because it allows typehints that don't match an exact class name.

Here's the notice message when the class being mocked doesn't implement an interface that requires the `callable` typehint:

```
Declaration of Mock_Mediator_34e8a535::push() should be compatible with Mediator::push($eventName, callable $listener) in /usr/share/pear/PHPUnit/Framework/MockObject/Generator.php(218) : eval()'d code on line 1
```

And here's the fatal error traceback received when mocking an object whose methods use the `callable` typehint and implement an interface that requires its use:

```
Declaration of Mock_Mediator_cf65899f::push() must be compatible with MediatorInterface::push($eventName, callable $listener) in /usr/share/pear/PHPUnit/Framework/MockObject/Generator.php(218) : eval()'d code on line 1
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/pear/PHPUnit/TextUI/Command.php:130
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/pear/PHPUnit/TextUI/Command.php:192
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/TextUI/TestRunner.php:325
PHP 6. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:705
PHP 7. PHPUnit_Framework_TestSuite->runTest() /usr/share/pear/PHPUnit/Framework/TestSuite.php:745
PHP 8. PHPUnit_Framework_TestCase->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:772
PHP 9. PHPUnit_Framework_TestResult->run() /usr/share/pear/PHPUnit/Framework/TestCase.php:751
PHP 10. PHPUnit_Framework_TestCase->runBare() /usr/share/pear/PHPUnit/Framework/TestResult.php:649
PHP 11. PHPUnit_Framework_TestCase->runTest() /usr/share/pear/PHPUnit/Framework/TestCase.php:804
PHP 12. ReflectionMethod->invokeArgs() /usr/share/pear/PHPUnit/Framework/TestCase.php:942
PHP 13. NotifierTraitTest->testNotifySendsMessageToMediatorIfSet() /usr/share/pear/PHPUnit/Framework/TestCase.php:942
PHP 14. PHPUnit_Framework_TestCase->getMock() /mnt/data/dev/php/artax/test/src/artax/NotifierTraitTest.php:10
PHP 15. PHPUnit_Framework_MockObject_Generator::getMock() /usr/share/pear/PHPUnit/Framework/TestCase.php:1270
PHP 16. PHPUnit_Framework_MockObject_Generator::getObject() /usr/share/pear/PHPUnit/Framework/MockObject/Generator.php:203
PHP 17. eval() /usr/share/pear/PHPUnit/Framework/MockObject/Generator.php:218
```