Skip to main content
Version: 2.x

Available Assertions

Overview

The assertions are the ones that perform the checks to ensure that things are going as planned.

caution

The assertions must be called after the reply() method!

Example

We assume that the $this->bot is loaded as mentioned on the Introduction page.

$this
->bot
->hearText('foo')
->reply()
->assertReplyText('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.

$this
->bot
->hearText('foo')
->reply()
->assertReply('sendMessage', ['text' => 'bar'], 0);
->assertReply('sendMessage', ['text' => 'baz'], 1);

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);
caution

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);
caution

You must use the willStartConversation() method to use this assertion without parameters.

assertNoReply

The assertNoReply asserts there is no reply.

->assertNoReply();

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:

$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;
});

OutgoingResource

This object is a wrapper from an outgoing resource and provide the following methods:

MethodTypeDescription
getName()?stringReturns the file name.
getType()?stringReturns the MIME content type.
getSize()intReturns the file size in bytes.
getError()intReturns 0 (There is no error) or 7 (Failed to create the resource)
getTmpResource()?resourceReturns the resource.