Completed
Push — master ( f1e1ae...7e3292 )
by Denis
10:20 queued 02:02
created

AppMetricsTest::testSetRequestDuration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 20
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Artprima\PrometheusMetricsBundle\Metrics;
4
5
use Artprima\PrometheusMetricsBundle\Metrics\AppMetrics;
6
use Artprima\PrometheusMetricsBundle\Metrics\Renderer;
7
use PHPUnit\Framework\TestCase;
8
use Prometheus\CollectorRegistry;
9
use Prometheus\Storage\InMemory;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
13
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
14
15
class AppMetricsTest extends TestCase
16
{
17
    private $namespace;
18
    private $collectionRegistry;
19
    /**
20
     * @var RendererTest
21
     */
22
    private $renderer;
23
24
    public function setUp()
25
    {
26
        $this->namespace = 'dummy';
27
        $this->collectionRegistry = new CollectorRegistry(new InMemory());
28
        $this->renderer = new Renderer($this->collectionRegistry);
29
    }
30
31
    public function testCollectRequest()
32
    {
33
        $metrics = new AppMetrics();
34
        $metrics->init($this->namespace, $this->collectionRegistry);
35
36
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
37
        $evt = $this->createMock(GetResponseEvent::class);
38
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
39
40
        $metrics->collectRequest($evt);
41
42
        $response = $this->renderer->renderResponse();
43
        $responseContent = $response->getContent();
44
45
        $this->assertContains('dummy_instance_name{instance="dev"} 1', $responseContent);
46
        $this->assertContains("dummy_http_requests_total{action=\"all\"} 1\n", $responseContent);
47
        $this->assertContains("dummy_http_requests_total{action=\"GET-test_route\"} 1\n", $responseContent);
48
    }
49
50
    public function testCollectRequestOptionsMethod()
51
    {
52
        $metrics = new AppMetrics();
53
        $metrics->init($this->namespace, $this->collectionRegistry);
54
55
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'OPTIONS']);
56
        $evt = $this->createMock(GetResponseEvent::class);
57
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
58
59
        $metrics->collectRequest($evt);
60
61
        $response = $this->renderer->renderResponse();
62
        $responseContent = $response->getContent();
63
64
        $this->assertEquals('', trim($responseContent));
65
    }
66
67
    public function provideMetricsName()
68
    {
69
        return [
70
            [200, 'http_2xx_responses_total'],
71
            [400, 'http_4xx_responses_total'],
72
            [500, 'http_5xx_responses_total'],
73
        ];
74
    }
75
76
    /**
77
     * @dataProvider provideMetricsName
78
     */
79
    public function testCollectResponse($code, $metricsName)
80
    {
81
        $metrics = new AppMetrics();
82
        $metrics->init($this->namespace, $this->collectionRegistry);
83
84
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
85
        $evt = $this->createMock(PostResponseEvent::class);
86
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
87
        $response = new Response('', $code);
88
        $evt->expects(self::any())->method('getResponse')->willReturn($response);
89
90
        $metrics->collectResponse($evt);
91
92
        $response = $this->renderer->renderResponse();
93
        $responseContent = $response->getContent();
94
95
        $this->assertContains("dummy_{$metricsName}{action=\"all\"} 1\n", $responseContent);
96
        $this->assertContains("dummy_{$metricsName}{action=\"GET-test_route\"} 1\n", $responseContent);
97
    }
98
99
    public function testSetRequestDuration()
100
    {
101
        $metrics = new AppMetrics();
102
        $metrics->init($this->namespace, $this->collectionRegistry);
103
104
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
105
        $evt = $this->createMock(PostResponseEvent::class);
106
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
107
        $response = new Response('', 200);
108
        $evt->expects(self::any())->method('getResponse')->willReturn($response);
109
110
        $metrics->collectResponse($evt);
111
        $response = $this->renderer->renderResponse();
112
        $content = $response->getContent();
113
        $this->assertContains('dummy_request_durations_histogram_seconds_bucket{action="GET-test_route",le=', $content);
114
        $this->assertContains('dummy_request_durations_histogram_seconds_count{action="GET-test_route"}', $content);
115
        $this->assertContains('dummy_request_durations_histogram_seconds_sum{action="GET-test_route"}', $content);
116
        $this->assertContains('dummy_request_durations_histogram_seconds_bucket{action="all",le=', $content);
117
        $this->assertContains('dummy_request_durations_histogram_seconds_count{action="all"}', $content);
118
        $this->assertContains('dummy_request_durations_histogram_seconds_sum{action="all"}', $content);
119
    }
120
}
121