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

RequestCounterListenerTest::testOnKernelRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Artprima\PrometheusMetricsBundle\EventListener;
4
5
use Artprima\PrometheusMetricsBundle\EventListener\RequestCounterListener;
6
use Artprima\PrometheusMetricsBundle\Metrics\MetricsGeneratorInterface;
7
use Artprima\PrometheusMetricsBundle\Metrics\MetricsGeneratorRegistry;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Debug\BufferingLogger;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
12
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
13
14
class RequestCounterListenerTest extends TestCase
15
{
16
    public function testOnKernelRequest()
17
    {
18
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
19
        $evt = $this->createMock(GetResponseEvent::class);
20
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
21
        $evt->expects(self::any())->method('isMasterRequest')->willReturn(true);
22
23
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
24
        $generator1->expects(self::once())->method('collectRequest')->with($evt);
25
        $generator2 = $this->createMock(MetricsGeneratorInterface::class);
26
        $generator2->expects(self::once())->method('collectRequest')->with($evt);
27
28
        $registry = new MetricsGeneratorRegistry();
29
        $registry->registerMetricsGenerator($generator1);
30
        $registry->registerMetricsGenerator($generator2);
31
32
        $listener = new RequestCounterListener($registry);
33
        $listener->onKernelRequest($evt);
34
    }
35
36
    public function testOnKernelRequestIgnoredRoute()
37
    {
38
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
39
        $evt = $this->createMock(GetResponseEvent::class);
40
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
41
        $evt->expects(self::any())->method('isMasterRequest')->willReturn(true);
42
43
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
44
        $generator1->expects(self::never())->method('collectRequest');
45
        $generator2 = $this->createMock(MetricsGeneratorInterface::class);
46
        $generator2->expects(self::never())->method('collectRequest');
47
48
        $registry = new MetricsGeneratorRegistry();
49
        $registry->registerMetricsGenerator($generator1);
50
        $registry->registerMetricsGenerator($generator2);
51
52
        $listener = new RequestCounterListener($registry, ['test_route']);
53
        $listener->onKernelRequest($evt);
54
    }
55
56
    public function testOnKernelRequestNonMasterRequest()
57
    {
58
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
59
        $evt = $this->createMock(GetResponseEvent::class);
60
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
61
        $evt->expects(self::any())->method('isMasterRequest')->willReturn(false);
62
63
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
64
        $generator1->expects(self::never())->method('collectRequest');
65
        $generator2 = $this->createMock(MetricsGeneratorInterface::class);
66
        $generator2->expects(self::never())->method('collectRequest');
67
68
        $registry = new MetricsGeneratorRegistry();
69
        $registry->registerMetricsGenerator($generator1);
70
        $registry->registerMetricsGenerator($generator2);
71
72
        $listener = new RequestCounterListener($registry);
73
        $listener->onKernelRequest($evt);
74
    }
75
76
    public function testOnKernelRequestExceptionHandlingWithLog()
77
    {
78
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
79
        $evt = $this->createMock(GetResponseEvent::class);
80
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
81
        $evt->expects(self::any())->method('isMasterRequest')->willReturn(true);
82
83
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
84
        $generator1->expects(self::once())->method('collectRequest')->willThrowException(new \Exception('test exception'));
85
86
        $registry = new MetricsGeneratorRegistry();
87
        $registry->registerMetricsGenerator($generator1);
88
89
        $listener = new RequestCounterListener($registry);
90
        $logger = new BufferingLogger();
91
        $listener->setLogger($logger);
92
        $listener->onKernelRequest($evt);
93
        $logs = $logger->cleanLogs();
94
        $this->assertCount(1, $logs);
95
        $this->assertCount(3, $logs[0]);
96
        $this->assertEquals('test exception', $logs[0][1]);
97
    }
98
99
    public function testOnKernelRequestExceptionHandlingWithoutLog()
100
    {
101
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
102
        $evt = $this->createMock(GetResponseEvent::class);
103
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
104
        $evt->expects(self::any())->method('isMasterRequest')->willReturn(true);
105
106
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
107
        $generator1->expects(self::once())->method('collectRequest')->willThrowException(new \Exception('test exception'));
108
109
        $registry = new MetricsGeneratorRegistry();
110
        $registry->registerMetricsGenerator($generator1);
111
112
        $listener = new RequestCounterListener($registry);
113
        $listener->onKernelRequest($evt);
114
    }
115
116
    public function testOnKernelTerminate()
117
    {
118
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
119
        $evt = $this->createMock(PostResponseEvent::class);
120
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
121
122
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
123
        $generator1->expects(self::once())->method('collectResponse')->with($evt);
124
        $generator2 = $this->createMock(MetricsGeneratorInterface::class);
125
        $generator2->expects(self::once())->method('collectResponse')->with($evt);
126
127
        $registry = new MetricsGeneratorRegistry();
128
        $registry->registerMetricsGenerator($generator1);
129
        $registry->registerMetricsGenerator($generator2);
130
131
        $listener = new RequestCounterListener($registry);
132
        $listener->onKernelTerminate($evt);
133
    }
134
135
    public function testOnKernelTerminateExceptionHandlingWithLog()
136
    {
137
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
138
        $evt = $this->createMock(PostResponseEvent::class);
139
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
140
141
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
142
        $generator1->expects(self::once())->method('collectResponse')->willThrowException(new \Exception('test exception'));
143
144
        $registry = new MetricsGeneratorRegistry();
145
        $registry->registerMetricsGenerator($generator1);
146
147
        $listener = new RequestCounterListener($registry);
148
        $logger = new BufferingLogger();
149
        $listener->setLogger($logger);
150
        $listener->onKernelTerminate($evt);
151
        $logs = $logger->cleanLogs();
152
        $this->assertCount(1, $logs);
153
        $this->assertCount(3, $logs[0]);
154
        $this->assertEquals('test exception', $logs[0][1]);
155
    }
156
157
    public function testOnKernelTerminateExceptionHandlingWithoutLog()
158
    {
159
        $request = new Request([], [], ['_route' => 'test_route'], [], [], ['REQUEST_METHOD' => 'GET']);
160
        $evt = $this->createMock(PostResponseEvent::class);
161
        $evt->expects(self::any())->method('getRequest')->willReturn($request);
162
163
        $generator1 = $this->createMock(MetricsGeneratorInterface::class);
164
        $generator1->expects(self::once())->method('collectResponse')->willThrowException(new \Exception('test exception'));
165
166
        $registry = new MetricsGeneratorRegistry();
167
        $registry->registerMetricsGenerator($generator1);
168
169
        $listener = new RequestCounterListener($registry);
170
        $listener->onKernelTerminate($evt);
171
    }
172
}
173