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); } }