Exemple #1
0
 public function subscribe()
 {
     $channels = new ChannelCollection();
     foreach (func_get_args() as $channel) {
         $channels->addChannel(new Channel($channel));
     }
     $message = new SubscribeMessage($channels);
     $this->subscriber->subscribe($message);
 }
 public function testSubscribeAndReceive()
 {
     $transport = new SocketTransport($_ENV['traffic_cop_host'], $_ENV['traffic_cop_port']);
     $subscriber = new Subscriber($transport);
     $channels = new ChannelCollection();
     $channels->addChannel(new Channel('channel_one'));
     $subscribeMessage = new SubscribeMessage($channels);
     $subscriber->subscribe($subscribeMessage);
     $test = new \TrafficCophp\Tests\Integration\PublisherTest();
     $test->testPublish();
     $subscriber->receive($message = new ServerMessage(new ChannelCollection()));
     $this->assertEquals('Hey there, im a message', $message->getMessage());
 }
 public function testReceive()
 {
     $transportMock = $this->getTransportMock();
     $transportMock->expects($this->exactly(1))->method('send');
     $channel = 'channel_one';
     $message = 'message';
     $buffer = new Buffer(4 + 11 + 7);
     $buffer->writeInt32BE(strlen($channel), 0);
     $buffer->write($channel, 4);
     $buffer->write($message, 4 + strlen($channel));
     // Create a map of arguments to return values.
     $map = array(array(4, 26), array(22, (string) $buffer));
     $transportMock->expects($this->exactly(2))->method('receive')->will($this->returnValueMap($map));
     $subscriber = new Subscriber($transportMock);
     $messageMock = $this->getMockBuilder('TrafficCophp\\Message\\SubscribeMessage')->disableOriginalConstructor()->getMock();
     $subscriber->subscribe($messageMock);
     $srvMessageMock = $this->getMockBuilder('TrafficCophp\\Message\\ServerMessage')->disableOriginalConstructor()->getMock();
     $srvMessageMock->expects($this->exactly(1))->method('parse');
     $subscriber->receive($srvMessageMock);
 }
<?php

require __DIR__ . '/../vendor/.composer/autoload.php';
use TrafficCophp\Network\SocketTransport;
use TrafficCophp\Publisher\Publisher;
use TrafficCophp\Subscriber\Subscriber;
use TrafficCophp\Message\SubscribeMessage;
use TrafficCophp\Message\PublishMessage;
use TrafficCophp\Channel\ChannelCollection;
use TrafficCophp\Channel\Channel;
use TrafficCophp\Message\ServerMessage;
$_ENV['traffic_cop_host'] = '127.0.0.1';
$_ENV['traffic_cop_port'] = 3542;
$transport = new SocketTransport($_ENV['traffic_cop_host'], $_ENV['traffic_cop_port']);
$subscriber = new Subscriber($transport);
$channels = new ChannelCollection();
$channels->addChannel(new Channel('channel_one'));
$subscribeMessage = new SubscribeMessage($channels);
$subscriber->subscribe($subscribeMessage);
$subscriber->receive($message = new ServerMessage(new ChannelCollection()));
var_dump($message);