我在使用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;
}
以下两种方法中的一种确实有效。
A( $client->subscribe($channels, function($r, $c, $m){});
B( $f = create_function($parms, $functionBody); $client->subscribe($channels, $f);