我无法使用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;
}
答案 0 :(得分:1)
以下两种方法之一确实有效。
A)$client->subscribe($channels, function($r, $c, $m){});
B)$f = create_function($parms, $functionBody); $client->subscribe($channels, $f);