Available Assertions
Overview
The assertions are the ones that perform the checks to ensure that things are going as planned.
The assertions must be called after the reply() method!
Example
We assume that the $this->bot is loaded as mentioned on the Introduction page.
- Test
- Handler
$this->bot
->hearText('foo')
->reply()
->assertReplyText('bar');
$bot->onText('foo', function (Nutgram $bot) {
$bot->sendMessage('bar');
});
Available Assertions
assertRaw
The assertRaw asserts the given callback returns a truthy value.
The index parameter is optional.
->assertRaw(function(GuzzleHttp\Psr7\Request $request){
$content = json_decode((string)$request->getBody(), true, flags: JSON_THROW_ON_ERROR);
return $content['text'] === 'bar';
}, index: 0);
⚠️ Caution! See the Asserting files section to assert files sent with multi-part requests.
assertCalled
The assertCalled asserts the given method name is called one time.
The times parameter is optional.
->assertCalled('sendMessage', times: 1);
assertReply
The assertReply asserts the given method name is called in the index history.
The expected and index parameters are optionals.
->assertReply('sendMessage', expected: ['text' => 'foo'], index: 0);
⚠️ Caution! See the Asserting files section to assert files sent with multi-part requests.
- Test
- Handler
$this
->bot
->hearText('foo')
->reply()
->assertReply('sendMessage', ['text' => 'bar'], 0);
->assertReply('sendMessage', ['text' => 'baz'], 1);
$bot->onText('foo', function (Nutgram $bot) {
$bot->sendMessage('bar');
$bot->sendMessage('baz');
});
assertReplyMessage
The assertReplyMessage asserts the given data exists in the message attribute.
The index and forceMethod parameters are optionals.
->assertReplyMessage(['text' => 'bar'], index: 0, forceMethod:'sendMessage');
⚠️ Caution! See the Asserting files section to assert files sent with multi-part requests.
assertReplyText
The assertReplyText asserts the given value is equal to the message->text attribute.
The index parameter is optional.
->assertReplyText('bar', index: 0);
assertActiveConversation
The assertActiveConversation asserts there is an active conversation.
The parameters userId and chatId are optionals.
->assertActiveConversation(userId: 123 , chatId: 321);
You must use the willStartConversation() method to use this assertion without parameters.
assertNoConversation
The assertNoConversation asserts there is no active conversation.
The parameters userId and chatId are optionals.
->assertNoConversation(userId: 123 , chatId: 321);
You must use the willStartConversation() method to use this assertion without parameters.
assertNoReply
The assertNoReply asserts there is no reply.
->assertNoReply();
assertSequence
The assertSequence asserts the given method names are called in the index history.
->assertSequence(
fn (FakeNutgram $x) => $x->assertReplyText('foo'), // index: 0
fn (FakeNutgram $x) => $x->assertReplyText('bar'), // index: 1
fn (FakeNutgram $x) => $x->assertReplyText('baz'), // index: 2
);
Asserting files
When you send a file as resource via the following methods:
sendPhoto, sendAudio, sendDocument, sendVideo, sendAnimation, sendVoice, sendVideoNote or sendSticker,
Nutgram will use a multi-part request.
With a multi-part request, the assertions assertRaw, assertReply,
assertReplyMessage will have an OutgoingResource object instead of a resource
in the field containing the file.
Example
To assert files you must use the assertRaw assertion in combination with FormDataParser::parse($request)->files,
see the example below:
- Test
- Handler
$this
->bot
->hearText('foo')
->reply()
->assertReply('sendDocument', [
//'document' => ??? We can't assert `document` field here because is an OutgoingResource
'caption' => 'my caption',
'parse_mode' => ParseMode::HTML,
])
->assertRaw(function (Request $request) {
/** @var OutgoingResource $document */
$document = FormDataParser::parse($request)->files['document'];
//check size
return $document->getSize() <= 123456;
});
$bot->onText('foo', function (Nutgram $bot) {
$file = fopen('image.jpg', 'r');
$bot->sendDocument(
document: InputFile::make(file, 'name.jpg'),
caption: 'my caption',
parse_mode: ParseMode::HTML,
]);
fclose($file);
});
OutgoingResource
This object is a wrapper from an outgoing resource and provide the following methods:
| Method | Type | Description |
|---|---|---|
getName() | ?string | Returns the file name. |
getType() | ?string | Returns the MIME content type. |
getSize() | int | Returns the file size in bytes. |
getError() | int | Returns 0 (There is no error) or 7 (Failed to create the resource) |
getTmpResource() | ?resource | Returns the resource. |