Skip to content

Commit 7fd7982

Browse files
committed
Add Test for Renderer
It was previously untested, it's now covered with unit tests. Signed-off-by: Rafael Dohms <rdohms@gmail.com>
1 parent 5503d01 commit 7fd7982

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Prometheus;
6+
7+
use Prometheus\CollectorRegistry;
8+
use Prometheus\Exception\MetricsRegistrationException;
9+
use Prometheus\MetricFamilySamples;
10+
use Prometheus\RenderTextFormat;
11+
use PHPUnit\Framework\TestCase;
12+
use Prometheus\Storage\InMemory;
13+
14+
class RenderTextFormatTest extends TestCase
15+
{
16+
17+
public function testOutputMatchesExpectations(): void
18+
{
19+
$metrics = $this->buildSamples();
20+
21+
$renderer = new RenderTextFormat();
22+
$output = $renderer->render($metrics);
23+
24+
self::assertSame($this->getExpectedOutput(), $output);
25+
}
26+
27+
/**
28+
* @return MetricFamilySamples[]
29+
* @throws MetricsRegistrationException
30+
*/
31+
private function buildSamples(): array
32+
{
33+
$namespace = 'mynamespace';
34+
$registry = new CollectorRegistry(new InMemory(), false);
35+
$registry->getOrRegisterCounter($namespace, 'counter', 'counter-help-text', ['label1', 'label2'])
36+
->inc(['bob', 'al\ice']);
37+
$registry->getOrRegisterGauge($namespace, 'gauge', 'counter-help-text', ['label1', 'label2'])
38+
->inc(["bo\nb", 'ali\"ce']);
39+
$registry->getOrRegisterHistogram($namespace, 'histogram', 'counter-help-text', ['label1', 'label2'], [0, 10, 100])
40+
->observe(5, ['bob', 'alice']);
41+
42+
return $registry->getMetricFamilySamples();
43+
}
44+
45+
private function getExpectedOutput(): string
46+
{
47+
return <<<TEXTPLAIN
48+
# HELP mynamespace_counter counter-help-text
49+
# TYPE mynamespace_counter counter
50+
mynamespace_counter{label1="bob",label2="al\\\\ice"} 1
51+
# HELP mynamespace_gauge counter-help-text
52+
# TYPE mynamespace_gauge gauge
53+
mynamespace_gauge{label1="bo\\nb",label2="ali\\\\\"ce"} 1
54+
# HELP mynamespace_histogram counter-help-text
55+
# TYPE mynamespace_histogram histogram
56+
mynamespace_histogram_bucket{label1="bob",label2="alice",le="0"} 0
57+
mynamespace_histogram_bucket{label1="bob",label2="alice",le="10"} 1
58+
mynamespace_histogram_bucket{label1="bob",label2="alice",le="100"} 1
59+
mynamespace_histogram_bucket{label1="bob",label2="alice",le="+Inf"} 1
60+
mynamespace_histogram_count{label1="bob",label2="alice"} 1
61+
mynamespace_histogram_sum{label1="bob",label2="alice"} 5
62+
63+
TEXTPLAIN;
64+
}
65+
}

0 commit comments

Comments
 (0)