This commit is contained in:
Marcel Pociot 2018-11-25 23:43:43 +01:00
parent 6a584c42c0
commit c822ea15a5
5 changed files with 121 additions and 15 deletions

View File

@ -35,7 +35,8 @@
"symfony/psr-http-message-bridge": "^1.1"
},
"require-dev": {
"larapack/dd": "^1.0",
"mockery/mockery": "^1.2",
"orchestra/testbench": "~3.5",
"phpunit/phpunit": "^7.0"
},
"autoload": {

51
tests/ConnectionTest.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests;
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException;
use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\PusherServer;
class ConnectionTest extends TestCase
{
/** @test */
public function unknown_app_keys_can_not_connect()
{
$this->expectException(UnknownAppKeyException::class);
/** @var PusherServer $server */
$server = app(PusherServer::class);
$server->onOpen($this->getWebSocketConnection('/?appKey=test'));
}
/** @test */
public function known_app_keys_can_connect()
{
/** @var PusherServer $server */
$server = app(PusherServer::class);
$connection = $this->getWebSocketConnection('/?appKey=TestKey');
$server->onOpen($connection);
$connection->assertSentEvent('pusher:connection_established');
}
/** @test */
public function successful_connections_have_the_client_attached()
{
/** @var PusherServer $server */
$server = app(PusherServer::class);
$connection = $this->getWebSocketConnection('/?appKey=TestKey');
$server->onOpen($connection);
$this->assertInstanceOf(Client::class, $connection->client);
$this->assertSame(1234, $connection->client->appId);
$this->assertSame('TestKey', $connection->client->appKey);
$this->assertSame('TestSecret', $connection->client->appSecret);
$this->assertSame('Test Client', $connection->client->name);
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/** @test */
public function true_is_true()
{
$this->assertTrue(true);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Mocks;
use GuzzleHttp\Psr7\Request;
use Ratchet\ConnectionInterface;
use PHPUnit\Framework\Assert as PHPUnit;
class Connection implements ConnectionInterface
{
/** @var Request */
public $httpRequest;
protected $sentData = [];
function send($data)
{
$this->sentData[] = json_decode($data, true);
}
function close()
{
// TODO: Implement close() method.
}
public function assertSentEvent(string $name)
{
PHPUnit::assertTrue(
! is_null(collect($this->sentData)->firstWhere('event', '=', $name))
);
}
}

36
tests/TestCase.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests;
use GuzzleHttp\Psr7\Request;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
use BeyondCode\LaravelWebSockets\LaravelWebSocketsServiceProvider;
abstract class TestCase extends \Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app)
{
return [LaravelWebSocketsServiceProvider::class];
}
protected function getEnvironmentSetUp($app)
{
$app['config']->set('websockets.clients', [
[
'name' => 'Test Client',
'app_id' => 1234,
'app_key' => 'TestKey',
'app_secret' => 'TestSecret'
]
]);
}
protected function getWebSocketConnection(string $url): Connection
{
$connection = new Connection();
$connection->httpRequest = new Request('GET', $url);
return $connection;
}
}