Marco
Marco

Reputation: 5109

phpredis Redis::subscribe() expects parameter 2 to be a valid callback

I'm having trouble to use the subscribe method. Any help would be welcome in order to get it working. Following php unit test gives me the following error.

This is the example as phpredis provides it.

https://github.com/nicolasff/phpredis#subscribe

Redis::subscribe() expects parameter 2 to be a valid callback, function 'f' not found or invalid function name

/myproj/test/RedisEventBusTest.php:37

RedisEventBusTest.php

<?php

namespace bkcqrs\eventing;

use bkcqrs\DefaultDomainEvent;
use bkcqrs\serializing\JsonSerializer;

class RedisEventBusTest extends \PHPUnit_Framework_TestCase
{
    private $redisEventBus;
    private $redisSubscribeClient;

    public function setUp()
    {
        $serializer = new JsonSerializer();
        $this->redisEventBus = new RedisEventBus($serializer);
        $this->redisSubscribeClient = new \Redis();
    }

    public function it_publishes_the_event_on_redis()
    {
        $notificationCountChangedEvent = new NotificationCountChangedEvent(array('userId' => 6, 'count' => 21));
        function f($redis, $channel, $msg)
        {
            var_dump($msg);
            $notification = json_decode($msg);
            var_dump($notification);

            $this->assertEquals($notification["userId"], $notificationCountChangedEvent->userId);
            $this->assertEquals($notification["count"], $notificationCountChangedEvent->count);
        }

        $this->redisSubscribeClient->subscribe(array("NotificationCountChanged"), 'f');
        $this->redisEventBus->publish($notificationCountChangedEvent);
    }
}

class NotificationCountChangedEvent extends DefaultDomainEvent
{
    public $userId;
    public $count;
}

Upvotes: 0

Views: 2008

Answers (1)

Marco
Marco

Reputation: 5109

One of the following 2 approaches does work.

A) $client->subscribe($channels, function($r, $c, $m){});

B) $f = create_function($parms, $functionBody); $client->subscribe($channels, $f);

Upvotes: 1

Related Questions