Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 80ac488

Browse files
committed
Refactored building statsd options building
1 parent 20966fb commit 80ac488

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

src/Client/StatsD.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ class StatsD extends AbstractClient implements Client
3232
public function __construct($dsn)
3333
{
3434
$this->client = new StatsDClient();
35-
$this->configure($dsn);
35+
$this->client->configure($this->buildOptions($dsn));
3636
$this->resetHTTPRequestSection();
3737
}
3838

3939
/**
4040
* @param string $dsn
41+
*
42+
* @return array
4143
*/
42-
protected function configure($dsn)
44+
protected function buildOptions($dsn)
4345
{
4446
$url = (array)parse_url($dsn);
4547

4648
parse_str(empty($url['query']) ? '' : $url['query'], $params);
47-
$options = [
49+
return [
4850
'host' => empty($url['host']) ? 'localhost' : $url['host'],
4951
'port' => empty($url['port']) ? 8125 : $url['port'],
5052
'namespace' => empty($url['path']) ? '' : trim($url['path'], '/'),
5153
'timeout' => empty($params['timeout']) ? null : (float)$params['timeout'],
5254
'throwConnectionExceptions' => isset($params['error']) ? (bool)$params['error'] : true,
5355
];
54-
55-
$this->client->configure($options);
5656
}
5757

5858
/**

tests/Client/StatsDTest.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class StatsDTest extends TestCase
1313
{
14-
public function testConfigure()
14+
public function testBuildOptions()
1515
{
1616
if (!class_exists('\League\StatsD\Client')) {
1717
$this->markTestSkipped('Missing league/statsd package');
@@ -21,25 +21,19 @@ public function testConfigure()
2121
->disableOriginalConstructor()
2222
->getMock();
2323

24-
$statsdClient = $this->getMockBuilder('\League\StatsD\Client')->getMock();
25-
$statsdClient->expects($this->once())
26-
->method('configure')
27-
->with([
24+
$reflection = new \ReflectionClass($statsClient);
25+
$methodBuildOptions = $reflection->getMethod('buildOptions');
26+
$methodBuildOptions->setAccessible(true);
27+
$this->assertEquals(
28+
[
2829
'host' => 'stats.local',
2930
'port' => 1234,
3031
'namespace' => 'prefix.ns',
3132
'timeout' => 2.5,
3233
'throwConnectionExceptions' => false,
33-
]);
34-
35-
$reflection = new \ReflectionClass($statsClient);
36-
$propertyClient = $reflection->getProperty('client');
37-
$propertyClient->setAccessible(true);
38-
$propertyClient->setValue($statsClient, $statsdClient);
39-
40-
$methodConfigure = $reflection->getMethod('configure');
41-
$methodConfigure->setAccessible(true);
42-
$methodConfigure->invoke($statsClient, 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0');
34+
],
35+
$methodBuildOptions->invoke($statsClient, 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0')
36+
);
4337
}
4438

4539
public function testInstances()

0 commit comments

Comments
 (0)