phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Weird behavior in expects

Open log0 opened this issue 14 years ago • 7 comments

Hi all,

I met a bug in the test code below, which acts differently on the return values. Please take a look at the code below:

61 $dm = $this->getMock('ABC\TestBundle\Service\ListManager', array('getMyService'), array($this->container), '', true); 62 $dm->expects($this->exactly(1)) 63 ->method('getMyService') 64 ->will($this->returnValue($ms)); 65 # ->will($this->throwException(new \Exception("test")));

The method "getMyService" will invoke an exception with a message "should not come here" (as a test). In the above case, the exception "should not come here" is seen, DESPITE it should return the value and never invoke the code of "getMyService".

If I comment out line 44 and restore line 65, I will get exception "test".

I expect the "returnValue" should return the value I expected! Now I cannot test my code properly.

log0 avatar Apr 08 '12 12:04 log0

Without a reproduce case there is not much i can say or do. Sorry.

Feel free to reopen this with some executable code :)

edorian avatar Apr 11 '12 08:04 edorian

I will come back with a small test case, as I cannot release the source code of mine here. But I guarantee you that the issue does exist - it's buggy. =]

log0 avatar Apr 11 '12 08:04 log0

I wasn't trying to say that i don't believe you, just that there is nothing we can do without a reproduce case :)

It doesn't make much sense to keep issues open that can't be "solved" because we can't say "look, this is wrong, once this works the bug is fixed" :)

edorian avatar Apr 11 '12 17:04 edorian

Understandably, I appreciate your response. I tried to work on the repro case but it is quite difficult as it is coupled with Symfony2 / Doctrine2, which I am not sure if it is of relation. But to give some context, this is the simplified case which does not EXACTLY repro due to the function being a PRIVATE function.

In my extreme case, I can repro even in a PUBLIC function. AFAIK, phpunit seems to fail working at mocking private functions, but failing to mock at public functions give me a nice surprises. Now I cannot work on to unit test all my managers because they, unfortunately, are interlinked.

cat myclass.php

<?php

class MyClass
{
    private $magentoService;

    public function __construct()
    {
    }

    public function cancel()
    {
        return $this->getMagentoService();
    }

    private function getMagentoService()
    {
        if ( null == $this->magentoService )
        {
            $this->magentoService = 99;
        }

        return $this->magentoService;
    }
}

cat mytest.php

<?php

require('myclass.php');

class MyTest extends PHPUnit_Framework_TestCase
{
    public function testGetMethod()
    {
        $mc = $this->getMock('MyClass', array('getMagentoService'), array(), '', true);
        $mc->expects($this->exactly(1))
           ->method('getMagentoService')
           ->will($this->returnValue(48));
        $val = $mc->cancel();

        $this->assertEquals(48, $val);
    }
}
?>

// edit by @edorian: Fixed the formatting

log0 avatar Apr 11 '12 17:04 log0

Like you stated mocking a private function is not possible.

The case you showed works with a protected.

I can't make it fail using a public method.. If I head to guess I'd say autoloading issues (the class can't be found hence problems during mocking) but thats a rather blind guess.

<?php

class MyTest extends PHPUnit_Framework_TestCase
{
    public function testGetMethod()
    {   
        $mc = $this->getMock('MyClass', array('getMagentoService'), array(), '', true);
        $mc->expects($this->exactly(1))
           ->method('getMagentoService')
           ->will($this->returnValue(48));
        $val = $mc->cancel();

        $this->assertEquals(48, $val);
    }   
}

class MyClass
{
    private $magentoService;

    public function __construct() { } 

    public function cancel() {
        return $this->getMagentoService();
    }   

    protected function getMagentoService() {
        if ( null == $this->magentoService ) { 
            $this->magentoService = 99; 
        }   
        return $this->magentoService;
    }   
}

Saidly I can't help you here :)

edorian avatar Apr 11 '12 18:04 edorian

Autoloading ... hey, you know I really want to dive deep to help, could you give me some hints on how to start?

Moreover, I am seriously curious why private methods cannot be mocked in phpunit? I know that in Ruby, it can, and for Python it has no private methods... but, why? Is it technically impossible? Or just some principles of the designers here?

log0 avatar Apr 16 '12 03:04 log0

@log0 Private method testing:

[http://stackoverflow.com/a/5937899/285578](Apart from the fact that one really shouldn't do it) there are technical reasons too: The way mocking in php(unit) works is that the "mock" class extends the original class as php doesn't have a build-in-by-default-for-everyone monkey patching system (probably for the better as it's more of a classic OO language like java but thats a completly differnt discussion).

While ruby allows for something like this:

public *private_methods.collect(&:to_sym)

php only allows for

$method = new ReflectionMethod('Foo', 'myPrivateMethod');
$method->setAccessible(true);

and then the method can be called but still not accessable when used by a child class/from a test.

edorian avatar Apr 17 '12 21:04 edorian