Skip to content

Commit 3cef79c

Browse files
committed
chore: test messenger instrumentation
1 parent 098304b commit 3cef79c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/Instrumentation/Symfony/tests/Integration/MessengerInstrumentationTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public function getMessage(): string
2929
}
3030
}
3131

32+
final class SendEmailMessageHandler
33+
{
34+
public function __invoke(SendEmailMessage $message)
35+
{
36+
// Handler logic
37+
return 'handled';
38+
}
39+
}
40+
3241
final class MessengerInstrumentationTest extends AbstractTest
3342
{
3443
protected function getMessenger(): MessageBusInterface
@@ -146,6 +155,61 @@ public function send(Envelope $envelope): Envelope
146155
}
147156
}
148157

158+
public function test_handle_message()
159+
{
160+
$bus = $this->getMessenger();
161+
$transport = $this->getTransport();
162+
$worker = new \Symfony\Component\Messenger\Worker(
163+
['transport' => $transport],
164+
$bus
165+
);
166+
167+
// Send a message to the transport
168+
$message = new SendEmailMessage('Hello Again');
169+
$envelope = new Envelope($message);
170+
$transport->send($envelope);
171+
172+
// Get and handle the message
173+
$messages = iterator_to_array($transport->get());
174+
$message = $messages[0];
175+
176+
// Use reflection to call the protected handleMessage method
177+
$reflection = new \ReflectionClass($worker);
178+
$handleMessageMethod = $reflection->getMethod('handleMessage');
179+
$handleMessageMethod->setAccessible(true);
180+
$handleMessageMethod->invoke($worker, $message, 'transport');
181+
182+
// We should have 3 spans: send, dispatch, and consume
183+
$this->assertCount(3, $this->storage);
184+
185+
// Check the send span
186+
$sendSpan = $this->storage[0];
187+
$this->assertEquals(
188+
'SEND OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration\SendEmailMessage',
189+
$sendSpan->getName()
190+
);
191+
$this->assertEquals(SpanKind::KIND_PRODUCER, $sendSpan->getKind());
192+
193+
// Check the dispatch span
194+
$dispatchSpan = $this->storage[1];
195+
$this->assertEquals(
196+
'DISPATCH Symfony\Component\Messenger\Envelope',
197+
$dispatchSpan->getName()
198+
);
199+
$this->assertEquals(SpanKind::KIND_PRODUCER, $dispatchSpan->getKind());
200+
201+
// Check the consumer span
202+
$consumeSpan = $this->storage[2];
203+
$this->assertEquals(
204+
'CONSUME OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration\SendEmailMessage',
205+
$consumeSpan->getName()
206+
);
207+
$this->assertEquals(SpanKind::KIND_CONSUMER, $consumeSpan->getKind());
208+
$this->assertTrue($consumeSpan->getAttributes()->has(MessengerInstrumentation::ATTRIBUTE_MESSAGING_SYSTEM));
209+
$this->assertEquals('symfony', $consumeSpan->getAttributes()->get(MessengerInstrumentation::ATTRIBUTE_MESSAGING_SYSTEM));
210+
$this->assertEquals('receive', $consumeSpan->getAttributes()->get(MessengerInstrumentation::ATTRIBUTE_MESSAGING_OPERATION));
211+
}
212+
149213
public function sendDataProvider(): array
150214
{
151215
return [

0 commit comments

Comments
 (0)