Added tests for send message

This commit is contained in:
Alex Renoki 2020-08-24 09:03:52 +03:00
parent c6b3c07510
commit afa8af5ddc
2 changed files with 69 additions and 6 deletions

View File

@ -4,6 +4,7 @@ namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
use BeyondCode\LaravelWebSockets\Contracts\PushesToPusher; use BeyondCode\LaravelWebSockets\Contracts\PushesToPusher;
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class SendMessage class SendMessage
@ -33,12 +34,21 @@ class SendMessage
'id' => $request->appId, 'id' => $request->appId,
]); ]);
try {
$broadcaster->broadcast( $broadcaster->broadcast(
[$request->channel], [$request->channel],
$request->event, $request->event,
json_decode($request->data, true) json_decode($request->data, true)
); );
} catch (Exception $e) {
return response()->json([
'ok' => false,
'exception' => $e->getMessage(),
]);
}
return 'ok'; return response()->json([
'ok' => true,
]);
} }
} }

View File

@ -0,0 +1,53 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Dashboard;
use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use BeyondCode\LaravelWebSockets\Tests\Models\User;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
class SendMessageTest extends TestCase
{
/** @test */
public function can_send_message()
{
$this->skipOnRedisReplication();
// Because the Pusher server is not active,
// we expect it to turn out ok: false.
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.event'), [
'appId' => '1234',
'key' => 'TestKey',
'secret' => 'TestSecret',
'channel' => 'test-channel',
'event' => 'some-event',
'data' => json_encode(['data' => 'yes']),
])
->seeJson([
'ok' => false,
]);
}
/** @test */
public function cant_send_message_for_invalid_app()
{
$this->skipOnRedisReplication();
// Because the Pusher server is not active,
// we expect it to turn out ok: false.
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.event'), [
'appId' => '9999',
'key' => 'TestKey',
'secret' => 'TestSecret',
'channel' => 'test-channel',
'event' => 'some-event',
'data' => json_encode(['data' => 'yes']),
])
->assertResponseStatus(422);
}
}