Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockery - call_user_func_array() expects parameter 1 to be a valid callback

I have a class I need to mock:

class MessagePublisher
{
    /**
     * @param \PhpAmqpLib\Message\AMQPMessage $msg
     * @param string $exchange - if not provided then one passed in constructor is used
     * @param string $routing_key
     * @param bool $mandatory
     * @param bool $immediate
     * @param null $ticket
     */
    public function publish(AMQPMessage $msg, $exchange = "", $routing_key = "", $mandatory = false, $immediate = false, $ticket = null)
    {
        if (empty($exchange)) {
            $exchange = $this->exchangeName;
        }

        $this->channel->basic_publish($msg, $exchange, $routing_key, $mandatory, $immediate, $ticket);
    }
}

I am using Mockery 0.7.2

$mediaPublisherMock = \Mockery::mock('MessagePublisher')
    ->shouldReceive('publish')
    ->withAnyArgs()
    ->times(3)
    ->andReturn(null);

unfortunately my tests failed, due to this error

call_user_func_array() expects parameter 1 to be a valid callback, class 'Mockery\Expectation' does not have a method 'publish' in /vendor/mockery/mockery/library/Mockery/CompositeExpectation.php on line 54

I have tried to debug I found that tests fails in this code

public function __call($method, array $args)
{
    foreach ($this->_expectations as $expectation) {
        call_user_func_array(array($expectation, $method), $args);
    }
    return $this;
}

where
$method = 'publish'
$args = array()
$expectation is instance of Mockery\Expectation object ()

I am using php 5.3.10 - any idea what is wrong?

like image 991
mrok Avatar asked Oct 13 '12 20:10

mrok


2 Answers

This is happening because you are assigning a mock expectation to $mediaPublisherMock, rather than the mock itself. Try adding the getMock method to the end of that call, like:

$mediaPublisherMock = \Mockery::mock('MessagePublisher')
    ->shouldReceive('publish')
    ->withAnyArgs()
    ->times(3)
    ->andReturn(null)
    ->getMock();
like image 94
Dave Marshall Avatar answered Oct 17 '22 02:10

Dave Marshall


Ok problem solved by using standard PhpUnit Mock library

This works:

$mediaPublisherMock = $this->getMock('Mrok\Model\MessagePublisher', array('publish'), array(), '', false);
$mediaPublisherMock->expects($this->once())
    ->method('publish');

Why I did not start from this ;)

like image 25
mrok Avatar answered Oct 17 '22 01:10

mrok