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

Commit d53d73b

Browse files
author
Eduardo Kasper
committed
[PATCH/PO-2958] adds an optional parameter when instantiating statsD
1 parent d54cfaf commit d53d73b

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/Client/StatsD.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use HelloFresh\Stats\HTTPMetricAlterCallback;
66
use HelloFresh\Stats\Incrementer;
77
use HelloFresh\Stats\State;
8-
use HelloFresh\Stats\StatsD\SilentClient as StatsDClient;
98
use HelloFresh\Stats\Timer;
9+
use League\StatsD\Client as StatsDClient;
1010
use League\StatsD\Exception\ConfigurationException;
1111

1212
class StatsD extends AbstractClient implements Client
@@ -26,12 +26,14 @@ class StatsD extends AbstractClient implements Client
2626
/**
2727
* StatsD constructor.
2828
*
29-
* @param string $dsn statsd connection dsn
29+
* @param string $dsn statsd connection dsn
30+
* @param Client $client statsd client
31+
*
3032
* @throws ConfigurationException
3133
*/
32-
public function __construct($dsn)
34+
public function __construct(string $dsn, $client = StatsDClient::class)
3335
{
34-
$this->client = new StatsDClient();
36+
$this->client = new $client();
3537
$this->client->configure($this->buildOptions($dsn));
3638
$this->resetHTTPRequestSection();
3739
}
@@ -41,7 +43,7 @@ public function __construct($dsn)
4143
*
4244
* @return array
4345
*/
44-
protected function buildOptions($dsn)
46+
protected function buildOptions(string $dsn): array
4547
{
4648
$url = (array)parse_url($dsn);
4749

tests/Client/StatsDTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@
33

44
use HelloFresh\Stats;
55
use HelloFresh\Stats\Bucket;
6+
use League\StatsD\Client;
7+
use League\StatsD\Exception\ConfigurationException;
68
use PHPUnit\Framework\TestCase;
9+
use PHPUnit_Framework_MockObject_MockObject;
10+
use ReflectionClass;
11+
12+
class ExposedClientStatsD extends StatsD
13+
{
14+
public function getClient()
15+
{
16+
return $this->client;
17+
}
18+
}
719

820
class StatsDTest extends TestCase
921
{
@@ -17,7 +29,7 @@ public function testBuildOptions()
1729
->disableOriginalConstructor()
1830
->getMock();
1931

20-
$reflection = new \ReflectionClass($statsClient);
32+
$reflection = new ReflectionClass($statsClient);
2133
$methodBuildOptions = $reflection->getMethod('buildOptions');
2234
$methodBuildOptions->setAccessible(true);
2335
$this->assertEquals(
@@ -32,6 +44,30 @@ public function testBuildOptions()
3244
);
3345
}
3446

47+
/**
48+
* @throws ConfigurationException
49+
*/
50+
public function testDefaultClientInstance()
51+
{
52+
$dns = 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0';
53+
$statsD = new ExposedClientStatsD($dns);
54+
$instantiatedClient = $statsD->getClient();
55+
56+
$this->assertInstanceOf(Client::class, $instantiatedClient);
57+
}
58+
59+
/**
60+
* @throws ConfigurationException
61+
*/
62+
public function testOptionalClientInstance()
63+
{
64+
$dns = 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0';
65+
$statsD = new ExposedClientStatsD($dns, Stats\StatsD\SilentClient::class);
66+
$instantiatedClient = $statsD->getClient();
67+
68+
$this->assertInstanceOf(Stats\StatsD\SilentClient::class, $instantiatedClient);
69+
}
70+
3571
public function testInstances()
3672
{
3773
if (!class_exists('\League\StatsD\Client')) {
@@ -50,12 +86,12 @@ public function testHTTPRequestSection()
5086

5187
$section = uniqid('section', true);
5288

53-
/** @var \PHPUnit_Framework_MockObject_MockObject|\League\StatsD\Client $statsd */
89+
/** @var PHPUnit_Framework_MockObject_MockObject|Client $statsd */
5490
$statsd = $this->getMockBuilder('\League\StatsD\Client')->setMethods(['gauge'])->getMock();
5591

5692
$statsClient = new StatsD($statsd);
5793

58-
$reflection = new \ReflectionClass($statsClient);
94+
$reflection = new ReflectionClass($statsClient);
5995
$reflectionProperty = $reflection->getProperty('httpRequestSection');
6096
$reflectionProperty->setAccessible(true);
6197

0 commit comments

Comments
 (0)