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

Commit 583334a

Browse files
committed
Improved some tests
1 parent 80ac488 commit 583334a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/Bucket/MetricOperation.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,27 @@ public function __construct(array $operations = [])
2222
array_splice($operations, static::LENGTH);
2323

2424
foreach (array_values($operations) as $key => $value) {
25-
$this->offsetSet($key, $value);
25+
$this->offsetSet($key, $this->valueToString($value));
2626
}
2727

2828
$opLength = count($operations);
2929
for ($i = $opLength; $i < static::LENGTH; $i++) {
3030
$this->offsetSet($i, Bucket::METRIC_EMPTY_PLACEHOLDER);
3131
}
3232
}
33+
34+
/**
35+
* @param mixed $value
36+
*
37+
* @return string
38+
*/
39+
protected function valueToString($value)
40+
{
41+
if (!is_scalar($value) && $value !== null && (is_object($value) && !method_exists($value, '__toString'))) {
42+
return Bucket::METRIC_EMPTY_PLACEHOLDER;
43+
}
44+
45+
$str = (string)$value;
46+
return empty($str) ? Bucket::METRIC_EMPTY_PLACEHOLDER : $str;
47+
}
3348
}

src/Bucket/Plain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function metricTotalWithSuffix()
7575
*/
7676
public static function sanitizeMetricName($metric)
7777
{
78-
if ($metric == "") {
78+
if ($metric === '') {
7979
return Bucket::METRIC_EMPTY_PLACEHOLDER;
8080
}
8181

tests/Bucket/HTTPRequestTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testHTTPMetricAlterCallback()
6060

6161
$callback->expects($this->once())
6262
->method('__invoke')
63+
->with($this->isInstanceOf(MetricOperation::class), $this->equalTo($request))
6364
->will($this->returnValue(new MetricOperation(['new', 'metric', 'here'])));
6465

6566
$bucket = new HTTPRequest('baz', $request, true, $callback);

tests/Bucket/MetricOperationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public function operations()
4545
['foo', 'bar', 'baz'],
4646
['one' => 'foo', 'two' => 'bar', 'three' => 'baz', 'four' => 'qux'],
4747
],
48+
[
49+
['-', '-', '-'],
50+
[null],
51+
],
52+
[
53+
['-', '-', '-'],
54+
[new \stdClass()],
55+
],
56+
[
57+
['-', '-', '-'],
58+
[''],
59+
],
4860
];
4961
}
5062
}

0 commit comments

Comments
 (0)