You've already forked wp-prometheus
feat: Add comprehensive PHPUnit test suite and CI/CD test gating (v0.5.0)
189 tests across 8 test classes covering all core plugin classes: CustomMetricBuilder, StorageFactory, Authentication, DashboardProvider, RuntimeCollector, Installer, Collector, and MetricsEndpoint. Added test job to Gitea release workflow that gates build-release. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
102
tests/Unit/Endpoint/MetricsEndpointTest.php
Normal file
102
tests/Unit/Endpoint/MetricsEndpointTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magdev\WpPrometheus\Tests\Unit\Endpoint;
|
||||
|
||||
use Magdev\WpPrometheus\Endpoint\MetricsEndpoint;
|
||||
use Magdev\WpPrometheus\Metrics\Collector;
|
||||
use Magdev\WpPrometheus\Metrics\StorageFactory;
|
||||
use Magdev\WpPrometheus\Tests\Helpers\GlobalFunctionState;
|
||||
use Magdev\WpPrometheus\Tests\Unit\TestCase;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
|
||||
#[CoversClass(MetricsEndpoint::class)]
|
||||
class MetricsEndpointTest extends TestCase
|
||||
{
|
||||
private Collector $collector;
|
||||
private MetricsEndpoint $endpoint;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->resetStorageFactory();
|
||||
$this->collector = new Collector();
|
||||
$this->endpoint = new MetricsEndpoint($this->collector);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->resetStorageFactory();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
// ── Constructor ───────────────────────────────────────────────
|
||||
|
||||
#[Test]
|
||||
public function constructor_accepts_collector(): void
|
||||
{
|
||||
$this->assertInstanceOf(MetricsEndpoint::class, $this->endpoint);
|
||||
}
|
||||
|
||||
// ── register_endpoint() ───────────────────────────────────────
|
||||
|
||||
#[Test]
|
||||
public function register_endpoint_adds_rewrite_rule(): void
|
||||
{
|
||||
$this->endpoint->register_endpoint();
|
||||
|
||||
$this->assertSame(1, GlobalFunctionState::getCallCount('add_rewrite_rule'));
|
||||
$args = GlobalFunctionState::$callArgs['add_rewrite_rule'][0];
|
||||
$this->assertSame('^metrics/?$', $args[0]);
|
||||
$this->assertSame('index.php?wp_prometheus_metrics=1', $args[1]);
|
||||
$this->assertSame('top', $args[2]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function register_endpoint_adds_rewrite_tag(): void
|
||||
{
|
||||
$this->endpoint->register_endpoint();
|
||||
|
||||
$this->assertSame(1, GlobalFunctionState::getCallCount('add_rewrite_tag'));
|
||||
$args = GlobalFunctionState::$callArgs['add_rewrite_tag'][0];
|
||||
$this->assertSame('%wp_prometheus_metrics%', $args[0]);
|
||||
$this->assertSame('([^&]+)', $args[1]);
|
||||
}
|
||||
|
||||
// ── handle_request() ──────────────────────────────────────────
|
||||
|
||||
#[Test]
|
||||
public function handle_request_returns_early_when_no_query_var(): void
|
||||
{
|
||||
$wp = new \WP();
|
||||
$wp->query_vars = [];
|
||||
|
||||
// Should return without calling exit or outputting anything.
|
||||
$this->endpoint->handle_request($wp);
|
||||
|
||||
// If we reach this assertion, handle_request returned early (no exit).
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handle_request_returns_early_when_query_var_empty(): void
|
||||
{
|
||||
$wp = new \WP();
|
||||
$wp->query_vars = ['wp_prometheus_metrics' => ''];
|
||||
|
||||
$this->endpoint->handle_request($wp);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────
|
||||
|
||||
private function resetStorageFactory(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(StorageFactory::class);
|
||||
$property = $reflection->getProperty('instance');
|
||||
$property->setValue(null, null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user