Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit afa8af5

Browse files
committed
Added tests for send message
1 parent c6b3c07 commit afa8af5

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

src/Dashboard/Http/Controllers/SendMessage.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BeyondCode\LaravelWebSockets\Contracts\PushesToPusher;
66
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
7+
use Exception;
78
use Illuminate\Http\Request;
89

910
class SendMessage
@@ -33,12 +34,21 @@ public function __invoke(Request $request)
3334
'id' => $request->appId,
3435
]);
3536

36-
$broadcaster->broadcast(
37-
[$request->channel],
38-
$request->event,
39-
json_decode($request->data, true)
40-
);
37+
try {
38+
$broadcaster->broadcast(
39+
[$request->channel],
40+
$request->event,
41+
json_decode($request->data, true)
42+
);
43+
} catch (Exception $e) {
44+
return response()->json([
45+
'ok' => false,
46+
'exception' => $e->getMessage(),
47+
]);
48+
}
4149

42-
return 'ok';
50+
return response()->json([
51+
'ok' => true,
52+
]);
4353
}
4454
}

tests/Dashboard/SendMessageTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Dashboard;
4+
5+
use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger;
6+
use BeyondCode\LaravelWebSockets\Tests\TestCase;
7+
use BeyondCode\LaravelWebSockets\Tests\Models\User;
8+
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
9+
10+
class SendMessageTest extends TestCase
11+
{
12+
/** @test */
13+
public function can_send_message()
14+
{
15+
$this->skipOnRedisReplication();
16+
17+
// Because the Pusher server is not active,
18+
// we expect it to turn out ok: false.
19+
20+
$this->actingAs(factory(User::class)->create())
21+
->json('POST', route('laravel-websockets.event'), [
22+
'appId' => '1234',
23+
'key' => 'TestKey',
24+
'secret' => 'TestSecret',
25+
'channel' => 'test-channel',
26+
'event' => 'some-event',
27+
'data' => json_encode(['data' => 'yes']),
28+
])
29+
->seeJson([
30+
'ok' => false,
31+
]);
32+
}
33+
34+
/** @test */
35+
public function cant_send_message_for_invalid_app()
36+
{
37+
$this->skipOnRedisReplication();
38+
39+
// Because the Pusher server is not active,
40+
// we expect it to turn out ok: false.
41+
42+
$this->actingAs(factory(User::class)->create())
43+
->json('POST', route('laravel-websockets.event'), [
44+
'appId' => '9999',
45+
'key' => 'TestKey',
46+
'secret' => 'TestSecret',
47+
'channel' => 'test-channel',
48+
'event' => 'some-event',
49+
'data' => json_encode(['data' => 'yes']),
50+
])
51+
->assertResponseStatus(422);
52+
}
53+
}

0 commit comments

Comments
 (0)