0

我无法使用 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;
}
4

1 回答 1

1

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

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

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

于 2014-08-07T09:52:53.690 回答