You've already forked wp-prometheus
156 lines
5.1 KiB
PHP
156 lines
5.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Magdev\WpPrometheus\Tests\Unit\Metrics;
|
||
|
|
|
||
|
|
use Magdev\WpPrometheus\Metrics\Collector;
|
||
|
|
use Magdev\WpPrometheus\Metrics\RuntimeCollector;
|
||
|
|
use Magdev\WpPrometheus\Metrics\StorageFactory;
|
||
|
|
use Magdev\WpPrometheus\Tests\Unit\TestCase;
|
||
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||
|
|
use PHPUnit\Framework\Attributes\Test;
|
||
|
|
use Prometheus\CollectorRegistry;
|
||
|
|
use Prometheus\Counter;
|
||
|
|
use Prometheus\Gauge;
|
||
|
|
use Prometheus\Histogram;
|
||
|
|
|
||
|
|
#[CoversClass(Collector::class)]
|
||
|
|
class CollectorTest extends TestCase
|
||
|
|
{
|
||
|
|
private Collector $collector;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
$this->resetStorageFactory();
|
||
|
|
$this->collector = new Collector();
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function tearDown(): void
|
||
|
|
{
|
||
|
|
$this->resetRuntimeCollectorSingleton();
|
||
|
|
$this->resetStorageFactory();
|
||
|
|
parent::tearDown();
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── Constructor & Basic Properties ─────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function constructor_creates_registry(): void
|
||
|
|
{
|
||
|
|
$this->assertInstanceOf(CollectorRegistry::class, $this->collector->get_registry());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function get_namespace_returns_wordpress(): void
|
||
|
|
{
|
||
|
|
$this->assertSame('wordpress', $this->collector->get_namespace());
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── register_gauge() ──────────────────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function register_gauge_returns_gauge_instance(): void
|
||
|
|
{
|
||
|
|
$gauge = $this->collector->register_gauge('test_metric', 'A test gauge');
|
||
|
|
$this->assertInstanceOf(Gauge::class, $gauge);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function register_gauge_with_labels(): void
|
||
|
|
{
|
||
|
|
$gauge = $this->collector->register_gauge(
|
||
|
|
'labeled_metric',
|
||
|
|
'A labeled gauge',
|
||
|
|
['label1', 'label2']
|
||
|
|
);
|
||
|
|
$this->assertInstanceOf(Gauge::class, $gauge);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── register_counter() ────────────────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function register_counter_returns_counter_instance(): void
|
||
|
|
{
|
||
|
|
$counter = $this->collector->register_counter('test_counter', 'A test counter');
|
||
|
|
$this->assertInstanceOf(Counter::class, $counter);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function register_counter_with_labels(): void
|
||
|
|
{
|
||
|
|
$counter = $this->collector->register_counter(
|
||
|
|
'labeled_counter',
|
||
|
|
'A labeled counter',
|
||
|
|
['method', 'status']
|
||
|
|
);
|
||
|
|
$this->assertInstanceOf(Counter::class, $counter);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── register_histogram() ──────────────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function register_histogram_returns_histogram_instance(): void
|
||
|
|
{
|
||
|
|
$histogram = $this->collector->register_histogram('test_histogram', 'A test histogram');
|
||
|
|
$this->assertInstanceOf(Histogram::class, $histogram);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function register_histogram_with_custom_buckets(): void
|
||
|
|
{
|
||
|
|
$buckets = [0.1, 0.5, 1.0, 5.0];
|
||
|
|
$histogram = $this->collector->register_histogram(
|
||
|
|
'custom_buckets_hist',
|
||
|
|
'A histogram with custom buckets',
|
||
|
|
['label1'],
|
||
|
|
$buckets
|
||
|
|
);
|
||
|
|
$this->assertInstanceOf(Histogram::class, $histogram);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── render() ──────────────────────────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function render_returns_string(): void
|
||
|
|
{
|
||
|
|
$getOption = $this->getFunctionMock('Magdev\\WpPrometheus\\Metrics', 'get_option');
|
||
|
|
$getOption->expects($this->any())->willReturn([]);
|
||
|
|
|
||
|
|
$output = $this->collector->render();
|
||
|
|
$this->assertIsString($output);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function render_includes_registered_gauge_value(): void
|
||
|
|
{
|
||
|
|
$getOption = $this->getFunctionMock('Magdev\\WpPrometheus\\Metrics', 'get_option');
|
||
|
|
$getOption->expects($this->any())->willReturn([]);
|
||
|
|
|
||
|
|
$gauge = $this->collector->register_gauge('test_render_metric', 'Test metric for render');
|
||
|
|
$gauge->set(42, []);
|
||
|
|
|
||
|
|
$output = $this->collector->render();
|
||
|
|
|
||
|
|
$this->assertStringContainsString('wordpress_test_render_metric', $output);
|
||
|
|
$this->assertStringContainsString('42', $output);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── Helpers ──────────────────────────────────────────────────
|
||
|
|
|
||
|
|
private function resetRuntimeCollectorSingleton(): void
|
||
|
|
{
|
||
|
|
$reflection = new \ReflectionClass(RuntimeCollector::class);
|
||
|
|
$property = $reflection->getProperty('instance');
|
||
|
|
$property->setValue(null, null);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function resetStorageFactory(): void
|
||
|
|
{
|
||
|
|
$reflection = new \ReflectionClass(StorageFactory::class);
|
||
|
|
$property = $reflection->getProperty('instance');
|
||
|
|
$property->setValue(null, null);
|
||
|
|
}
|
||
|
|
}
|