Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Update::getChat() for cases when a User banned and/or unbanned a bot #977

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Objects/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ protected function getRelationValue(string $relationName, iterable $relationRawD
/** @var class-string<\Telegram\Bot\Objects\BaseObject>|list<class-string<\Telegram\Bot\Objects\BaseObject>> $relation */
$relation = $this->relations()[$relationName];

if (is_string($relation) && class_exists($relation)) {
if (is_string($relation)) {
if (! class_exists($relation)) {
throw new \InvalidArgumentException("Could not load “{$relationName}” relation: class “{$relation}” not found.");
}
return $relation::make($relationRawData);
}

Expand All @@ -102,7 +105,7 @@ protected function getRelationValue(string $relationName, iterable $relationRawD
return $relatedObjects;
}

throw new \InvalidArgumentException("Unknown type of the relationship data for the $relationName relation.");
throw new \InvalidArgumentException("Unknown type of the relationship data for the “{$relationName}” relation.");
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Objects/ChatMemberUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
/**
* Class ChatMemberUpdated.
*
* @link https://core.telegram.org/bots/api#message
* @link https://core.telegram.org/bots/api#chatmemberupdated
*
* @property Chat $chat Chat the user belongs to.
* @property User $from Performer of the action, which resulted in the change.
* @property int $date Date the change was done in Unix time.
* @property ChatMember $old_chat_member Previous information about the chat member.
* @property ChatMember $new_chat_member New information about the chat member.
* @property ChatInviteLink|null $invite_link (Optional). Chat invite link, which was used by the user to join the chat; for joining by invite link events only.
* @property ChatMember $oldChatMember Previous information about the chat member.
* @property ChatMember $newChatMember New information about the chat member.
* @property ChatInviteLink|null $inviteLink (Optional). Chat invite link, which was used by the user to join the chat; for joining by invite link events only.
*/
class ChatMemberUpdated extends BaseObject
{
Expand Down
4 changes: 4 additions & 0 deletions src/Objects/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public function getRelatedObject()
*/
public function getChat(): Collection
{
if ($this->has('my_chat_member')) { // message is not available in such case
return $this->myChatMember->chat;
}

$message = $this->getMessage();

return $message->has('chat') ? $message->get('chat') : collect();
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Objects/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Telegram\Bot\Tests\Unit\Objects;

use PHPUnit\Framework\TestCase;
use Telegram\Bot\Objects\Chat;
use Telegram\Bot\Objects\Update;

/** @covers \Telegram\Bot\Objects\Update */
class UpdateTest extends TestCase
{
/** @test */
public function it_parses_chat_relation_for_bot_kicked_event(): void
{
$update = new Update([
// there is no "message" key at this even type!
'my_chat_member' => [
'chat' => [
'id' => 42,
'first_name' => 'Firstname',
'last_name' => 'Lastname',
'type' => 'private',
],
],
'old_chat_member' => [
'user' => [], // doesn't matter in this case
'status' => 'member',
],
'new_chat_member' => [
'user' => [], // doesn't matter in this case
'status' => 'kicked',
'until_date' => 0,
],
]);

$this->assertInstanceOf(Chat::class, $update->getChat());
}
}