-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 improve error handling with telegram api
fixes #2815
- Loading branch information
1 parent
fbe5608
commit 024e01e
Showing
7 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class TelegramException extends Exception | ||
{ | ||
// | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Exceptions\TelegramException; | ||
use App\Services\TelegramService; | ||
use Illuminate\Support\Facades\Http; | ||
use Tests\FeatureTestCase; | ||
|
||
class TelegramServiceTest extends FeatureTestCase | ||
{ | ||
|
||
private const CHAT_ID = '123456789'; | ||
private const TOKEN = '123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'; | ||
|
||
public function testSendTelegramMessage(): void { | ||
Http::fake(['https://api.telegram.org/bot' . self::TOKEN . '/sendMessage' => Http::response(['result' => ['message_id' => 123]])]); | ||
$telegramService = new TelegramService(self::CHAT_ID, self::TOKEN); | ||
$messageId = $telegramService->sendMessage('Hello, World!'); | ||
$this->assertIsInt($messageId); | ||
} | ||
|
||
public function testSendTelegramMessageWithWrongCredentials(): void { | ||
Http::fake([ | ||
'https://api.telegram.org/bot' . self::TOKEN . '/sendMessage' => Http::response( | ||
body: [ | ||
'ok' => false, | ||
'error_code' => 401, | ||
'description' => 'Unauthorized', | ||
], | ||
status: 401 | ||
) | ||
]); | ||
$telegramService = new TelegramService(self::CHAT_ID, self::TOKEN); | ||
$this->expectException(TelegramException::class); | ||
$telegramService->sendMessage('Hello, World!'); | ||
} | ||
|
||
public function testDeleteTelegramMessage(): void { | ||
Http::fake(['https://api.telegram.org/bot' . self::TOKEN . '/deleteMessage' => Http::response()]); | ||
$telegramService = new TelegramService(self::CHAT_ID, self::TOKEN); | ||
$this->assertTrue($telegramService->deleteMessage(123)); | ||
} | ||
|
||
public function testAdminChatSelector(): void { | ||
config(['services.telegram.admin.active' => true]); | ||
$this->assertTrue(TelegramService::isAdminActive()); | ||
|
||
config(['services.telegram.admin.chat_id' => self::CHAT_ID]); | ||
config(['services.telegram.admin.token' => self::TOKEN]); | ||
$telegramService = TelegramService::admin(); | ||
$this->assertEquals(self::CHAT_ID, $telegramService->chatId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters