This commit is contained in:
Marcel Pociot 2018-11-26 00:14:50 +01:00
parent c822ea15a5
commit fb71f41e05
4 changed files with 42 additions and 4 deletions

View File

@ -32,7 +32,7 @@ class PusherMessage implements RespondableMessage
$eventName = camel_case(str_after($this->payload->event, ':'));
if (method_exists($this, $eventName)) {
call_user_func([$this, $eventName], $this->connection, $this->payload->data);
call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass());
}
}

View File

@ -5,6 +5,8 @@ namespace BeyondCode\LaravelWebSockets\Tests;
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException;
use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\PusherServer;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use Ratchet\RFC6455\Messaging\MessageInterface;
class ConnectionTest extends TestCase
{
@ -25,7 +27,7 @@ class ConnectionTest extends TestCase
/** @var PusherServer $server */
$server = app(PusherServer::class);
$connection = $this->getWebSocketConnection('/?appKey=TestKey');
$connection = $this->getWebSocketConnection();
$server->onOpen($connection);
@ -38,7 +40,7 @@ class ConnectionTest extends TestCase
/** @var PusherServer $server */
$server = app(PusherServer::class);
$connection = $this->getWebSocketConnection('/?appKey=TestKey');
$connection = $this->getWebSocketConnection();
$server->onOpen($connection);
@ -48,4 +50,21 @@ class ConnectionTest extends TestCase
$this->assertSame('TestSecret', $connection->client->appSecret);
$this->assertSame('Test Client', $connection->client->name);
}
/** @test */
public function ping_returns_pong()
{
/** @var PusherServer $server */
$server = app(PusherServer::class);
$connection = $this->getWebSocketConnection();
$message = new Message('{"event": "pusher:ping"}');
$server->onOpen($connection);
$server->onMessage($connection, $message);
$connection->assertSentEvent('pusher:pong');
}
}

19
tests/Mocks/Message.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Mocks;
class Message extends \Ratchet\RFC6455\Messaging\Message
{
protected $payload;
public function __construct($payload)
{
$this->payload = $payload;
}
public function getPayload()
{
return $this->payload;
}
}

View File

@ -25,7 +25,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
]);
}
protected function getWebSocketConnection(string $url): Connection
protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection
{
$connection = new Connection();