fedi-feed-router/backend/tests/Feature/DatabaseIntegrationTest.php
myrmidex d2416a3ae2 Fix tests
+ Move LogLevelEnum
2025-08-05 21:27:42 +02:00

352 lines
No EOL
11 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Article;
use App\Models\ArticlePublication;
use App\Models\Feed;
use App\Models\Keyword;
use App\Models\Language;
use App\Models\Log;
use App\Models\PlatformAccount;
use App\Models\PlatformChannel;
use App\Models\PlatformChannelPost;
use App\Models\PlatformInstance;
use App\Models\Route;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class DatabaseIntegrationTest extends TestCase
{
use RefreshDatabase;
public function test_user_model_creates_successfully(): void
{
$user = User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com'
]);
$this->assertDatabaseHas('users', [
'name' => 'Test User',
'email' => 'test@example.com'
]);
$this->assertEquals('Test User', $user->name);
$this->assertEquals('test@example.com', $user->email);
}
public function test_language_model_creates_successfully(): void
{
$language = Language::factory()->create([
'name' => 'English',
'short_code' => 'en'
]);
$this->assertDatabaseHas('languages', [
'name' => 'English',
'short_code' => 'en'
]);
}
public function test_platform_instance_model_creates_successfully(): void
{
$instance = PlatformInstance::factory()->create([
'name' => 'Test Instance',
'url' => 'https://test.lemmy.world'
]);
$this->assertDatabaseHas('platform_instances', [
'name' => 'Test Instance',
'url' => 'https://test.lemmy.world'
]);
}
public function test_platform_account_model_creates_successfully(): void
{
$account = PlatformAccount::factory()->create([
'username' => 'testuser',
'is_active' => true
]);
$this->assertDatabaseHas('platform_accounts', [
'username' => 'testuser',
'is_active' => true
]);
$this->assertEquals('testuser', $account->username);
}
public function test_platform_channel_model_creates_successfully(): void
{
$language = Language::factory()->create();
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create([
'platform_instance_id' => $instance->id,
'language_id' => $language->id,
'name' => 'Test Channel',
'is_active' => true
]);
$this->assertDatabaseHas('platform_channels', [
'platform_instance_id' => $instance->id,
'language_id' => $language->id,
'name' => 'Test Channel',
'is_active' => true
]);
$this->assertEquals($instance->id, $channel->platformInstance->id);
$this->assertEquals($language->id, $channel->language->id);
}
public function test_feed_model_creates_successfully(): void
{
$language = Language::factory()->create();
$feed = Feed::factory()->create([
'language_id' => $language->id,
'name' => 'Test Feed',
'url' => 'https://example.com/feed.rss',
'is_active' => true
]);
$this->assertDatabaseHas('feeds', [
'language_id' => $language->id,
'name' => 'Test Feed',
'url' => 'https://example.com/feed.rss',
'is_active' => true
]);
$this->assertEquals($language->id, $feed->language->id);
}
public function test_article_model_creates_successfully(): void
{
$feed = Feed::factory()->create();
$article = Article::factory()->create([
'feed_id' => $feed->id,
'title' => 'Test Article',
'url' => 'https://example.com/article',
'approval_status' => 'pending'
]);
$this->assertDatabaseHas('articles', [
'feed_id' => $feed->id,
'title' => 'Test Article',
'url' => 'https://example.com/article',
'approval_status' => 'pending'
]);
$this->assertEquals($feed->id, $article->feed->id);
}
public function test_article_publication_model_creates_successfully(): void
{
$article = Article::factory()->create();
$channel = PlatformChannel::factory()->create();
$publication = ArticlePublication::create([
'article_id' => $article->id,
'platform_channel_id' => $channel->id,
'post_id' => 'test-post-123',
'published_at' => now(),
'published_by' => 'test-user'
]);
$this->assertDatabaseHas('article_publications', [
'article_id' => $article->id,
'platform_channel_id' => $channel->id,
'post_id' => 'test-post-123',
'published_by' => 'test-user'
]);
$this->assertEquals($article->id, $publication->article->id);
$this->assertEquals($channel->id, $publication->platform_channel_id);
}
public function test_route_model_creates_successfully(): void
{
$feed = Feed::factory()->create();
$channel = PlatformChannel::factory()->create();
$route = Route::factory()->create([
'feed_id' => $feed->id,
'platform_channel_id' => $channel->id,
'is_active' => true
]);
$this->assertDatabaseHas('routes', [
'feed_id' => $feed->id,
'platform_channel_id' => $channel->id,
'is_active' => true
]);
$this->assertEquals($feed->id, $route->feed->id);
$this->assertEquals($channel->id, $route->platformChannel->id);
}
public function test_platform_channel_post_model_creates_successfully(): void
{
$post = PlatformChannelPost::create([
'platform' => 'lemmy',
'channel_id' => 'technology',
'post_id' => 'external-post-123',
'title' => 'Test Post',
'url' => 'https://example.com/post',
'posted_at' => now()
]);
$this->assertDatabaseHas('platform_channel_posts', [
'platform' => 'lemmy',
'channel_id' => 'technology',
'post_id' => 'external-post-123',
'title' => 'Test Post'
]);
$this->assertEquals('external-post-123', $post->post_id);
}
public function test_keyword_model_creates_successfully(): void
{
$feed = Feed::factory()->create();
$channel = PlatformChannel::factory()->create();
$keyword = Keyword::factory()
->forFeed($feed)
->forChannel($channel)
->create([
'keyword' => 'test keyword',
'is_active' => true
]);
$this->assertDatabaseHas('keywords', [
'keyword' => 'test keyword',
'is_active' => true,
'feed_id' => $feed->id,
'platform_channel_id' => $channel->id
]);
}
public function test_log_model_creates_successfully(): void
{
$log = Log::create([
'level' => 'info',
'message' => 'Test log message',
'context' => json_encode(['key' => 'value']),
'logged_at' => now()
]);
$this->assertDatabaseHas('logs', [
'level' => 'info',
'message' => 'Test log message'
]);
}
public function test_setting_model_creates_successfully(): void
{
$setting = Setting::create([
'key' => 'test_setting',
'value' => 'test_value'
]);
$this->assertDatabaseHas('settings', [
'key' => 'test_setting',
'value' => 'test_value'
]);
}
public function test_feed_articles_relationship(): void
{
$feed = Feed::factory()->create();
$articles = Article::factory()->count(3)->create(['feed_id' => $feed->id]);
$this->assertCount(3, $feed->articles);
foreach ($articles as $article) {
$this->assertTrue($feed->articles->contains($article));
}
}
public function test_platform_account_channels_many_to_many_relationship(): void
{
$account = PlatformAccount::factory()->create();
$channel = PlatformChannel::factory()->create();
// Test the pivot table relationship
$account->channels()->attach($channel->id, ['is_active' => true, 'priority' => 1]);
$this->assertDatabaseHas('platform_account_channels', [
'platform_account_id' => $account->id,
'platform_channel_id' => $channel->id,
'is_active' => true
]);
}
public function test_language_platform_instances_relationship(): void
{
$language = Language::factory()->create();
$instances = PlatformInstance::factory()->count(2)->create();
// Attach language to instances via pivot table
foreach ($instances as $instance) {
$language->platformInstances()->attach($instance->id);
}
$this->assertCount(2, $language->platformInstances);
foreach ($instances as $instance) {
$this->assertTrue($language->platformInstances->contains($instance));
}
}
public function test_model_soft_deletes_work_correctly(): void
{
// Test models that might use soft deletes
$feed = Feed::factory()->create();
$feedId = $feed->id;
$feed->delete();
// Should not find with normal query if soft deleted
$this->assertNull(Feed::find($feedId));
// Should find with withTrashed if model uses soft deletes
if (method_exists($feed, 'withTrashed')) {
$this->assertNotNull(Feed::withTrashed()->find($feedId));
}
}
public function test_database_constraints_are_enforced(): void
{
// Test foreign key constraints
$this->expectException(\Illuminate\Database\QueryException::class);
// Try to create article with non-existent feed_id
Article::factory()->create(['feed_id' => 99999]);
}
public function test_all_factories_work_correctly(): void
{
// Test that all model factories can create valid records
$models = [
User::factory()->make(),
Language::factory()->make(),
PlatformInstance::factory()->make(),
PlatformAccount::factory()->make(),
PlatformChannel::factory()->make(),
Feed::factory()->make(),
Article::factory()->make(),
Route::factory()->make(),
Keyword::factory()->make(),
];
foreach ($models as $model) {
$this->assertNotNull($model);
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Model::class, $model);
}
}
}