From dc200278d644be1de8c99a17462aa9ef70cedc89 Mon Sep 17 00:00:00 2001 From: Charley Wu Date: Tue, 13 Oct 2020 04:38:47 +0800 Subject: [PATCH] Fixes connectionStrings parsing (#141) * travis has built-in redis-server 6.0.5 - make downloaded redis-server PATH priority higher then built-in one * Fixes connectionStrings parsing - Fixes port always be 6379 even no port specified in host string and specified the port number other then 6379 in constructor - Fixes persistent too * Added test cases - Code Cleanup Co-authored-by: Colin Mollenhour --- Client.php | 4 ++-- tests/CredisTest.php | 36 ++++++++++++------------------------ 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Client.php b/Client.php index e01f423..5bbb6f7 100755 --- a/Client.php +++ b/Client.php @@ -423,8 +423,8 @@ protected function convertHost() throw new CredisException('Invalid host format; expected '.$this->scheme.'://host[:port][/persistence_identifier]'); } $this->host = $matches[1]; - $this->port = (int) (isset($matches[3]) ? $matches[3] : 6379); - $this->persistent = isset($matches[5]) ? $matches[5] : ''; + $this->port = (int) (isset($matches[3]) ? $matches[3] : $this->port); + $this->persistent = isset($matches[5]) ? $matches[5] : $this->persistent; } else { $this->host = $matches[2]; $this->port = NULL; diff --git a/tests/CredisTest.php b/tests/CredisTest.php index ccd8a84..579c5bf 100644 --- a/tests/CredisTest.php +++ b/tests/CredisTest.php @@ -631,44 +631,32 @@ public function testGettersAndSetters() public function testConnectionStrings() { - $this->credis->close(); $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'] . ':' . $this->redisConfig[0]['port']); - if ($this->useStandalone) { - $this->credis->forceStandalone(); - } $this->assertEquals($this->credis->getHost(),$this->redisConfig[0]['host']); $this->assertEquals($this->credis->getPort(),$this->redisConfig[0]['port']); $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host']); - if ($this->useStandalone) { - $this->credis->forceStandalone(); - } - $this->assertEquals($this->credis->getPort(),$this->redisConfig[0]['port']); + $this->assertEquals($this->credis->getPort(),6379); $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'] . ':' . $this->redisConfig[0]['port'] . '/abc123'); - if ($this->useStandalone) { - $this->credis->forceStandalone(); - } - $this->assertEquals('abc123',$this->credis->getPersistence()); + $this->assertEquals($this->credis->getPersistence(),'abc123'); + $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'],6380); + $this->assertEquals($this->credis->getPort(),6380); + $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'],NULL,NULL,"abc123"); + $this->assertEquals($this->credis->getPersistence(),'abc123'); } public function testConnectionStringsTls() { - $this->credis->close(); $this->credis = new Credis_Client('tls://'.$this->redisConfig[0]['host'] . ':' . $this->redisConfig[0]['port']); - if ($this->useStandalone) { - $this->credis->forceStandalone(); - } $this->assertEquals($this->credis->getHost(),$this->redisConfig[0]['host']); $this->assertEquals($this->credis->getPort(),$this->redisConfig[0]['port']); $this->credis = new Credis_Client('tls://'.$this->redisConfig[0]['host']); - if ($this->useStandalone) { - $this->credis->forceStandalone(); - } - $this->assertEquals($this->credis->getPort(),$this->redisConfig[0]['port']); + $this->assertEquals($this->credis->getPort(),6379); $this->credis = new Credis_Client('tls://'.$this->redisConfig[0]['host'] . ':' . $this->redisConfig[0]['port'] . '/abc123'); - if ($this->useStandalone) { - $this->credis->forceStandalone(); - } - $this->assertEquals('abc123',$this->credis->getPersistence()); + $this->assertEquals($this->credis->getPersistence(),'abc123'); + $this->credis = new Credis_Client('tls://'.$this->redisConfig[0]['host'],6380); + $this->assertEquals($this->credis->getPort(),6380); + $this->credis = new Credis_Client('tls://'.$this->redisConfig[0]['host'],NULL,NULL,"abc123"); + $this->assertEquals($this->credis->getPersistence(),'abc123'); } /**