fedi-feed-router/backend/tests/Feature/Http/Controllers/Api/V1/PlatformChannelsControllerTest.php
2025-08-15 18:20:19 +02:00

241 lines
No EOL
7.7 KiB
PHP

<?php
namespace Tests\Feature\Http\Controllers\Api\V1;
use Domains\Platform\Models\PlatformAccount;
use Domains\Platform\Models\PlatformChannel;
use Domains\Platform\Models\PlatformInstance;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PlatformChannelsControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_successful_response(): void
{
$instance = PlatformInstance::factory()->create();
PlatformChannel::factory()->count(3)->create(['platform_instance_id' => $instance->id]);
$response = $this->getJson('/api/v1/platform-channels');
$response->assertStatus(200)
->assertJsonStructure([
'success',
'message',
'data' => [
'*' => [
'id',
'platform_instance_id',
'channel_id',
'name',
'display_name',
'description',
'is_active',
'created_at',
'updated_at',
'platform_instance'
]
]
])
->assertJson([
'success' => true,
'message' => 'Platform channels retrieved successfully.'
]);
}
public function test_store_creates_platform_channel_successfully(): void
{
$instance = PlatformInstance::factory()->create();
// Create a platform account for this instance first
PlatformAccount::factory()->create([
'instance_url' => $instance->url,
'is_active' => true
]);
$data = [
'platform_instance_id' => $instance->id,
'channel_id' => 'test_channel',
'name' => 'Test Channel',
'display_name' => 'Test Channel Display',
'description' => 'A test channel',
'is_active' => true
];
$response = $this->postJson('/api/v1/platform-channels', $data);
$response->assertStatus(201)
->assertJsonStructure([
'success',
'message',
'data' => [
'id',
'platform_instance_id',
'channel_id',
'name',
'display_name',
'description',
'is_active',
'created_at',
'updated_at',
]
])
->assertJson([
'success' => true,
'message' => 'Platform channel created successfully and linked to platform account!'
]);
$this->assertDatabaseHas('platform_channels', [
'platform_instance_id' => $instance->id,
'channel_id' => 'test_channel',
'name' => 'Test Channel',
]);
}
public function test_store_validates_required_fields(): void
{
$response = $this->postJson('/api/v1/platform-channels', []);
$response->assertStatus(422)
->assertJsonValidationErrors(['platform_instance_id', 'channel_id', 'name']);
}
public function test_store_validates_platform_instance_exists(): void
{
$data = [
'platform_instance_id' => 999,
'channel_id' => 'test_channel',
'name' => 'Test Channel'
];
$response = $this->postJson('/api/v1/platform-channels', $data);
$response->assertStatus(422)
->assertJsonValidationErrors(['platform_instance_id']);
}
public function test_show_returns_platform_channel_successfully(): void
{
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]);
$response = $this->getJson("/api/v1/platform-channels/{$channel->id}");
$response->assertStatus(200)
->assertJsonStructure([
'success',
'message',
'data' => [
'id',
'platform_instance_id',
'channel_id',
'name',
'display_name',
'description',
'is_active',
'created_at',
'updated_at',
'platform_instance'
]
])
->assertJson([
'success' => true,
'message' => 'Platform channel retrieved successfully.',
'data' => [
'id' => $channel->id,
'name' => $channel->name,
]
]);
}
public function test_update_modifies_platform_channel_successfully(): void
{
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]);
$updateData = [
'name' => 'Updated Channel',
'display_name' => 'Updated Display Name',
'description' => 'Updated description',
'is_active' => false
];
$response = $this->putJson("/api/v1/platform-channels/{$channel->id}", $updateData);
$response->assertStatus(200)
->assertJson([
'success' => true,
'message' => 'Platform channel updated successfully!'
]);
$this->assertDatabaseHas('platform_channels', [
'id' => $channel->id,
'name' => 'Updated Channel',
'display_name' => 'Updated Display Name',
'is_active' => false,
]);
}
public function test_destroy_deletes_platform_channel_successfully(): void
{
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]);
$response = $this->deleteJson("/api/v1/platform-channels/{$channel->id}");
$response->assertStatus(200)
->assertJson([
'success' => true,
'message' => 'Platform channel deleted successfully!'
]);
$this->assertDatabaseMissing('platform_channels', [
'id' => $channel->id
]);
}
public function test_toggle_activates_inactive_channel(): void
{
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create([
'platform_instance_id' => $instance->id,
'is_active' => false
]);
$response = $this->postJson("/api/v1/platform-channels/{$channel->id}/toggle");
$response->assertStatus(200)
->assertJson([
'success' => true,
'message' => 'Platform channel activated successfully!'
]);
$this->assertDatabaseHas('platform_channels', [
'id' => $channel->id,
'is_active' => true
]);
}
public function test_toggle_deactivates_active_channel(): void
{
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create([
'platform_instance_id' => $instance->id,
'is_active' => true
]);
$response = $this->postJson("/api/v1/platform-channels/{$channel->id}/toggle");
$response->assertStatus(200)
->assertJson([
'success' => true,
'message' => 'Platform channel deactivated successfully!'
]);
$this->assertDatabaseHas('platform_channels', [
'id' => $channel->id,
'is_active' => false
]);
}
}