request = $this->createMock(IRequest::class); $this->request->method('getRemoteAddress') ->willReturn('192.168.1.1'); $this->config = $this->createMock(IConfig::class); $this->exporterManager = $this->createMock(ExporterManager::class); $this->exporterManager->method('export')->willReturnCallback([$this, 'getFakeMetrics']); $this->logger = $this->createMock(LoggerInterface::class); $this->controller = new OpenMetricsController('core', $this->request, $this->config, $this->exporterManager, $this->logger); } public function getFakeMetrics(): Generator { $metric = $this->createMock(IMetricFamily::class); $metric->method('type')->willReturn(MetricType::gauge); $metric->method('unit')->willReturn('fake'); $metric->method('name')->willReturn('fake_count'); $metric->method('help')->willReturn('A fake count used for tests'); $metric->method('metrics')->willReturnCallback(function () { yield new Metric(42, ['type' => 'used']); yield new Metric(24, ['type' => 'unused']); }); yield $metric; } public function testGetMetrics(): void { $output = $this->createMock(IOutput::class); $fullOutput = ''; $output->method('setOutput') ->willReturnCallback(function ($output) use (&$fullOutput) { $fullOutput .= $output; }); $this->config->expects($this->once()) ->method('getSystemValue') ->with('openmetrics_allowed_clients') ->willReturn(['192.168.0.0/16']); $response = $this->controller->export(); $this->assertInstanceOf(StreamTraversableResponse::class, $response); $this->assertEquals('200', $response->getStatus()); $this->assertEquals('application/openmetrics-text; version=1.0.0; charset=utf-8', $response->getHeaders()['Content-Type']); $expected = <<callback($output); $this->assertStringMatchesFormat($expected, $fullOutput); } public function testGetMetricsFromForbiddenIp(): void { $this->config->expects($this->once()) ->method('getSystemValue') ->with('openmetrics_allowed_clients') ->willReturn(['1.2.3.4']); $response = $this->controller->export(); $this->assertInstanceOf(Response::class, $response); $this->assertEquals('403', $response->getStatus()); } }