Skip to content

Commit f9c6241

Browse files
committed
[12.x] Redis broadcaster integration test
1 parent 3c02d28 commit f9c6241

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Broadcasting;
4+
5+
use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster;
6+
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
7+
use Orchestra\Testbench\Attributes\RequiresEnv;
8+
use Orchestra\Testbench\TestCase;
9+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
10+
11+
#[RequiresPhpExtension('redis')]
12+
#[RequiresEnv('REDIS_CLIENT')]
13+
class RedisBroadcasterTest extends TestCase
14+
{
15+
use InteractsWithRedis;
16+
17+
#[RequiresPhpExtension('pcntl')]
18+
public function testBroadcast()
19+
{
20+
$this->beforeApplicationDestroyed(function () {
21+
$this->tearDownRedis();
22+
});
23+
24+
if ($pid = pcntl_fork() > 0) {
25+
$this->setUpRedis();
26+
/** @var \Redis|\RedisCluster $redisClient */
27+
$redisClient = $this->redis['phpredis']->client();
28+
$redisClient->subscribe(['channel-1'], function ($redis, $channel, $message) {
29+
$redis->unsubscribe(['channel-1']);
30+
$redis->close();
31+
$receivedPayload = json_decode($message, true);
32+
$this->assertEquals('test_channel-1', $channel);
33+
$this->assertEquals([
34+
'event' => 'test.event',
35+
'data' => ['foo' => 'bar'],
36+
'socket' => null,
37+
], $receivedPayload);
38+
});
39+
} elseif ($pid == 0) {
40+
$this->setUpRedis();
41+
$redis = $this->app['redis'];
42+
$broadcaster = new RedisBroadcaster($redis, null, 'test_');
43+
$channels = ['channel-1', 'channel-2'];
44+
usleep(1000);
45+
$broadcaster->broadcast($channels, 'test.event', ['foo' => 'bar']);
46+
exit;
47+
} else {
48+
$this->fail('Cannot fork');
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)