Skip to content
This repository was archived by the owner on May 27, 2024. It is now read-only.

Commit a5c4ab1

Browse files
authored
Fix boolean tag values (DataDog#119)
* baseline passing tests * failing test cases * make tests pass * add comment * one more test
1 parent bec0a07 commit a5c4ab1

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/DogStatsd.php

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ private function serializeTags($tags)
323323
foreach ($all_tags as $tag => $value) {
324324
if ($value === null) {
325325
$tag_strings[] = $tag;
326+
} elseif (is_bool($value)) {
327+
$tag_strings[] = $tag . ':' . ($value === true ? 'true' : 'false');
326328
} else {
327329
$tag_strings[] = $tag . ':' . $value;
328330
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace DataDog\UnitTests\DogStatsd;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use DataDog\DogStatsd;
7+
8+
class TagSerializationTest extends TestCase
9+
{
10+
private function callPrivate($object, $method, $params) {
11+
$reflectionMethod = new \ReflectionMethod(get_class($object), $method);
12+
$reflectionMethod->setAccessible(true);
13+
return $reflectionMethod->invoke($object, $params);
14+
}
15+
16+
/**
17+
* @dataProvider tagProvider
18+
*
19+
* @param $tags
20+
* @param $expected
21+
*/
22+
public function testTagSerialization($tags, $expected) {
23+
$dog = new DogStatsd();
24+
25+
$this->assertsame(
26+
$expected,
27+
$this->callPrivate($dog, 'serializeTags', $tags)
28+
);
29+
}
30+
31+
public function tagProvider()
32+
{
33+
return [
34+
'without tags' => [
35+
[],
36+
''
37+
],
38+
'one string' => [
39+
['foo' => 'bar'],
40+
'|#foo:bar'
41+
],
42+
'two strings' => [
43+
['foo' => 'bar', 'baz' => 'blam'],
44+
'|#foo:bar,baz:blam'
45+
],
46+
'one string one int' => [
47+
['foo' => 'bar', 'baz' => 42],
48+
'|#foo:bar,baz:42'
49+
],
50+
// https://github.com/DataDog/php-datadogstatsd/issues/118
51+
'one string one true boolean' => [
52+
['foo' => 'bar', 'baz' => true],
53+
'|#foo:bar,baz:true'
54+
],
55+
'one string one false boolean' => [
56+
['foo' => 'bar', 'baz' => false],
57+
'|#foo:bar,baz:false'
58+
],
59+
'grab bag' => [
60+
['foo' => 'bar', 'baz' => false, 'nullValue' => null, 'blam' => 1, 'blah' => 0],
61+
'|#foo:bar,baz:false,nullValue,blam:1,blah:0'
62+
]
63+
];
64+
}
65+
}

0 commit comments

Comments
 (0)