update tests

This commit is contained in:
Daniil Zobov 2020-04-14 16:15:28 +03:00
parent 31eaa993eb
commit 4b056b8c1e
6 changed files with 18 additions and 58 deletions

View file

@ -18,10 +18,12 @@ composer require superbalist/laravel-prometheus-exporter
``` ```
Register the service provider in app.php Register the service provider in app.php
#### Laravel
```php ```php
'providers' => [ 'providers' => [
// ... // ...
Superbalist\LaravelPrometheusExporter\PrometheusServiceProvider::class, Superbalist\LaravelPrometheusExporter\LaravelServiceProvider::class,
] ]
``` ```
@ -33,6 +35,11 @@ Register the facade in app.php
] ]
``` ```
#### Lumen
```php
$app->register(Superbalist\LaravelPrometheusExporter\LumenServiceProvider::class);
```
## Configuration ## Configuration
The package has a default configuration which uses the following environment variables. The package has a default configuration which uses the following environment variables.

View file

@ -2,7 +2,7 @@
namespace Superbalist\LaravelPrometheusExporter; namespace Superbalist\LaravelPrometheusExporter;
class LaravelPrometheusServiceProvider extends PrometheusServiceProvider class LaravelServiceProvider extends PrometheusServiceProvider
{ {
/** /**
* Perform post-registration booting of services. * Perform post-registration booting of services.

View file

@ -2,7 +2,7 @@
namespace Superbalist\LaravelPrometheusExporter; namespace Superbalist\LaravelPrometheusExporter;
class LumenPrometheusServiceProvider extends PrometheusServiceProvider class LumenServiceProvider extends PrometheusServiceProvider
{ {
/** /**
* Load routes. * Load routes.

View file

@ -2,40 +2,25 @@
namespace Superbalist\LaravelPrometheusExporter; namespace Superbalist\LaravelPrometheusExporter;
use Illuminate\Contracts\Routing\ResponseFactory; use Illuminate\Http\Response;
use Illuminate\Routing\Controller; use Illuminate\Routing\Controller;
use Prometheus\RenderTextFormat; use Prometheus\RenderTextFormat;
class MetricsController extends Controller class MetricsController extends Controller
{ {
/**
* @var ResponseFactory
*/
protected $responseFactory;
/** /**
* @var PrometheusExporter * @var PrometheusExporter
*/ */
protected $prometheusExporter; protected $prometheusExporter;
/** /**
* @param ResponseFactory $responseFactory
* @param PrometheusExporter $prometheusExporter * @param PrometheusExporter $prometheusExporter
*/ */
public function __construct(ResponseFactory $responseFactory, PrometheusExporter $prometheusExporter) public function __construct(PrometheusExporter $prometheusExporter)
{ {
$this->responseFactory = $responseFactory;
$this->prometheusExporter = $prometheusExporter; $this->prometheusExporter = $prometheusExporter;
} }
/**
* @return ResponseFactory
*/
public function getResponseFactory()
{
return $this->responseFactory;
}
/** /**
* @return PrometheusExporter * @return PrometheusExporter
*/ */
@ -59,6 +44,6 @@ class MetricsController extends Controller
$renderer = new RenderTextFormat(); $renderer = new RenderTextFormat();
$result = $renderer->render($metrics); $result = $renderer->render($metrics);
return $this->responseFactory->make($result, 200, ['Content-Type' => RenderTextFormat::MIME_TYPE]); return Response::create($result, 200, ['Content-Type' => RenderTextFormat::MIME_TYPE]);
} }
} }

View file

@ -1,17 +0,0 @@
<?php
/** @var \Illuminate\Routing\Route $route */
$route = Route::get(
config('prometheus.metrics_route_path'),
\Superbalist\LaravelPrometheusExporter\MetricsController::class . '@getMetrics'
);
if ($name = config('prometheus.metrics_route_name')) {
$route->name($name);
}
$middleware = config('prometheus.metrics_route_middleware');
if ($middleware) {
$route->middleware($middleware);
}

View file

@ -2,8 +2,6 @@
namespace Tests; namespace Tests;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use Mockery; use Mockery;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prometheus\RenderTextFormat; use Prometheus\RenderTextFormat;
@ -14,35 +12,22 @@ class MetricsControllerTest extends TestCase
{ {
public function testConstruct() public function testConstruct()
{ {
$responseFactory = Mockery::mock(ResponseFactory::class);
$exporter = Mockery::mock(PrometheusExporter::class); $exporter = Mockery::mock(PrometheusExporter::class);
$controller = new MetricsController($responseFactory, $exporter); $controller = new MetricsController($exporter);
$this->assertSame($responseFactory, $controller->getResponseFactory());
$this->assertSame($exporter, $controller->getPrometheusExporter()); $this->assertSame($exporter, $controller->getPrometheusExporter());
} }
public function testGetMetrics() public function testGetMetrics()
{ {
$response = Mockery::mock(Response::class);
$responseFactory = Mockery::mock(ResponseFactory::class);
$responseFactory->shouldReceive('make')
->once()
->withArgs([
"\n",
200,
['Content-Type' => RenderTextFormat::MIME_TYPE],
])
->andReturn($response);
$exporter = Mockery::mock(PrometheusExporter::class); $exporter = Mockery::mock(PrometheusExporter::class);
$exporter->shouldReceive('export') $exporter->shouldReceive('export')
->once() ->once()
->andReturn([]); ->andReturn([]);
$controller = new MetricsController($responseFactory, $exporter); $controller = new MetricsController($exporter);
$r = $controller->getMetrics(); $response = $controller->getMetrics();
$this->assertSame($response, $r); $this->assertSame(200, $response->getStatusCode());
$this->assertSame(RenderTextFormat::MIME_TYPE, $response->headers->get('Content-Type'));
} }
} }