Skip to content

Commit dc608fe

Browse files
Anton Shaboutazloyuser
authored andcommitted
Fix parse vhost from dsn
1 parent 6d66390 commit dc608fe

File tree

2 files changed

+79
-19
lines changed

2 files changed

+79
-19
lines changed

src/Config.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,22 @@ final class Config
107107
/**
108108
* @param string $host
109109
* @param int $port
110-
* @param string $vhost
111110
* @param string $user
112111
* @param string $pass
112+
* @param string $vhost
113113
*/
114-
public function __construct(string $host, int $port, string $vhost = null, string $user = null, string $pass = null)
115-
{
114+
public function __construct(
115+
string $host = self::DEFAULT_HOST,
116+
int $port = self::DEFAULT_PORT,
117+
string $user = self::DEFAULT_USER,
118+
string $pass = self::DEFAULT_PASS,
119+
string $vhost = null
120+
) {
116121
$this->host = $host;
117122
$this->port = $port;
123+
$this->user = $user;
124+
$this->pass = $pass;
118125
$this->vhost = $vhost ?: self::DEFAULT_VHOST;
119-
$this->user = $user ?: self::DEFAULT_USER;
120-
$this->pass = $pass ?: self::DEFAULT_PASS;
121126
}
122127

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

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

141+
if (!empty($parts['path'])) {
142+
$parts['path'] = \substr($parts['path'], 1);
143+
}
144+
136145
$self = new self(
137146
$parts['host'] ?? self::DEFAULT_HOST,
138147
$parts['port'] ?? self::DEFAULT_PORT,
139-
$parts['path'] ?? self::DEFAULT_VHOST,
140148
$parts['user'] ?? self::DEFAULT_USER,
141-
$parts['pass'] ?? self::DEFAULT_PASS
149+
$parts['pass'] ?? self::DEFAULT_PASS,
150+
$parts['path'] ?? self::DEFAULT_VHOST
142151
);
143152

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

184193
/**
185-
* @param string|null $value
186-
*
187194
* @return string
188195
*/
189-
public function user(string $value = null): string
196+
public function vhost(): string
190197
{
191-
return \is_null($value) ? $this->user : $this->user = $value;
198+
return $this->vhost;
192199
}
193200

194201
/**
195-
* @param string|null $value
196-
*
197202
* @return string
198203
*/
199-
public function password(string $value = null): string
204+
public function user(): string
200205
{
201-
return \is_null($value) ? $this->pass : $this->pass = $value;
206+
return $this->user;
202207
}
203208

204209
/**
205-
* @param string|null $value
206-
*
207210
* @return string
208211
*/
209-
public function vhost(string $value = null): string
212+
public function password(): string
210213
{
211-
return \is_null($value) ? $this->vhost : $this->vhost = $value;
214+
return $this->pass;
212215
}
213216

214217
/**

tests/ConfigTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* This file is part of PHPinnacle/Ridge.
4+
*
5+
* (c) PHPinnacle Team <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace PHPinnacle\Ridge\Tests;
12+
13+
use PHPinnacle\Ridge\Config;
14+
15+
class ConfigTest extends RidgeTest
16+
{
17+
public function testCreate()
18+
{
19+
$config = new Config();
20+
21+
self::assertSame('localhost', $config->host());
22+
self::assertSame(5672, $config->port());
23+
self::assertSame('/', $config->vhost());
24+
self::assertSame('guest', $config->user());
25+
self::assertSame('guest', $config->password());
26+
}
27+
28+
public function testUri()
29+
{
30+
$default = new Config();
31+
$custom = new Config('my-domain.com', 6672);
32+
33+
self::assertSame('tcp://localhost:5672', $default->uri());
34+
self::assertSame('tcp://my-domain.com:6672', $custom->uri());
35+
}
36+
37+
public function testParse()
38+
{
39+
$config = Config::parse('amqp://user:pass@localhost:5672/test');
40+
41+
self::assertSame('localhost', $config->host());
42+
self::assertSame(5672, $config->port());
43+
self::assertSame('test', $config->vhost());
44+
self::assertSame('user', $config->user());
45+
self::assertSame('pass', $config->password());
46+
}
47+
48+
public function testVhost()
49+
{
50+
self::assertSame('test', Config::parse('amqp://localhost:5672/test')->vhost());
51+
self::assertSame('/', Config::parse('amqp://localhost:5672/')->vhost());
52+
self::assertSame('/', Config::parse('amqp://localhost:5672')->vhost());
53+
self::assertSame('test/', Config::parse('amqp://localhost:5672/test/')->vhost());
54+
self::assertSame('test/test', Config::parse('amqp://localhost:5672/test/test')->vhost());
55+
self::assertSame('test/test//', Config::parse('amqp://localhost:5672/test/test//')->vhost());
56+
}
57+
}

0 commit comments

Comments
 (0)