Files
zemailnator/tests/Unit/NotifyMeTest.php
idevakk 68ef391c5d test: achieve 100% test coverage with comprehensive test suite fixes
- Fix Laravel bootstrap issues in TestCase setup
  - Add missing database factories (Setting, PremiumEmail, ActivationKey, etc.)
  - Convert Pest tests to PHPUnit style for compatibility
  - Fix model relationships and boolean casts
  - Add missing Filament resource actions and filters
  - Fix form validation and test data mismatches
  - Resolve assertion parameter order issues
  - Add proper configuration for test views
  - Fix searchable columns and table sorting
  - Simplify complex filter assertions for stability
2025-11-13 09:11:14 -08:00

157 lines
4.7 KiB
PHP

<?php
use App\Http\Controllers\WebhookController;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
// Create a test class that uses the trait
class TestNotifier
{
use App\NotifyMe;
}
class NotifyMeTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->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 = '<b>Bold text</b> and <i>italic text</i>';
$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);
}
}