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

Commit 0e35325

Browse files
committed
added failing test
1 parent 07cc7ec commit 0e35325

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

tests/Test/Prometheus/AbstractCounterTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use PHPUnit_Framework_TestCase;
77
use Prometheus\Counter;
88
use Prometheus\MetricFamilySamples;
9+
use Prometheus\Sample;
10+
use Prometheus\Storage\Adapter;
911

1012
/**
1113
* See https://prometheus.io/docs/instrumenting/exposition_formats/
@@ -139,5 +141,50 @@ public function itShouldRejectInvalidLabelNames()
139141
new Counter($this->adapter, 'test', 'some_metric', 'help', array('invalid label'));
140142
}
141143

144+
/**
145+
* @test
146+
* @dataProvider labelValuesDataProvider
147+
*
148+
* @param mixed $value The label value
149+
*/
150+
public function isShouldAcceptAnySequenceOfBasicLatinCharactersForLabelValues($value)
151+
{
152+
$label = 'foo';
153+
$histogram = new Counter($this->adapter, 'test', 'some_metric', 'help', array($label));
154+
$histogram->inc(array($value));
155+
156+
$metrics = $this->adapter->collect();
157+
self::assertInternalType('array', $metrics);
158+
self::assertCount(1, $metrics);
159+
self::assertContainsOnlyInstancesOf(MetricFamilySamples::class, $metrics);
160+
161+
$metric = reset($metrics);
162+
$samples = $metric->getSamples();
163+
self::assertContainsOnlyInstancesOf(Sample::class, $samples);
164+
165+
foreach ($samples as $sample) {
166+
$labels = array_combine(
167+
array_merge($metric->getLabelNames(), $sample->getLabelNames()),
168+
$sample->getLabelValues()
169+
);
170+
self::assertEquals($value, $labels[$label]);
171+
}
172+
}
173+
174+
/**
175+
* @see isShouldAcceptArbitraryLabelValues
176+
* @return array
177+
*/
178+
public function labelValuesDataProvider()
179+
{
180+
$cases = [];
181+
// Basic Latin
182+
// See https://en.wikipedia.org/wiki/List_of_Unicode_characters#Basic_Latin
183+
for ($i = 32; $i <= 121; $i++) {
184+
$cases['ASCII code ' . $i] = array(chr($i));
185+
}
186+
return $cases;
187+
}
188+
142189
public abstract function configureAdapter();
143190
}

tests/Test/Prometheus/AbstractGaugeTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit_Framework_TestCase;
77
use Prometheus\Gauge;
88
use Prometheus\MetricFamilySamples;
9+
use Prometheus\Sample;
910
use Prometheus\Storage\Adapter;
1011

1112
/**
@@ -308,5 +309,50 @@ public function itShouldRejectInvalidLabelNames()
308309
new Gauge($this->adapter, 'test', 'some_metric', 'help', array('invalid label'));
309310
}
310311

312+
/**
313+
* @test
314+
* @dataProvider labelValuesDataProvider
315+
*
316+
* @param mixed $value The label value
317+
*/
318+
public function isShouldAcceptAnySequenceOfBasicLatinCharactersForLabelValues($value)
319+
{
320+
$label = 'foo';
321+
$histogram = new Gauge($this->adapter, 'test', 'some_metric', 'help', array($label));
322+
$histogram->inc(array($value));
323+
324+
$metrics = $this->adapter->collect();
325+
self::assertInternalType('array', $metrics);
326+
self::assertCount(1, $metrics);
327+
self::assertContainsOnlyInstancesOf(MetricFamilySamples::class, $metrics);
328+
329+
$metric = reset($metrics);
330+
$samples = $metric->getSamples();
331+
self::assertContainsOnlyInstancesOf(Sample::class, $samples);
332+
333+
foreach ($samples as $sample) {
334+
$labels = array_combine(
335+
array_merge($metric->getLabelNames(), $sample->getLabelNames()),
336+
$sample->getLabelValues()
337+
);
338+
self::assertEquals($value, $labels[$label]);
339+
}
340+
}
341+
342+
/**
343+
* @see isShouldAcceptArbitraryLabelValues
344+
* @return array
345+
*/
346+
public function labelValuesDataProvider()
347+
{
348+
$cases = [];
349+
// Basic Latin
350+
// See https://en.wikipedia.org/wiki/List_of_Unicode_characters#Basic_Latin
351+
for ($i = 32; $i <= 121; $i++) {
352+
$cases['ASCII code ' . $i] = array(chr($i));
353+
}
354+
return $cases;
355+
}
356+
311357
public abstract function configureAdapter();
312358
}

tests/Test/Prometheus/AbstractHistogramTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit_Framework_TestCase;
77
use Prometheus\Histogram;
88
use Prometheus\MetricFamilySamples;
9+
use Prometheus\Sample;
910
use Prometheus\Storage\Adapter;
1011

1112

@@ -421,5 +422,50 @@ public function itShouldRejectInvalidLabelNames()
421422
new Histogram($this->adapter, 'test', 'some_metric', 'help', array('invalid label'), array(1));
422423
}
423424

425+
/**
426+
* @test
427+
* @dataProvider labelValuesDataProvider
428+
*
429+
* @param mixed $value The label value
430+
*/
431+
public function isShouldAcceptAnySequenceOfBasicLatinCharactersForLabelValues($value)
432+
{
433+
$label = 'foo';
434+
$histogram = new Histogram($this->adapter, 'test', 'some_metric', 'help', array($label), array(1));
435+
$histogram->observe(1, array($value));
436+
437+
$metrics = $this->adapter->collect();
438+
self::assertInternalType('array', $metrics);
439+
self::assertCount(1, $metrics);
440+
self::assertContainsOnlyInstancesOf(MetricFamilySamples::class, $metrics);
441+
442+
$metric = reset($metrics);
443+
$samples = $metric->getSamples();
444+
self::assertContainsOnlyInstancesOf(Sample::class, $samples);
445+
446+
foreach ($samples as $sample) {
447+
$labels = array_combine(
448+
array_merge($metric->getLabelNames(), $sample->getLabelNames()),
449+
$sample->getLabelValues()
450+
);
451+
self::assertEquals($value, $labels[$label]);
452+
}
453+
}
454+
455+
/**
456+
* @see isShouldAcceptArbitraryLabelValues
457+
* @return array
458+
*/
459+
public function labelValuesDataProvider()
460+
{
461+
$cases = [];
462+
// Basic Latin
463+
// See https://en.wikipedia.org/wiki/List_of_Unicode_characters#Basic_Latin
464+
for ($i = 32; $i <= 121; $i++) {
465+
$cases['ASCII code ' . $i] = array(chr($i));
466+
}
467+
return $cases;
468+
}
469+
424470
public abstract function configureAdapter();
425471
}

0 commit comments

Comments
 (0)