Skip to content

Commit

Permalink
Fix parse vhost from dsn
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Shabouta authored and zloyuser committed Jun 24, 2019
1 parent 6d66390 commit dc608fe
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,22 @@ final class Config
/**
* @param string $host
* @param int $port
* @param string $vhost
* @param string $user
* @param string $pass
* @param string $vhost
*/
public function __construct(string $host, int $port, string $vhost = null, string $user = null, string $pass = null)
{
public function __construct(
string $host = self::DEFAULT_HOST,
int $port = self::DEFAULT_PORT,
string $user = self::DEFAULT_USER,
string $pass = self::DEFAULT_PASS,
string $vhost = null
) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->pass = $pass;
$this->vhost = $vhost ?: self::DEFAULT_VHOST;
$this->user = $user ?: self::DEFAULT_USER;
$this->pass = $pass ?: self::DEFAULT_PASS;
}

/**
Expand All @@ -133,12 +138,16 @@ public static function parse(string $dsn): self

$options = \array_replace(self::OPTIONS, $query);

if (!empty($parts['path'])) {
$parts['path'] = \substr($parts['path'], 1);
}

$self = new self(
$parts['host'] ?? self::DEFAULT_HOST,
$parts['port'] ?? self::DEFAULT_PORT,
$parts['path'] ?? self::DEFAULT_VHOST,
$parts['user'] ?? self::DEFAULT_USER,
$parts['pass'] ?? self::DEFAULT_PASS
$parts['pass'] ?? self::DEFAULT_PASS,
$parts['path'] ?? self::DEFAULT_VHOST
);

$self->timeout = \filter_var($options['timeout'], FILTER_VALIDATE_INT);
Expand Down Expand Up @@ -182,33 +191,27 @@ public function port(): int
}

/**
* @param string|null $value
*
* @return string
*/
public function user(string $value = null): string
public function vhost(): string
{
return \is_null($value) ? $this->user : $this->user = $value;
return $this->vhost;
}

/**
* @param string|null $value
*
* @return string
*/
public function password(string $value = null): string
public function user(): string
{
return \is_null($value) ? $this->pass : $this->pass = $value;
return $this->user;
}

/**
* @param string|null $value
*
* @return string
*/
public function vhost(string $value = null): string
public function password(): string
{
return \is_null($value) ? $this->vhost : $this->vhost = $value;
return $this->pass;
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* This file is part of PHPinnacle/Ridge.
*
* (c) PHPinnacle Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPinnacle\Ridge\Tests;

use PHPinnacle\Ridge\Config;

class ConfigTest extends RidgeTest
{
public function testCreate()
{
$config = new Config();

self::assertSame('localhost', $config->host());
self::assertSame(5672, $config->port());
self::assertSame('/', $config->vhost());
self::assertSame('guest', $config->user());
self::assertSame('guest', $config->password());
}

public function testUri()
{
$default = new Config();
$custom = new Config('my-domain.com', 6672);

self::assertSame('tcp://localhost:5672', $default->uri());
self::assertSame('tcp://my-domain.com:6672', $custom->uri());
}

public function testParse()
{
$config = Config::parse('amqp://user:pass@localhost:5672/test');

self::assertSame('localhost', $config->host());
self::assertSame(5672, $config->port());
self::assertSame('test', $config->vhost());
self::assertSame('user', $config->user());
self::assertSame('pass', $config->password());
}

public function testVhost()
{
self::assertSame('test', Config::parse('amqp://localhost:5672/test')->vhost());
self::assertSame('/', Config::parse('amqp://localhost:5672/')->vhost());
self::assertSame('/', Config::parse('amqp://localhost:5672')->vhost());
self::assertSame('test/', Config::parse('amqp://localhost:5672/test/')->vhost());
self::assertSame('test/test', Config::parse('amqp://localhost:5672/test/test')->vhost());
self::assertSame('test/test//', Config::parse('amqp://localhost:5672/test/test//')->vhost());
}
}

0 comments on commit dc608fe

Please sign in to comment.