diff --git a/src/Illuminate/Database/Connectors/PostgresConnector.php b/src/Illuminate/Database/Connectors/PostgresConnector.php index c54163f9b89f..2d68a205a87c 100755 --- a/src/Illuminate/Database/Connectors/PostgresConnector.php +++ b/src/Illuminate/Database/Connectors/PostgresConnector.php @@ -167,13 +167,14 @@ protected function getDsn(array $config) // name than the database used for "information_schema" queries. This is // typically the case if using "pgbouncer" type software when pooling. $database = $connect_via_database ?? $database; + $port = $connect_via_port ?? $port ?? null; $dsn = "pgsql:{$host}dbname='{$database}'"; // If a port was specified, we will add it to this Postgres DSN connections // format. Once we have done that we are ready to return this connection // string back out for usage, as this has been fully constructed here. - if (isset($config['port'])) { + if ($port !== null) { $dsn .= ";port={$port}"; } diff --git a/tests/Database/DatabaseConnectorTest.php b/tests/Database/DatabaseConnectorTest.php index 24f6bc326842..79ee6d0606c4 100755 --- a/tests/Database/DatabaseConnectorTest.php +++ b/tests/Database/DatabaseConnectorTest.php @@ -232,6 +232,22 @@ public function testPostgresApplicationUseAlternativeDatabaseName() $this->assertSame($result, $connection); } + public function testPostgresApplicationUseAlternativeDatabaseNameAndPort() + { + $dsn = 'pgsql:dbname=\'baz\';port=2345'; + $config = ['database' => 'bar', 'connect_via_database' => 'baz', 'port' => 5432, 'connect_via_port' => 2345]; + $connector = $this->getMockBuilder(PostgresConnector::class)->onlyMethods(['createConnection', 'getOptions'])->getMock(); + $connection = m::mock(stdClass::class); + $connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->willReturn(['options']); + $connector->expects($this->once())->method('createConnection')->with($this->equalTo($dsn), $this->equalTo($config), $this->equalTo(['options']))->willReturn($connection); + $statement = m::mock(PDOStatement::class); + $connection->shouldReceive('prepare')->zeroOrMoreTimes()->andReturn($statement); + $statement->shouldReceive('execute')->zeroOrMoreTimes(); + $result = $connector->connect($config); + + $this->assertSame($result, $connection); + } + public function testPostgresConnectorReadsIsolationLevelFromConfig() { $dsn = 'pgsql:host=foo;dbname=\'bar\';port=111';