Mock incoming responses
Overview
After you have configured your hearing method, to test a custom response from Telegram you have to mock the received data. To do that, the framework gives you some methods to automatically generate incoming responses.
Example
We assume that the $this->bot
is loaded as mentioned on the Introduction page.
- Test
- Handler
$this
->bot
->hearText('dice')
->willReceivePartial(['dice' => ['emoji' => '🎲', 'value' => 4]])
->reply()
->assertReplyMessage(['dice' => ['emoji' => '🎲', 'value' => 4]]);
$bot->onText('dice', function (Nutgram $bot) {
$bot->sendDice(['emoji' => Emojis::DICE]);
});
Available Methods
willReceive
The willReceive
will mock an entire response.
The ok
parameter is optional.
->willReceive([
'message_id' => 1234,
'date' => 1647284950,
'from' => [
'id' => 123456789,
'is_bot' => true,
'username' => 'nutgrambot',
'first_name' => 'nutgrambot',
],
'chat' => [
'id' => 12345,
'type' => 'private',
'username' => 'nutgram',
'first_name' => 'foo',
'last_name' => 'bar',
],
'dice' => [
'emoji' => '🎲',
'value' => 4
]
], ok: true);
willReceivePartial
The willReceivePartial
will mock a partial response.
The ok
parameter is optional.
->willReceivePartial([
'dice' => [
'emoji' => '🎲',
'value' => 4
]
], ok: true);
willStartConversation
The willStartConversation
will cache userId
and chatId
.
Useful when used with assertActiveConversation
and assertNoConversation
.
The remember
parameter is optional.
->willStartConversation(remember: true);
withoutMiddleware
The withoutMiddleware
will remove the listed middlewares from the global middleware list.
->withoutMiddleware([FooMiddleware::class]);
overridemiddleware
The overridemiddleware
will override the global middleware list with the listed middlewares.
->overridemiddleware([BarMiddleware::class]);