Pass by reference in a callback when mocking in PHPUnit

冷暖自知 提交于 2019-12-12 12:08:36

问题


I have an interface I want to mock, and mock the behaviour of one of it's methods.

So I have created a callback that mocks the behaviour very simply.

This test passes if I create a new object that is based on this interface, but I would like to mock the interface.

The mocked setUp method is being called fine, and calling getVar('testing') in my callback returns the value. However my assertion fails, because that value isn't available.

It seems that you can't do this in PHPUnit? Unless I am being stupid.

Brief explanation of the code flow; The code in "getVar" calls a method which calls the "setUp" on the added plugin. When it calls "setUp" it passes in "$this". It is $this I am expecting to be passed by reference and which works with a "real" object.

class DefaultRendererTest extends \PHPUnit_Framework_TestCase
{

    public function testSetGetVar()
    {
        $theme = $this->getMock('ThemeInterface');

        $plugin = $this->getMock('PluginInterface');
        $plugin->expects($this->once())
          ->method('setUp')
          ->will($this->returnCallback(function($r){

              $r->setVar('testing', "fooBar");

        }));

        $renderer = new DefaultRenderer($theme, null);
        $renderer->addPlugin($plugin);
        $this->assertEquals('fooBar',$renderer->getVar('testing'));
    }
}

For info here is the interface, the DefaultRenderer implements a RendererInterface

interface PluginInterface
{
    function setUp(RendererInterface $renderer);
}

回答1:


OK, out of interest, I tracked down the issue. It seems that PHPUnit automatically clones the parameters before the actual invocation takes place. I don't see a real reason for this, but maybe there is one. Taking a look at Framework/MockObject/Invocation/Static.php, there is only a single way how you can avoid this (on basis of the built in mock code): Implement a private __clone() method in the DefaultRenderer.

I'd also suggest you ask on IRC or the PHPUnit mailinglist about this behaviour or the mock object library.



来源:https://stackoverflow.com/questions/5170747/pass-by-reference-in-a-callback-when-mocking-in-phpunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!