fix notification chanels and more tests (#108)

* fix notification chanels and more tests

* fix code style
This commit is contained in:
Saeed Vaziry
2024-02-16 21:10:17 +01:00
committed by GitHub
parent b75df8e1c5
commit f70963d6bb
103 changed files with 484 additions and 112 deletions

View File

@ -0,0 +1,87 @@
<?php
namespace Tests\Unit\NotificationChannels;
use App\Models\NotificationChannel;
use App\NotificationChannels\Discord;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class DiscordTest extends TestCase
{
public function test_create_rules(): void
{
$provider = new Discord(NotificationChannel::factory()->create([
'provider' => 'discord',
]));
$this->assertSame([
'webhook_url' => 'required|url',
], $provider->createRules([]));
}
public function test_create_data(): void
{
$provider = new Discord(NotificationChannel::factory()->create([
'provider' => 'discord',
]));
$this->assertSame([
'webhook_url' => 'https://discord.com/xxxxx',
], $provider->createData([
'webhook_url' => 'https://discord.com/xxxxx',
]));
}
public function test_data(): void
{
$provider = new Discord(NotificationChannel::factory()->create([
'provider' => 'discord',
'data' => [
'webhook_url' => 'https://discord.com/xxxxx',
],
]));
$this->assertSame([
'webhook_url' => 'https://discord.com/xxxxx',
], $provider->data());
}
public function test_connect(): void
{
$provider = new Discord(NotificationChannel::factory()->create([
'provider' => 'discord',
'data' => [
'webhook_url' => 'https://discord.com/xxxxx',
],
]));
Http::fake();
$this->assertTrue($provider->connect());
Http::assertSent(function ($request) {
return $request->url() === 'https://discord.com/xxxxx';
});
}
public function test_send(): void
{
$channel = NotificationChannel::factory()->create([
'provider' => 'discord',
'data' => [
'webhook_url' => 'https://discord.com/xxxxx',
],
]);
$provider = new Discord($channel);
Http::fake();
$provider->send($channel, new TestNotification());
Http::assertSent(function (Request $request) {
return $request->body() === '{"content":"Hello"}';
});
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace Tests\Unit\NotificationChannels;
use App\Mail\NotificationMail;
use App\Models\NotificationChannel;
use App\NotificationChannels\Email;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class EmailTest extends TestCase
{
public function test_create_rules(): void
{
$provider = new Email(NotificationChannel::factory()->create([
'provider' => 'email',
]));
$this->assertSame([
'email' => 'required|email',
], $provider->createRules([]));
}
public function test_create_data(): void
{
$provider = new Email(NotificationChannel::factory()->create([
'provider' => 'email',
]));
$this->assertSame([
'email' => 'user@example.com',
], $provider->createData([
'email' => 'user@example.com',
]));
}
public function test_data(): void
{
$provider = new Email(NotificationChannel::factory()->create([
'provider' => 'email',
'data' => [
'email' => 'user@example.com',
],
]));
$this->assertSame([
'email' => 'user@example.com',
], $provider->data());
}
public function test_connect(): void
{
$provider = new Email(NotificationChannel::factory()->create([
'provider' => 'email',
'data' => [
'email' => 'user@example.com',
],
]));
Mail::fake();
$this->assertTrue($provider->connect());
Mail::assertSent(NotificationMail::class);
}
public function test_send(): void
{
$channel = NotificationChannel::factory()->create([
'provider' => 'email',
'data' => [
'email' => 'user@example.com',
],
]);
$provider = new Email($channel);
Mail::fake();
$provider->send($channel, new TestNotification());
Mail::assertSent(NotificationMail::class);
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace Tests\Unit\NotificationChannels;
use App\Models\NotificationChannel;
use App\NotificationChannels\Slack;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class SlackTest extends TestCase
{
public function test_create_rules(): void
{
$provider = new Slack(NotificationChannel::factory()->create([
'provider' => 'slack',
]));
$this->assertSame([
'webhook_url' => 'required|url',
], $provider->createRules([]));
}
public function test_create_data(): void
{
$provider = new Slack(NotificationChannel::factory()->create([
'provider' => 'slack',
]));
$this->assertSame([
'webhook_url' => 'https://slack.com/xxxxx',
], $provider->createData([
'webhook_url' => 'https://slack.com/xxxxx',
]));
}
public function test_data(): void
{
$provider = new Slack(NotificationChannel::factory()->create([
'provider' => 'slack',
'data' => [
'webhook_url' => 'https://slack.com/xxxxx',
],
]));
$this->assertSame([
'webhook_url' => 'https://slack.com/xxxxx',
], $provider->data());
}
public function test_connect(): void
{
$provider = new Slack(NotificationChannel::factory()->create([
'provider' => 'slack',
'data' => [
'webhook_url' => 'https://slack.com/xxxxx',
],
]));
Http::fake();
$this->assertTrue($provider->connect());
Http::assertSent(function ($request) {
return $request->url() === 'https://slack.com/xxxxx';
});
}
public function test_send(): void
{
$channel = NotificationChannel::factory()->create([
'provider' => 'slack',
'data' => [
'webhook_url' => 'https://slack.com/xxxxx',
],
]);
$provider = new Slack($channel);
Http::fake();
$provider->send($channel, new TestNotification());
Http::assertSent(function (Request $request) {
return $request->body() === '{"text":"Hello"}';
});
}
}

View File

@ -0,0 +1,108 @@
<?php
namespace Tests\Unit\NotificationChannels;
use App\Models\NotificationChannel;
use App\NotificationChannels\Telegram;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class TelegramTest extends TestCase
{
public function test_create_rules(): void
{
$provider = new Telegram(NotificationChannel::factory()->create([
'provider' => 'telegram',
]));
$this->assertSame([
'bot_token' => 'required|string',
'chat_id' => 'required',
], $provider->createRules([]));
}
public function test_create_data(): void
{
$provider = new Telegram(NotificationChannel::factory()->create([
'provider' => 'telegram',
]));
$this->assertSame([
'bot_token' => 'xxxxx',
'chat_id' => '12345',
], $provider->createData([
'bot_token' => 'xxxxx',
'chat_id' => '12345',
]));
}
public function test_data(): void
{
$provider = new Telegram(NotificationChannel::factory()->create([
'provider' => 'telegram',
'data' => [
'bot_token' => 'xxxxx',
'chat_id' => '12345',
],
]));
$this->assertSame([
'bot_token' => 'xxxxx',
'chat_id' => '12345',
], $provider->data());
}
public function test_connect(): void
{
$provider = new Telegram(NotificationChannel::factory()->create([
'provider' => 'telegram',
'data' => [
'bot_token' => 'xxxxx',
'chat_id' => '12345',
],
]));
Http::fake();
$this->assertTrue($provider->connect());
Http::assertSent(function ($request) {
if ($request->url() === 'https://api.telegram.org/botxxxxx/sendMessage') {
return $request->data() === [
'chat_id' => '12345',
'text' => 'Connected!',
'parse_mode' => 'markdown',
'disable_web_page_preview' => true,
];
}
});
}
public function test_send(): void
{
$channel = NotificationChannel::factory()->create([
'provider' => 'telegram',
'data' => [
'bot_token' => 'xxxxx',
'chat_id' => '12345',
],
]);
$provider = new Telegram($channel);
Http::fake();
$provider->send($channel, new TestNotification());
Http::assertSent(function (Request $request) {
if ($request->url() === 'https://api.telegram.org/botxxxxx/sendMessage') {
return $request->data() === [
'chat_id' => '12345',
'text' => 'Hello',
'parse_mode' => 'markdown',
'disable_web_page_preview' => true,
];
}
});
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Tests\Unit\NotificationChannels;
use App\Notifications\AbstractNotification;
class TestNotification extends AbstractNotification
{
public function rawText(): string
{
return 'Hello';
}
}