|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Tests\Transport\RedisExt; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Messenger\Exception\LogicException; |
| 16 | +use Symfony\Component\Messenger\Transport\RedisExt\Connection; |
| 17 | + |
| 18 | +/** |
| 19 | + * @requires extension redis |
| 20 | + */ |
| 21 | +class ConnectionTest extends TestCase |
| 22 | +{ |
| 23 | + public function testFromInvalidDsn() |
| 24 | + { |
| 25 | + $this->expectException(\InvalidArgumentException::class); |
| 26 | + $this->expectExceptionMessage('The given Redis DSN "redis://" is invalid.'); |
| 27 | + |
| 28 | + Connection::fromDsn('redis://'); |
| 29 | + } |
| 30 | + |
| 31 | + public function testFromDsn() |
| 32 | + { |
| 33 | + $this->assertEquals( |
| 34 | + new Connection(['stream' => 'queue'], [ |
| 35 | + 'host' => 'localhost', |
| 36 | + 'port' => 6379, |
| 37 | + ]), |
| 38 | + Connection::fromDsn('redis://localhost/queue') |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function testFromDsnWithOptions() |
| 43 | + { |
| 44 | + $this->assertEquals( |
| 45 | + new Connection(['stream' => 'queue', 'group' => 'group1', 'consumer' => 'consumer1'], [ |
| 46 | + 'host' => 'localhost', |
| 47 | + 'port' => 6379, |
| 48 | + ], [ |
| 49 | + 'blocking_timeout' => 30, |
| 50 | + ]), |
| 51 | + Connection::fromDsn('redis://localhost/queue/group1/consumer1', ['blocking_timeout' => 30]) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testFromDsnWithQueryOptions() |
| 56 | + { |
| 57 | + $this->assertEquals( |
| 58 | + new Connection(['stream' => 'queue', 'group' => 'group1', 'consumer' => 'consumer1'], [ |
| 59 | + 'host' => 'localhost', |
| 60 | + 'port' => 6379, |
| 61 | + ], [ |
| 62 | + 'blocking_timeout' => 30, |
| 63 | + ]), |
| 64 | + Connection::fromDsn('redis://localhost/queue/group1/consumer1?blocking_timeout=30') |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function testKeepGettingPendingMessages() |
| 69 | + { |
| 70 | + $redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock(); |
| 71 | + |
| 72 | + $redis->expects($this->exactly(3))->method('xreadgroup') |
| 73 | + ->with('symfony', 'consumer', ['queue' => 0], 1, null) |
| 74 | + ->willReturn(['queue' => [['message' => json_encode(['body' => 'Test', 'headers' => []])]]]); |
| 75 | + |
| 76 | + $connection = Connection::fromDsn('redis://localhost/queue', [], $redis); |
| 77 | + $this->assertNotNull($connection->get()); |
| 78 | + $this->assertNotNull($connection->get()); |
| 79 | + $this->assertNotNull($connection->get()); |
| 80 | + } |
| 81 | + |
| 82 | + public function testFirstGetPendingMessagesThenNewMessages() |
| 83 | + { |
| 84 | + $redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock(); |
| 85 | + |
| 86 | + $count = 0; |
| 87 | + |
| 88 | + $redis->expects($this->exactly(2))->method('xreadgroup') |
| 89 | + ->with('symfony', 'consumer', $this->callback(function ($arr_streams) use (&$count) { |
| 90 | + ++$count; |
| 91 | + |
| 92 | + if (1 === $count) { |
| 93 | + return '0' === $arr_streams['queue']; |
| 94 | + } |
| 95 | + |
| 96 | + return '>' === $arr_streams['queue']; |
| 97 | + }), 1, null) |
| 98 | + ->willReturn(['queue' => []]); |
| 99 | + |
| 100 | + $connection = Connection::fromDsn('redis://localhost/queue', [], $redis); |
| 101 | + $connection->get(); |
| 102 | + } |
| 103 | + |
| 104 | + public function testUnexpectedRedisError() |
| 105 | + { |
| 106 | + $this->expectException(LogicException::class); |
| 107 | + $this->expectExceptionMessage('Redis error happens'); |
| 108 | + $redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock(); |
| 109 | + $redis->expects($this->once())->method('xreadgroup')->willReturn(false); |
| 110 | + $redis->expects($this->once())->method('getLastError')->willReturn('Redis error happens'); |
| 111 | + |
| 112 | + $connection = Connection::fromDsn('redis://localhost/queue', [], $redis); |
| 113 | + $connection->get(); |
| 114 | + } |
| 115 | +} |
0 commit comments