vincent15000's avatar

I need to test if a particular method is called

Hello,

I'm using RabbitMQ and I dispatch de messages to a specific method inside a specific class according to the routing key of the RabbitMQ message.

I need to write a test to check that according to the routing key, it's the right method in the right class that is executed.

First I thought it was possible to do that with mocking, but I'm not sure that mocking is the solution.

How can I do that ?

Here is the message handling class.

class MessageHandler
{
    protected $strategies = [];

    public function __construct()
    {
        $this->strategies['endpoint'] = new EndpointMessageHandler;
        $this->strategies['infrastructure'] = new InfrastructureMessageHandler;
    }

    public function handle($body)
    {
        $validator = new MessageValidator($body);

        if ($validator->validate()) {
            return $this->strategies[$body->family]->handle($message);
        } else {
            (new RabbitMQService)->sendMessage('MySuperExchange', 'topic', 'message.error', json_encode($validator->errors));
        }
    }
}

For example what I want is to check that if family is endpoint, then it's the handle method of the EndpointMessageHandler that is called.

Thanks for your help.

V

0 likes
0 replies

Please or to participate in this conversation.