8000 [Messenger] Fix `TraceableMessageBus` implementation so it can comput… · symfony/symfony@1423229 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1423229

Browse files
Ocramiusnicolas-grekas
authored andcommitted
[Messenger] Fix TraceableMessageBus implementation so it can compute caller even when used within a callback
1 parent 085a42f commit 1423229

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,22 @@ public function testItTracesExceptions()
156156
],
157157
], $actualTracedMessage);
158158
}
159+
160+
public function testItTracesExceptionsWhenMessageBusIsFiredFromArrayCallback()
161+
{
162+
$message = new DummyMessage('Hello');
163+
$exception = new \RuntimeException();
164+
165+
$bus = $this->createMock(MessageBusInterface::class);
166+
$bus->expects($this->once())
167+
->method('dispatch')
168+
->with($message)
169+
->willThrowException($exception);
170+
171+
$traceableBus = new TraceableMessageBus($bus);
172+
173+
$this->expectExceptionObject($exception);
174+
175+
array_map([$traceableBus, 'dispatch'], [$message]);
176+
}
159177
}

src/Symfony/Component/Messenger/TraceableMessageBus.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ private function getCaller(): array
6262
{
6363
$trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 8);
6464

65-
$file = $trace[1]['file'];
66-
$line = $trace[1]['line'];
65+
$file = $trace[1]['file'] ?? null;
66+
$line = $trace[1]['line'] ?? null;
6767

6868
$handleTraitFile = (new \ReflectionClass(HandleTrait::class))->getFileName();
6969
$found = false;
@@ -97,9 +97,12 @@ private function getCaller(): array
9797
}
9898
}
9999

100-
$name = str_replace('\\', '/', $file);
101-
$name = substr($name, strrpos($name, '/') + 1);
100+
$name = str_replace('\\', '/', (string) $file);
102101

103-
return compact('name', 'file', 'line');
102+
return [
103+
'name' => substr($name, strrpos($name, '/') + 1),
104+
'file' => $file,
105+
'line' => $line,
106+
];
104107
}
105108
}

0 commit comments

Comments
 (0)
0