Pass by reference in callback when taunted in PHPUnit

I have an interface that I want to make fun of, and make fun of the behavior of one of its methods.

So, I created a callback that very perversely behaves.

This test passes if I create a new object based on this interface, but I would like to make fun of the interface.

The cheated setUp method is fined, and the getVar ('testing') call in my callback returns a value. However, my statement fails because this value is not available.

It seems you can't do it in PHPUnit? If I'm not stupid.

A brief description of the code stream; The code in "getVar" calls a method that calls "setUp" in the added plugin. When it calls "setUp", it goes to "$ this". This is $ this, which I expect to pass 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 information, here is the interface, DefaultRenderer implements RendererInterface

interface PluginInterface
{
    function setUp(RendererInterface $renderer);
}
+3
source share
1 answer

OK, out of interest, I figured out the problem. It seems that PHPUnit automatically clones the parameters before the actual call. I see no real reason for this, but perhaps there is one. Taking a look at Framework/MockObject/Invocation/Static.php, there is only one way you can avoid this (based on the built-in code layout): Add a private __clone()method to DefaultRenderer.

IRC PHPUnit .

+4

Source: https://habr.com/ru/post/1795789/


All Articles