Skip to content
This repository was archived by the owner on Apr 4, 2022. It is now read-only.

Commit fd09a24

Browse files
author
toni.lopez
committed
Improving namespace.
1 parent 227dd35 commit fd09a24

File tree

7 files changed

+51
-10
lines changed

7 files changed

+51
-10
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
}
1111
],
1212
"autoload": {
13-
"psr-0": {"Statsd": "src/"}
13+
"psr-4": {
14+
"Statsd\\": "src/"
15+
}
1416
}
1517
}

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
processIsolation="false"
1010
stopOnFailure="false"
1111
syntaxCheck="false"
12-
bootstrap="tests/bootstrap.php"
12+
bootstrap="vendor/autoload.php"
1313
>
1414
<testsuites>
1515
<testsuite name="Statsd Test Suite">
16-
<directory>./tests/Statsd/</directory>
16+
<directory>./tests/</directory>
1717
</testsuite>
1818
</testsuites>
1919
<filter>

src/Statsd/Client.php renamed to src/Client.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Statsd;
99

10+
use Exception;
1011
use Monolog\Logger;
1112
use Statsd\Client\Configuration;
1213

@@ -55,7 +56,7 @@ public function addStat(array $stat)
5556
{
5657
try {
5758
$this->isValidStat($stat);
58-
} catch (Exception) {
59+
} catch (Exception $e) {
5960
throw new Exception('Stat is not valid: ' . $e->getMessage());
6061
}
6162

@@ -98,7 +99,9 @@ public function sendStats()
9899
*/
99100
private function isValidStat(array $stat)
100101
{
101-
if (!preg_match('/^[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*$/', $stat['namespace'])) {
102+
if (!isset($stat['namespace']) || !isset($stat['value']) || !isset($stat['type'])) {
103+
throw new Exception('namespace, type and value are mandatory keys.');
104+
} elseif (!preg_match('/^[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*$/', $stat['namespace'])) {
102105
throw new Exception(
103106
"'$stat[namespace]' does not seem to be a valid prefix. Use a string of "
104107
. 'alphanumerics and dots, e.g. "stats.infratools.twitterhose".'
@@ -114,7 +117,7 @@ private function isValidStat(array $stat)
114117
* @param array $stat
115118
* @return string
116119
*/
117-
private function statToString()
120+
private function statToString($stat)
118121
{
119122
return sprintf('%s:%s|%s', $stat['namespace'], $stat['value'], $stat['type']);
120123
}
File renamed without changes.

tests/ClientTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* @package Statsd\Tests
5+
* @author toni <toni.lopez@shazam.com>
6+
*/
7+
8+
namespace Statsd\Tests;
9+
10+
use Statsd\Client;
11+
use Statsd\Client\Configuration;
12+
use Exception;
13+
14+
/**
15+
* @package Statsd\Tests
16+
*/
17+
18+
class ClientTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function providerWrongStat()
21+
{
22+
return array(
23+
array('stat' => array()),
24+
array('stat' => array('namespace' => '', 'value' => '')),
25+
array('stat' => array('namespace' => 'a.namespace.W£@£$%', 'type' => 'c', 'value' => 23)),
26+
array('stat' => array('namespace' => 'a.namespace', 'type' => 'something', 'value' => 23)),
27+
array('stat' => array('namespace' => 'a.namespace', 'type' => 'c', 'value' => 'halal'))
28+
);
29+
}
30+
31+
/**
32+
* @dataProvider providerWrongStat
33+
* @expectedException Exception
34+
*/
35+
public function testAddWrongStat(array $stat)
36+
{
37+
$client = new Client(new Configuration());
38+
$client->addStat($stat);
39+
}
40+
}

tests/bootstrap.php

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)