|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypervel\Tests\Core; |
| 6 | + |
| 7 | +use FriendsOfHyperf\Redis\Subscriber\Exception\SocketException; |
| 8 | +use FriendsOfHyperf\Redis\Subscriber\Subscriber as RedisSubscriber; |
| 9 | +use Hypervel\Redis\Subscriber; |
| 10 | +use Hypervel\Tests\TestCase; |
| 11 | +use Mockery as m; |
| 12 | +use stdClass; |
| 13 | + |
| 14 | +/** |
| 15 | + * @internal |
| 16 | + * @covers \Hypervel\Redis\Subscriber |
| 17 | + */ |
| 18 | +class RedisSubscriberTest extends TestCase |
| 19 | +{ |
| 20 | + public function testSubscribe() |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + $config = ['host' => 'localhost']; |
| 24 | + $channels = ['channel1', 'channel2']; |
| 25 | + $mockRedisSubscriber = m::mock(RedisSubscriber::class); |
| 26 | + $mockChannel = m::mock('stdClass'); |
| 27 | + |
| 28 | + // Set up the channel to return a message once and then null |
| 29 | + $message = new stdClass(); |
| 30 | + $message->payload = 'test-payload'; |
| 31 | + $message->channel = 'channel1'; |
| 32 | + |
| 33 | + $mockChannel->shouldReceive('pop') |
| 34 | + ->once() |
| 35 | + ->andReturn($message); |
| 36 | + $mockChannel->shouldReceive('pop') |
| 37 | + ->once() |
| 38 | + ->andReturnNull(); |
| 39 | + |
| 40 | + $mockRedisSubscriber->shouldReceive('subscribe') |
| 41 | + ->once() |
| 42 | + ->with(...$channels); |
| 43 | + |
| 44 | + $mockRedisSubscriber->shouldReceive('channel') |
| 45 | + ->twice() |
| 46 | + ->andReturn($mockChannel); |
| 47 | + |
| 48 | + $mockRedisSubscriber->closed = true; |
| 49 | + |
| 50 | + $subscriber = new Subscriber($config, $mockRedisSubscriber); |
| 51 | + |
| 52 | + $callbackCalled = false; |
| 53 | + $callback = function ($payload, $channel) use (&$callbackCalled, $message) { |
| 54 | + $callbackCalled = true; |
| 55 | + $this->assertEquals($message->payload, $payload); |
| 56 | + $this->assertEquals($message->channel, $channel); |
| 57 | + }; |
| 58 | + |
| 59 | + // Act |
| 60 | + $subscriber->subscribe($channels, $callback); |
| 61 | + |
| 62 | + // Assert |
| 63 | + $this->assertTrue($callbackCalled); |
| 64 | + } |
| 65 | + |
| 66 | + public function testSubscribeThrowsExceptionWhenConnectionClosedAbnormally() |
| 67 | + { |
| 68 | + // Arrange |
| 69 | + $config = ['host' => 'localhost']; |
| 70 | + $channels = ['channel1']; |
| 71 | + $mockRedisSubscriber = m::mock(RedisSubscriber::class); |
| 72 | + $mockChannel = m::mock('stdClass'); |
| 73 | + |
| 74 | + $mockChannel->shouldReceive('pop') |
| 75 | + ->once() |
| 76 | + ->andReturnNull(); |
| 77 | + |
| 78 | + $mockRedisSubscriber->shouldReceive('subscribe') |
| 79 | + ->once() |
| 80 | + ->with(...$channels); |
| 81 | + |
| 82 | + $mockRedisSubscriber->shouldReceive('channel') |
| 83 | + ->once() |
| 84 | + ->andReturn($mockChannel); |
| 85 | + |
| 86 | + $mockRedisSubscriber->closed = false; |
| 87 | + |
| 88 | + $subscriber = new Subscriber($config, $mockRedisSubscriber); |
| 89 | + |
| 90 | + $callback = function () { |
| 91 | + // Empty callback |
| 92 | + }; |
| 93 | + |
| 94 | + // Assert & Act |
| 95 | + $this->expectException(SocketException::class); |
| 96 | + $this->expectExceptionMessage('Redis connection is disconnected abnormally.'); |
| 97 | + |
| 98 | + $subscriber->subscribe($channels, $callback); |
| 99 | + } |
| 100 | + |
| 101 | + public function testPsubscribe() |
| 102 | + { |
| 103 | + // Arrange |
| 104 | + $config = ['host' => 'localhost']; |
| 105 | + $patterns = ['channel*']; |
| 106 | + $mockRedisSubscriber = m::mock(RedisSubscriber::class); |
| 107 | + $mockChannel = m::mock('stdClass'); |
| 108 | + |
| 109 | + // Set up the channel to return a message once and then null |
| 110 | + $message = new stdClass(); |
| 111 | + $message->payload = 'test-payload'; |
| 112 | + $message->channel = 'channel1'; |
| 113 | + |
| 114 | + $mockChannel->shouldReceive('pop') |
| 115 | + ->once() |
| 116 | + ->andReturn($message); |
| 117 | + $mockChannel->shouldReceive('pop') |
| 118 | + ->once() |
| 119 | + ->andReturnNull(); |
| 120 | + |
| 121 | + $mockRedisSubscriber->shouldReceive('psubscribe') |
| 122 | + ->once() |
| 123 | + ->with(...$patterns); |
| 124 | + |
| 125 | + $mockRedisSubscriber->shouldReceive('channel') |
| 126 | + ->twice() |
| 127 | + ->andReturn($mockChannel); |
| 128 | + |
| 129 | + $mockRedisSubscriber->closed = true; |
| 130 | + |
| 131 | + $subscriber = new Subscriber($config, $mockRedisSubscriber); |
| 132 | + |
| 133 | + $callbackCalled = false; |
| 134 | + $callback = function ($payload, $channel) use (&$callbackCalled, $message) { |
| 135 | + $callbackCalled = true; |
| 136 | + $this->assertEquals($message->payload, $payload); |
| 137 | + $this->assertEquals($message->channel, $channel); |
| 138 | + }; |
| 139 | + |
| 140 | + // Act |
| 141 | + $subscriber->psubscribe($patterns, $callback); |
| 142 | + |
| 143 | + // Assert |
| 144 | + $this->assertTrue($callbackCalled); |
| 145 | + } |
| 146 | + |
| 147 | + public function testPsubscribeThrowsExceptionWhenConnectionClosedAbnormally() |
| 148 | + { |
| 149 | + // Arrange |
| 150 | + $config = ['host' => 'localhost']; |
| 151 | + $patterns = ['channel*']; |
| 152 | + $mockRedisSubscriber = m::mock(RedisSubscriber::class); |
| 153 | + $mockChannel = m::mock('stdClass'); |
| 154 | + |
| 155 | + $mockChannel->shouldReceive('pop') |
| 156 | + ->once() |
| 157 | + ->andReturnNull(); |
| 158 | + |
| 159 | + $mockRedisSubscriber->shouldReceive('psubscribe') |
| 160 | + ->once() |
| 161 | + ->with(...$patterns); |
| 162 | + |
| 163 | + $mockRedisSubscriber->shouldReceive('channel') |
| 164 | + ->once() |
| 165 | + ->andReturn($mockChannel); |
| 166 | + |
| 167 | + $mockRedisSubscriber->closed = false; |
| 168 | + |
| 169 | + $subscriber = new Subscriber($config, $mockRedisSubscriber); |
| 170 | + |
| 171 | + $callback = function () { |
| 172 | + // Empty callback |
| 173 | + }; |
| 174 | + |
| 175 | + // Assert & Act |
| 176 | + $this->expectException(SocketException::class); |
| 177 | + $this->expectExceptionMessage('Redis connection is disconnected abnormally.'); |
| 178 | + |
| 179 | + $subscriber->psubscribe($patterns, $callback); |
| 180 | + } |
| 181 | +} |
0 commit comments