notifier = new TestNotifier; } /** @test */ public function it_sends_telegram_notification_successfully() { Config::set('app.notify_tg_bot_token', 'test_bot_token'); Config::set('app.notify_tg_chat_id', 'test_chat_id'); Http::fake([ 'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([ 'ok' => true, 'result' => ['message_id' => 123], ], 200), ]); $result = $this->notifier->sendTelegramNotification('Test message'); $this->assertTrue($result); Http::assertSent(function ($request) { return $request->url() === 'https://api.telegram.org/bottest_bot_token/sendMessage' && $request['chat_id'] === 'test_chat_id' && $request['text'] === 'Test message' && $request['parse_mode'] === 'HTML'; }); } /** @test */ public function it_fails_when_bot_token_is_not_configured() { Config::set('app.notify_tg_bot_token', null); Config::set('app.notify_tg_chat_id', 'test_chat_id'); Log::shouldReceive('error') ->once() ->with('Telegram bot token or chat ID not configured'); $result = $this->notifier->sendTelegramNotification('Test message'); $this->assertFalse($result); } /** @test */ public function it_fails_when_chat_id_is_not_configured() { Config::set('app.notify_tg_bot_token', 'test_bot_token'); Config::set('app.notify_tg_chat_id', null); Log::shouldReceive('error') ->once() ->with('Telegram bot token or chat ID not configured'); $result = $this->notifier->sendTelegramNotification('Test message'); $this->assertFalse($result); } /** @test */ public function it_handles_http_errors_gracefully() { Config::set('app.notify_tg_bot_token', 'test_bot_token'); Config::set('app.notify_tg_chat_id', 'test_chat_id'); Http::fake([ 'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([ 'ok' => false, 'error_code' => 400, 'description' => 'Bad Request', ], 400), ]); $result = $this->notifier->sendTelegramNotification('Test message'); $this->assertFalse($result); } /** @test */ public function it_handles_network_exceptions() { Config::set('app.notify_tg_bot_token', 'test_bot_token'); Config::set('app.notify_tg_chat_id', 'test_chat_id'); Http::fake([ 'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::throw(function ($request) { throw new \Exception('Network error'); }), ]); Log::shouldReceive('error') ->once(); $result = $this->notifier->sendTelegramNotification('Test message'); $this->assertFalse($result); } /** @test */ public function it_sends_messages_with_html_parsing_mode() { Config::set('app.notify_tg_bot_token', 'test_bot_token'); Config::set('app.notify_tg_chat_id', 'test_chat_id'); Http::fake([ 'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([ 'ok' => true, 'result' => ['message_id' => 123], ], 200), ]); $htmlMessage = 'Bold text and italic text'; $this->notifier->sendTelegramNotification($htmlMessage); Http::assertSent(function ($request) use ($htmlMessage) { return $request['parse_mode'] === 'HTML' && $request['text'] === $htmlMessage; }); } /** @test */ public function it_can_be_used_in_controller_context() { Config::set('app.notify_tg_bot_token', 'test_bot_token'); Config::set('app.notify_tg_chat_id', 'test_chat_id'); Http::fake([ 'https://api.telegram.org/bottest_bot_token/sendMessage' => Http::response([ 'ok' => true, 'result' => ['message_id' => 123], ], 200), ]); $controller = new WebhookController; $result = $controller->sendTelegramNotification('Test from controller'); $this->assertTrue($result); } }