|
18 | 18 | use Monolog\Formatter\LineFormatter; |
19 | 19 | use Monolog\Level; |
20 | 20 | use Monolog\Test\MonologTestCase; |
| 21 | +use RuntimeException; |
21 | 22 | use stdClass; |
22 | 23 |
|
23 | 24 | /** |
|
28 | 29 | class ProcessingHandlerTest extends MonologTestCase |
29 | 30 | { |
30 | 31 |
|
| 32 | + /** |
| 33 | + * Tests the processing handler implementation. |
| 34 | + * |
| 35 | + * @covers \Lunr\Ticks\Monolog\ProcessingHandler::handle |
| 36 | + */ |
| 37 | + public function testHandleWithTraceIDUnavailable(): void |
| 38 | + { |
| 39 | + $record = $this->getRecord(Level::Warning, 'test', [ 'data' => new stdClass(), 'foo' => 34 ]); |
| 40 | + |
| 41 | + $event = $this->createMock(EventInterface::class); |
| 42 | + |
| 43 | + $event->expects($this->once()) |
| 44 | + ->method('setTimestamp') |
| 45 | + ->with($record->datetime->format('Uu')); |
| 46 | + |
| 47 | + $event->expects($this->never()) |
| 48 | + ->method('addTags'); |
| 49 | + |
| 50 | + $event->expects($this->never()) |
| 51 | + ->method('addFields'); |
| 52 | + |
| 53 | + $event->expects($this->never()) |
| 54 | + ->method('setTraceId'); |
| 55 | + |
| 56 | + $event->expects($this->never()) |
| 57 | + ->method('setSpanId'); |
| 58 | + |
| 59 | + $event->expects($this->never()) |
| 60 | + ->method('setParentSpanId'); |
| 61 | + |
| 62 | + $eventLogger = $this->createMock(EventLoggerInterface::class); |
| 63 | + |
| 64 | + $eventLogger->expects($this->once()) |
| 65 | + ->method('newEvent') |
| 66 | + ->willReturn($event); |
| 67 | + |
| 68 | + $controller = $this->createMockForIntersectionOfInterfaces([ |
| 69 | + TracingControllerInterface::class, |
| 70 | + TracingInfoInterface::class, |
| 71 | + ]); |
| 72 | + |
| 73 | + $controller->expects($this->once()) |
| 74 | + ->method('getTraceId') |
| 75 | + ->willReturn(NULL); |
| 76 | + |
| 77 | + $controller->expects($this->never()) |
| 78 | + ->method('getSpanId'); |
| 79 | + |
| 80 | + $controller->expects($this->never()) |
| 81 | + ->method('getParentSpanId'); |
| 82 | + |
| 83 | + $controller->expects($this->never()) |
| 84 | + ->method('getSpanSpecificTags'); |
| 85 | + |
| 86 | + $this->expectException(RuntimeException::class); |
| 87 | + $this->expectExceptionMessage('Trace ID not available!'); |
| 88 | + |
| 89 | + $handler = new ProcessingHandler($eventLogger, $controller); |
| 90 | + $handler->handle($record); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Tests the processing handler implementation. |
| 95 | + * |
| 96 | + * @covers \Lunr\Ticks\Monolog\ProcessingHandler::handle |
| 97 | + */ |
| 98 | + public function testHandleWithSpanIDUnavailable(): void |
| 99 | + { |
| 100 | + $traceID = '7b333e15-aa78-4957-a402-731aecbb358e'; |
| 101 | + |
| 102 | + $record = $this->getRecord(Level::Warning, 'test', [ 'data' => new stdClass(), 'foo' => 34 ]); |
| 103 | + |
| 104 | + $event = $this->createMock(EventInterface::class); |
| 105 | + |
| 106 | + $event->expects($this->once()) |
| 107 | + ->method('setTimestamp') |
| 108 | + ->with($record->datetime->format('Uu')); |
| 109 | + |
| 110 | + $event->expects($this->never()) |
| 111 | + ->method('addTags'); |
| 112 | + |
| 113 | + $event->expects($this->never()) |
| 114 | + ->method('addFields'); |
| 115 | + |
| 116 | + $event->expects($this->once()) |
| 117 | + ->method('setTraceId') |
| 118 | + ->with($traceID); |
| 119 | + |
| 120 | + $event->expects($this->never()) |
| 121 | + ->method('setSpanId'); |
| 122 | + |
| 123 | + $event->expects($this->never()) |
| 124 | + ->method('setParentSpanId'); |
| 125 | + |
| 126 | + $eventLogger = $this->createMock(EventLoggerInterface::class); |
| 127 | + |
| 128 | + $eventLogger->expects($this->once()) |
| 129 | + ->method('newEvent') |
| 130 | + ->willReturn($event); |
| 131 | + |
| 132 | + $controller = $this->createMockForIntersectionOfInterfaces([ |
| 133 | + TracingControllerInterface::class, |
| 134 | + TracingInfoInterface::class, |
| 135 | + ]); |
| 136 | + |
| 137 | + $controller->expects($this->once()) |
| 138 | + ->method('getTraceId') |
| 139 | + ->willReturn($traceID); |
| 140 | + |
| 141 | + $controller->expects($this->once()) |
| 142 | + ->method('getSpanId') |
| 143 | + ->willReturn(NULL); |
| 144 | + |
| 145 | + $controller->expects($this->never()) |
| 146 | + ->method('getParentSpanId'); |
| 147 | + |
| 148 | + $controller->expects($this->never()) |
| 149 | + ->method('getSpanSpecificTags'); |
| 150 | + |
| 151 | + $this->expectException(RuntimeException::class); |
| 152 | + $this->expectExceptionMessage('Span ID not available!'); |
| 153 | + |
| 154 | + $handler = new ProcessingHandler($eventLogger, $controller); |
| 155 | + $handler->handle($record); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Tests the processing handler implementation. |
| 160 | + * |
| 161 | + * @covers \Lunr\Ticks\Monolog\ProcessingHandler::handle |
| 162 | + */ |
| 163 | + public function testHandleWithParentSpanIDUnavailable(): void |
| 164 | + { |
| 165 | + $traceID = '7b333e15-aa78-4957-a402-731aecbb358e'; |
| 166 | + $spanID = '24ec5f90-7458-4dd5-bb51-7a1e8f4baafe'; |
| 167 | + |
| 168 | + $record = $this->getRecord(Level::Warning, 'test', [ 'data' => new stdClass(), 'foo' => 34 ]); |
| 169 | + |
| 170 | + $event = $this->createMock(EventInterface::class); |
| 171 | + |
| 172 | + $event->expects($this->once()) |
| 173 | + ->method('setTimestamp') |
| 174 | + ->with($record->datetime->format('Uu')); |
| 175 | + |
| 176 | + $event->expects($this->never()) |
| 177 | + ->method('addTags'); |
| 178 | + |
| 179 | + $event->expects($this->never()) |
| 180 | + ->method('addFields'); |
| 181 | + |
| 182 | + $event->expects($this->once()) |
| 183 | + ->method('setTraceId') |
| 184 | + ->with($traceID); |
| 185 | + |
| 186 | + $event->expects($this->once()) |
| 187 | + ->method('setSpanId') |
| 188 | + ->with($spanID); |
| 189 | + |
| 190 | + $event->expects($this->never()) |
| 191 | + ->method('setParentSpanId'); |
| 192 | + |
| 193 | + $eventLogger = $this->createMock(EventLoggerInterface::class); |
| 194 | + |
| 195 | + $eventLogger->expects($this->once()) |
| 196 | + ->method('newEvent') |
| 197 | + ->willReturn($event); |
| 198 | + |
| 199 | + $controller = $this->createMockForIntersectionOfInterfaces([ |
| 200 | + TracingControllerInterface::class, |
| 201 | + TracingInfoInterface::class, |
| 202 | + ]); |
| 203 | + |
| 204 | + $controller->expects($this->once()) |
| 205 | + ->method('getTraceId') |
| 206 | + ->willReturn($traceID); |
| 207 | + |
| 208 | + $controller->expects($this->once()) |
| 209 | + ->method('getSpanId') |
| 210 | + ->willReturn($spanID); |
| 211 | + |
| 212 | + $controller->expects($this->once()) |
| 213 | + ->method('getParentSpanId') |
| 214 | + ->willReturn(NULL); |
| 215 | + |
| 216 | + $controller->expects($this->never()) |
| 217 | + ->method('getSpanSpecificTags'); |
| 218 | + |
| 219 | + $this->expectException(RuntimeException::class); |
| 220 | + $this->expectExceptionMessage('Span ID not available!'); |
| 221 | + |
| 222 | + $handler = new ProcessingHandler($eventLogger, $controller); |
| 223 | + $handler->handle($record); |
| 224 | + } |
| 225 | + |
31 | 226 | /** |
32 | 227 | * Tests the processing handler implementation. |
33 | 228 | * |
@@ -61,14 +256,23 @@ public function testHandle(): void |
61 | 256 | $event->expects($this->once()) |
62 | 257 | ->method('addFields') |
63 | 258 | ->with([ |
64 | | - 'message' => (new LineFormatter())->format($record), |
65 | | - 'level' => $record->level->value, |
66 | | - 'line' => NULL, |
67 | | - 'traceID' => $traceID, |
68 | | - 'spanID' => $spanID, |
69 | | - 'parentSpanID' => $parentSpanID, |
| 259 | + 'message' => (new LineFormatter())->format($record), |
| 260 | + 'level' => $record->level->value, |
| 261 | + 'line' => NULL, |
70 | 262 | ]); |
71 | 263 |
|
| 264 | + $event->expects($this->once()) |
| 265 | + ->method('setTraceId') |
| 266 | + ->with($traceID); |
| 267 | + |
| 268 | + $event->expects($this->once()) |
| 269 | + ->method('setSpanId') |
| 270 | + ->with($spanID); |
| 271 | + |
| 272 | + $event->expects($this->once()) |
| 273 | + ->method('setParentSpanId') |
| 274 | + ->with($parentSpanID); |
| 275 | + |
72 | 276 | $eventLogger = $this->createMock(EventLoggerInterface::class); |
73 | 277 |
|
74 | 278 | $eventLogger->expects($this->once()) |
@@ -140,14 +344,23 @@ public function testHandleWithIntrospectionData(): void |
140 | 344 | $event->expects($this->once()) |
141 | 345 | ->method('addFields') |
142 | 346 | ->with([ |
143 | | - 'message' => (new LineFormatter())->format($record), |
144 | | - 'level' => $record->level->value, |
145 | | - 'line' => 101, |
146 | | - 'traceID' => $traceID, |
147 | | - 'spanID' => $spanID, |
148 | | - 'parentSpanID' => $parentSpanID, |
| 347 | + 'message' => (new LineFormatter())->format($record), |
| 348 | + 'level' => $record->level->value, |
| 349 | + 'line' => 101, |
149 | 350 | ]); |
150 | 351 |
|
| 352 | + $event->expects($this->once()) |
| 353 | + ->method('setTraceId') |
| 354 | + ->with($traceID); |
| 355 | + |
| 356 | + $event->expects($this->once()) |
| 357 | + ->method('setSpanId') |
| 358 | + ->with($spanID); |
| 359 | + |
| 360 | + $event->expects($this->once()) |
| 361 | + ->method('setParentSpanId') |
| 362 | + ->with($parentSpanID); |
| 363 | + |
151 | 364 | $eventLogger = $this->createMock(EventLoggerInterface::class); |
152 | 365 |
|
153 | 366 | $eventLogger->expects($this->once()) |
@@ -221,15 +434,24 @@ public function testHandleWithExceptionData(): void |
221 | 434 | $event->expects($this->once()) |
222 | 435 | ->method('addFields') |
223 | 436 | ->with([ |
224 | | - 'message' => (new LineFormatter())->format($record), |
225 | | - 'level' => $record->level->value, |
226 | | - 'line' => NULL, |
227 | | - 'traceID' => $traceID, |
228 | | - 'spanID' => $spanID, |
229 | | - 'parentSpanID' => $parentSpanID, |
230 | | - 'stacktrace' => $exception->getTraceAsString(), |
| 437 | + 'message' => (new LineFormatter())->format($record), |
| 438 | + 'level' => $record->level->value, |
| 439 | + 'line' => NULL, |
| 440 | + 'stacktrace' => $exception->getTraceAsString(), |
231 | 441 | ]); |
232 | 442 |
|
| 443 | + $event->expects($this->once()) |
| 444 | + ->method('setTraceId') |
| 445 | + ->with($traceID); |
| 446 | + |
| 447 | + $event->expects($this->once()) |
| 448 | + ->method('setSpanId') |
| 449 | + ->with($spanID); |
| 450 | + |
| 451 | + $event->expects($this->once()) |
| 452 | + ->method('setParentSpanId') |
| 453 | + ->with($parentSpanID); |
| 454 | + |
233 | 455 | $eventLogger = $this->createMock(EventLoggerInterface::class); |
234 | 456 |
|
235 | 457 | $eventLogger->expects($this->once()) |
@@ -299,15 +521,24 @@ public function testHandleWithGenericExtraData(): void |
299 | 521 | $event->expects($this->once()) |
300 | 522 | ->method('addFields') |
301 | 523 | ->with([ |
302 | | - 'message' => (new LineFormatter())->format($record), |
303 | | - 'level' => $record->level->value, |
304 | | - 'line' => NULL, |
305 | | - 'traceID' => $traceID, |
306 | | - 'spanID' => $spanID, |
307 | | - 'parentSpanID' => $parentSpanID, |
308 | | - 'meta' => 'bar', |
| 524 | + 'message' => (new LineFormatter())->format($record), |
| 525 | + 'level' => $record->level->value, |
| 526 | + 'line' => NULL, |
| 527 | + 'meta' => 'bar', |
309 | 528 | ]); |
310 | 529 |
|
| 530 | + $event->expects($this->once()) |
| 531 | + ->method('setTraceId') |
| 532 | + ->with($traceID); |
| 533 | + |
| 534 | + $event->expects($this->once()) |
| 535 | + ->method('setSpanId') |
| 536 | + ->with($spanID); |
| 537 | + |
| 538 | + $event->expects($this->once()) |
| 539 | + ->method('setParentSpanId') |
| 540 | + ->with($parentSpanID); |
| 541 | + |
311 | 542 | $eventLogger = $this->createMock(EventLoggerInterface::class); |
312 | 543 |
|
313 | 544 | $eventLogger->expects($this->once()) |
|
0 commit comments