-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCounterStorageTesting.php
80 lines (65 loc) · 2.3 KB
/
CounterStorageTesting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Zlodes\PrometheusClient\Storage\Testing;
use Zlodes\PrometheusClient\Storage\Commands\IncrementCounter;
use Zlodes\PrometheusClient\Storage\Contracts\CounterStorage;
use Zlodes\PrometheusClient\Storage\DTO\MetricNameWithLabels;
use Zlodes\PrometheusClient\Storage\DTO\MetricValue;
// phpcs:ignore
use function PHPUnit\Framework\assertCount;
use function PHPUnit\Framework\assertEmpty;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertSame;
/**
* @codeCoverageIgnore
*/
trait CounterStorageTesting
{
public function testFullCycle(): void
{
$storage = $this->createStorage();
assertEmpty([...$storage->fetchCounters()]);
// Increment Bob by 1
$storage->incrementCounter(
new IncrementCounter(
new MetricNameWithLabels('mileage', ['name' => 'Bob'])
),
);
// Increment Bob by 9.5
$storage->incrementCounter(
new IncrementCounter(
new MetricNameWithLabels('mileage', ['name' => 'Bob']),
8.5
),
);
// Increment Bob by 0.5
$storage->incrementCounter(
new IncrementCounter(
new MetricNameWithLabels('mileage', ['name' => 'Bob']),
0.5
),
);
// Increment Alice by 3
$storage->incrementCounter(
new IncrementCounter(
new MetricNameWithLabels('mileage', ['name' => 'Alice']),
3
),
);
/** @var list<MetricValue> $counters */
$counters = [...$storage->fetchCounters()];
assertCount(2, $counters);
// Bob
assertSame('mileage', $counters[0]->metricNameWithLabels->metricName);
assertSame(['name' => 'Bob'], $counters[0]->metricNameWithLabels->labels);
assertEquals(10, $counters[0]->value);
// Alice
assertSame('mileage', $counters[1]->metricNameWithLabels->metricName);
assertSame(['name' => 'Alice'], $counters[1]->metricNameWithLabels->labels);
assertEquals(3, $counters[1]->value);
// Clean
$storage->clearCounters();
assertEmpty([...$storage->fetchCounters()]);
}
abstract protected function createStorage(): CounterStorage;
}