public function testRemoveChannel()
 {
     $chn = new Channel('my-channel');
     $this->channels->add($chn);
     $this->assertTrue($this->channels->exists('my-channel'));
     $this->channels->remove('my-channel');
     $this->assertFalse($this->channels->exists('my-channel'));
 }
 /**
  * @inheritdoc
  */
 public function run(ConnectionInterface $from, \stdClass $package)
 {
     $channelName = $package->channel;
     if (!$this->channels->exists($channelName)) {
         $chn = new Channel($channelName);
         $this->channels->add($chn);
     }
     $this->channels->get($channelName)->addConnection($from);
 }
 /**
  * @inheritdoc
  */
 public function run(ConnectionInterface $from, \stdClass $package)
 {
     $channelName = $package->channel;
     $fromName = $package->from;
     $message = $package->message;
     // can't send to non-existent channels
     if (!$this->channels->exists($channelName)) {
         return;
     }
     $chn = $this->channels->get($channelName);
     // can't send to channels you're not apart of
     if (!$chn->connectionExists($from)) {
         return;
     }
     // this json-encoded string will be sent over the socket
     $outgoing = json_encode(['command' => $this->getName(), 'channel' => $channelName, 'message' => $message, 'from' => $fromName, 'timestamp' => time()]);
     foreach ($chn->getConnections() as $conn) {
         $conn->send($outgoing);
     }
 }
 public function testChannelIsRemovedWhenEmpty()
 {
     $package = (object) ['channel' => 'my-channel'];
     $this->command->run($this->connection, $package);
     $this->assertFalse($this->channels->exists('my-channel'));
 }
 public function testAddsChannelIfItDoesNotExist()
 {
     $package = (object) ['channel' => 'my-channel'];
     $this->command->run($this->connection, $package);
     $this->assertTrue($this->channels->exists('my-channel'));
 }