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

Commit 522bfb3

Browse files
committed
Added more unit tests
1 parent 55f7f98 commit 522bfb3

File tree

8 files changed

+118
-7
lines changed

8 files changed

+118
-7
lines changed

src/Client/Log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(LoggerInterface $logger)
3535
*/
3636
public function buildTimer()
3737
{
38-
return new Timer\Memory();
38+
return new Timer\Log($this->logger);
3939
}
4040

4141
/**

src/Client/Memory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ class Memory extends AbstractClient implements Client
1717
/** @var Timer\Memory[] */
1818
protected $timers;
1919

20+
/**
21+
* Memory constructor.
22+
*/
23+
public function __construct()
24+
{
25+
$this->resetHTTPRequestSection();
26+
}
27+
2028
/**
2129
* @inheritdoc
2230
*/

src/Client/NoOp.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class NoOp extends AbstractClient implements Client
1515
/** @var State\NoOp */
1616
protected $state;
1717

18+
/**
19+
* NoOp constructor.
20+
*/
21+
public function __construct()
22+
{
23+
$this->resetHTTPRequestSection();
24+
}
25+
1826
/**
1927
* @inheritdoc
2028
*/

src/Client/StatsD.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use HelloFresh\Stats\HTTPMetricAlterCallback;
88
use HelloFresh\Stats\Incrementer;
99
use HelloFresh\Stats\State;
10-
use HelloFresh\Stats\Timer\StatsD as StatsDTimer;
10+
use HelloFresh\Stats\Timer;
1111
use League\StatsD\Client as StatsDClient;
1212

1313
class StatsD extends AbstractClient implements Client
@@ -33,6 +33,7 @@ public function __construct($dsn)
3333
{
3434
$this->client = new StatsDClient();
3535
$this->configure($dsn);
36+
$this->resetHTTPRequestSection();
3637
}
3738

3839
/**
@@ -42,13 +43,13 @@ protected function configure($dsn)
4243
{
4344
$url = (array)parse_url($dsn);
4445

45-
$params = parse_str(empty($url['query']) ? '' : $url['query']);
46+
parse_str(empty($url['query']) ? '' : $url['query'], $params);
4647
$options = [
47-
'host' => empty($url['host']) ? 'localhost' : '',
48-
'port' => empty($url['port']) ? $url['port'] : 8125,
48+
'host' => empty($url['host']) ? 'localhost' : $url['host'],
49+
'port' => empty($url['port']) ? 8125 : $url['port'],
4950
'namespace' => empty($url['path']) ? '' : trim($url['path'], '/'),
5051
'timeout' => empty($params['timeout']) ? null : (float)$params['timeout'],
51-
'throwConnectionExceptions' => empty($params['error']) ? true : (bool)$params['error'],
52+
'throwConnectionExceptions' => isset($params['error']) ? (bool)$params['error'] : true,
5253
];
5354

5455
$this->client->configure($options);
@@ -59,7 +60,7 @@ protected function configure($dsn)
5960
*/
6061
public function buildTimer()
6162
{
62-
return new StatsDTimer($this->client);
63+
return new Timer\StatsD($this->client);
6364
}
6465

6566
/**

tests/Client/LogTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Client;
4+
5+
6+
use HelloFresh\Stats;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class LogTest extends TestCase
10+
{
11+
public function testInstances()
12+
{
13+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */
14+
$logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock();
15+
16+
$statsClient = new Log($logger);
17+
$this->assertInstanceOf(Stats\Timer\Log::class, $statsClient->buildTimer());
18+
}
19+
}

tests/Client/MemoryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Client;
4+
5+
6+
use HelloFresh\Stats;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class MemoryTest extends TestCase
10+
{
11+
public function testInstances()
12+
{
13+
$statsClient = new Memory();
14+
$this->assertInstanceOf(Stats\Timer\Memory::class, $statsClient->buildTimer());
15+
}
16+
}

tests/Client/NoOpTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Client;
4+
5+
6+
use HelloFresh\Stats;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class NoOpTest extends TestCase
10+
{
11+
public function testInstances()
12+
{
13+
$statsClient = new NoOp();
14+
$this->assertInstanceOf(Stats\Timer\Memory::class, $statsClient->buildTimer());
15+
}
16+
}

tests/Client/StatsDTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Client;
4+
5+
6+
use HelloFresh\Stats;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class StatsDTest extends TestCase
10+
{
11+
public function testConfigure()
12+
{
13+
$statsClient = $this->getMockBuilder('\HelloFresh\Stats\Client\StatsD')
14+
->disableOriginalConstructor()
15+
->getMock();
16+
17+
$statsdClient = $this->getMockBuilder('\League\StatsD\Client')->getMock();
18+
$statsdClient->expects($this->once())
19+
->method('configure')
20+
->with([
21+
'host' => 'stats.local',
22+
'port' => 1234,
23+
'namespace' => 'prefix.ns',
24+
'timeout' => 2.5,
25+
'throwConnectionExceptions' => false,
26+
]);
27+
28+
$reflection = new \ReflectionClass($statsClient);
29+
$propertyClient = $reflection->getProperty('client');
30+
$propertyClient->setAccessible(true);
31+
$propertyClient->setValue($statsClient, $statsdClient);
32+
33+
$methodConfigure = $reflection->getMethod('configure');
34+
$methodConfigure->setAccessible(true);
35+
$methodConfigure->invoke($statsClient, 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0');
36+
}
37+
38+
public function testInstances()
39+
{
40+
$statsClient = new StatsD('statsd://');
41+
$this->assertInstanceOf(Stats\Timer\StatsD::class, $statsClient->buildTimer());
42+
}
43+
}

0 commit comments

Comments
 (0)