|
| 1 | +<?php |
| 2 | +namespace Gt\Session\Test; |
| 3 | + |
| 4 | +use Gt\Session\RedisHandler; |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +class RedisHandlerTest extends TestCase { |
| 8 | + public function testOpenParsesStandardDsn():void { |
| 9 | + $client = new TestRedisClient(); |
| 10 | + $sut = new class($client) extends RedisHandler { |
| 11 | + public function __construct(private readonly TestRedisClient $client) {} |
| 12 | + |
| 13 | + protected function createClient():object { |
| 14 | + return $this->client; |
| 15 | + } |
| 16 | + }; |
| 17 | + |
| 18 | + $sut->open( |
| 19 | + "redis://default:secret@example.internal:25061/2?prefix=prod:session:&ttl=1800&timeout=1.5&read_timeout=2.5&persistent=1&persistent_id=pool-a", |
| 20 | + "GT", |
| 21 | + ); |
| 22 | + |
| 23 | + self::assertSame("example.internal", $client->connectParameters["host"]); |
| 24 | + self::assertSame(25061, $client->connectParameters["port"]); |
| 25 | + self::assertSame(1.5, $client->connectParameters["timeout"]); |
| 26 | + self::assertSame(2.5, $client->connectParameters["readTimeout"]); |
| 27 | + self::assertSame("pool-a", $client->connectParameters["persistentId"]); |
| 28 | + self::assertSame([["default", "secret"]], $client->authCalls); |
| 29 | + self::assertSame([2], $client->selectCalls); |
| 30 | + |
| 31 | + $sut->write("abc123", "payload"); |
| 32 | + self::assertSame("payload", $sut->read("abc123")); |
| 33 | + self::assertSame( |
| 34 | + [ |
| 35 | + "key" => "prod:session:abc123", |
| 36 | + "ttl" => 1800, |
| 37 | + "value" => "payload", |
| 38 | + ], |
| 39 | + $client->setExCalls[0], |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testOpenParsesTlsDsn():void { |
| 44 | + $client = new TestRedisClient(); |
| 45 | + $sut = new class($client) extends RedisHandler { |
| 46 | + public function __construct(private readonly TestRedisClient $client) {} |
| 47 | + |
| 48 | + protected function createClient():object { |
| 49 | + return $this->client; |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + $sut->open( |
| 54 | + "rediss://:secret@example.internal?verify_peer=0&verify_peer_name=0", |
| 55 | + "GT", |
| 56 | + ); |
| 57 | + $sut->write("abc123", "payload"); |
| 58 | + |
| 59 | + self::assertSame("tls://example.internal", $client->connectParameters["host"]); |
| 60 | + self::assertSame( |
| 61 | + [ |
| 62 | + "stream" => [ |
| 63 | + "verify_peer" => false, |
| 64 | + "verify_peer_name" => false, |
| 65 | + ], |
| 66 | + ], |
| 67 | + $client->connectParameters["context"], |
| 68 | + ); |
| 69 | + self::assertSame(["secret"], $client->authCalls); |
| 70 | + self::assertSame("payload", $client->data["GT:abc123"]); |
| 71 | + } |
| 72 | + |
| 73 | + public function testDestroyAndClose():void { |
| 74 | + $client = new TestRedisClient(); |
| 75 | + $sut = new class($client) extends RedisHandler { |
| 76 | + public function __construct(private readonly TestRedisClient $client) {} |
| 77 | + |
| 78 | + protected function createClient():object { |
| 79 | + return $this->client; |
| 80 | + } |
| 81 | + }; |
| 82 | + $sut->open("redis://cache.internal", "GT"); |
| 83 | + $sut->write("abc123", "payload"); |
| 84 | + |
| 85 | + self::assertTrue($sut->destroy("abc123")); |
| 86 | + self::assertSame("", $sut->read("abc123")); |
| 87 | + self::assertTrue($sut->close()); |
| 88 | + self::assertTrue($client->closed); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class TestRedisClient { |
| 93 | + /** @var array<string,mixed> */ |
| 94 | + public array $connectParameters = []; |
| 95 | + /** @var array<int,array{key:string,ttl:int,value:string}> */ |
| 96 | + public array $setExCalls = []; |
| 97 | + /** @var array<int,string> */ |
| 98 | + public array $setCalls = []; |
| 99 | + /** @var array<int,array|string> */ |
| 100 | + public array $authCalls = []; |
| 101 | + /** @var array<int,int> */ |
| 102 | + public array $selectCalls = []; |
| 103 | + /** @var array<string,string> */ |
| 104 | + public array $data = []; |
| 105 | + public int $deleted = 0; |
| 106 | + public bool $closed = false; |
| 107 | + |
| 108 | + public function connect( |
| 109 | + string $host, |
| 110 | + int $port, |
| 111 | + float $timeout = 0, |
| 112 | + ?string $persistentId = null, |
| 113 | + int $retryInterval = 0, |
| 114 | + float $readTimeout = 0, |
| 115 | + ?array $context = null, |
| 116 | + ):bool { |
| 117 | + $this->connectParameters = [ |
| 118 | + "host" => $host, |
| 119 | + "port" => $port, |
| 120 | + "timeout" => $timeout, |
| 121 | + "persistentId" => $persistentId, |
| 122 | + "retryInterval" => $retryInterval, |
| 123 | + "readTimeout" => $readTimeout, |
| 124 | + "context" => $context, |
| 125 | + ]; |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + public function auth(array|string $credentials):bool { |
| 130 | + $this->authCalls []= $credentials; |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + public function select(int $database):bool { |
| 135 | + $this->selectCalls []= $database; |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + public function get(string $key):string|false { |
| 140 | + return $this->data[$key] ?? false; |
| 141 | + } |
| 142 | + |
| 143 | + public function set(string $key, string $value):bool { |
| 144 | + $this->setCalls []= $key; |
| 145 | + $this->data[$key] = $value; |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + public function setEx(string $key, int $ttl, string $value):bool { |
| 150 | + $this->setExCalls []= [ |
| 151 | + "key" => $key, |
| 152 | + "ttl" => $ttl, |
| 153 | + "value" => $value, |
| 154 | + ]; |
| 155 | + $this->data[$key] = $value; |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + public function del(string $key):int { |
| 160 | + unset($this->data[$key]); |
| 161 | + return ++$this->deleted; |
| 162 | + } |
| 163 | + |
| 164 | + public function close():bool { |
| 165 | + $this->closed = true; |
| 166 | + return true; |
| 167 | + } |
| 168 | +} |
0 commit comments