phpredis Redis :: subscribe()期望参数2是有效的回调

时间:2014-07-31 16:47:26

标签: php redis phpredis

我无法使用subscribe方法。欢迎任何帮助,以使其工作。以下php单元测试给出了以下错误。

这是phpredis提供的示例。

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;
}

1 个答案:

答案 0 :(得分:1)

以下两种方法之一确实有效。

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

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