|
| 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\TestCase; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
| 10 | + |
| 11 | +#[RequiresPhpExtension('redis')] |
| 12 | +class RedisBroadcasterTest extends TestCase |
| 13 | +{ |
| 14 | + use InteractsWithRedis; |
| 15 | + |
| 16 | + /** |
| 17 | + * @param string $driver |
| 18 | + */ |
| 19 | + #[DataProvider('redisDriverProvider')] |
| 20 | + #[RequiresPhpExtension('pcntl')] |
| 21 | + public function testBroadcast($driver) |
| 22 | + { |
| 23 | + $this->beforeApplicationDestroyed(function () { |
| 24 | + $this->tearDownRedis(); |
| 25 | + }); |
| 26 | + |
| 27 | + if ($pid = pcntl_fork() > 0) { |
| 28 | + $this->setUpRedis(); |
| 29 | + /** @var \Redis|\RedisCluster $redisClient */ |
| 30 | + $redisClient = $this->redis['phpredis']->client(); |
| 31 | + $redisClient->subscribe(['channel-1'], function ($redis, $channel, $message) { |
| 32 | + $redis->unsubscribe(['channel-1']); |
| 33 | + $redis->close(); |
| 34 | + $receivedPayload = json_decode($message, true); |
| 35 | + $this->assertEquals('test_channel-1', $channel); |
| 36 | + $this->assertEquals([ |
| 37 | + 'event' => 'test.event', |
| 38 | + 'data' => ['foo' => 'bar'], |
| 39 | + 'socket' => null, |
| 40 | + ], $receivedPayload); |
| 41 | + }); |
| 42 | + } elseif ($pid == 0) { |
| 43 | + $this->setUpRedis(); |
| 44 | + $redis = $this->redis[$driver]; |
| 45 | + $broadcaster = new RedisBroadcaster($redis, null, 'test_'); |
| 46 | + $channels = ['channel-1', 'channel-2']; |
| 47 | + usleep(1000); |
| 48 | + $broadcaster->broadcast($channels, 'test.event', ['foo' => 'bar']); |
| 49 | + exit; |
| 50 | + } else { |
| 51 | + $this->fail('Cannot fork'); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments