183 lines
No EOL
7 KiB
PHP
183 lines
No EOL
7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
use App\Services\DashboardStatsService;
|
|
use App\Models\Article;
|
|
use App\Models\Feed;
|
|
use App\Models\PlatformChannel;
|
|
use App\Models\Route;
|
|
use App\Models\ArticlePublication;
|
|
use Tests\TestCase;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
class DashboardStatsServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected DashboardStatsService $dashboardStatsService;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->dashboardStatsService = new DashboardStatsService();
|
|
}
|
|
|
|
public function test_get_stats_returns_correct_structure(): void
|
|
{
|
|
$stats = $this->dashboardStatsService->getStats();
|
|
|
|
$this->assertIsArray($stats);
|
|
$this->assertArrayHasKey('articles_fetched', $stats);
|
|
$this->assertArrayHasKey('articles_published', $stats);
|
|
$this->assertArrayHasKey('published_percentage', $stats);
|
|
}
|
|
|
|
public function test_get_stats_with_today_period(): void
|
|
{
|
|
$feed = Feed::factory()->create();
|
|
$channel = PlatformChannel::factory()->create();
|
|
|
|
// Create articles for today
|
|
$todayArticle = Article::factory()->create([
|
|
'feed_id' => $feed->id,
|
|
'created_at' => now()
|
|
]);
|
|
|
|
// Create publication for today
|
|
ArticlePublication::factory()->create([
|
|
'article_id' => $todayArticle->id,
|
|
'platform_channel_id' => $channel->id,
|
|
'published_at' => now()
|
|
]);
|
|
|
|
$stats = $this->dashboardStatsService->getStats('today');
|
|
|
|
$this->assertEquals(1, $stats['articles_fetched']);
|
|
$this->assertEquals(1, $stats['articles_published']);
|
|
$this->assertEquals(100.0, $stats['published_percentage']);
|
|
}
|
|
|
|
public function test_get_stats_with_week_period(): void
|
|
{
|
|
$stats = $this->dashboardStatsService->getStats('week');
|
|
|
|
$this->assertArrayHasKey('articles_fetched', $stats);
|
|
$this->assertArrayHasKey('articles_published', $stats);
|
|
$this->assertArrayHasKey('published_percentage', $stats);
|
|
}
|
|
|
|
public function test_get_stats_with_all_time_period(): void
|
|
{
|
|
$feed = Feed::factory()->create();
|
|
|
|
// Create articles across different times
|
|
Article::factory()->count(5)->create(['feed_id' => $feed->id]);
|
|
|
|
$stats = $this->dashboardStatsService->getStats('all');
|
|
|
|
$this->assertEquals(5, $stats['articles_fetched']);
|
|
$this->assertIsFloat($stats['published_percentage']);
|
|
}
|
|
|
|
public function test_get_stats_calculates_percentage_correctly(): void
|
|
{
|
|
$feed = Feed::factory()->create();
|
|
$channel = PlatformChannel::factory()->create();
|
|
|
|
// Create 4 articles
|
|
$articles = Article::factory()->count(4)->create(['feed_id' => $feed->id]);
|
|
|
|
// Publish 2 of them
|
|
foreach ($articles->take(2) as $article) {
|
|
ArticlePublication::factory()->create([
|
|
'article_id' => $article->id,
|
|
'platform_channel_id' => $channel->id,
|
|
'published_at' => now()
|
|
]);
|
|
}
|
|
|
|
$stats = $this->dashboardStatsService->getStats('all');
|
|
|
|
$this->assertEquals(4, $stats['articles_fetched']);
|
|
$this->assertEquals(2, $stats['articles_published']);
|
|
$this->assertEquals(50.0, $stats['published_percentage']);
|
|
}
|
|
|
|
public function test_get_stats_handles_zero_articles(): void
|
|
{
|
|
$stats = $this->dashboardStatsService->getStats();
|
|
|
|
$this->assertEquals(0, $stats['articles_fetched']);
|
|
$this->assertEquals(0, $stats['articles_published']);
|
|
$this->assertEquals(0.0, $stats['published_percentage']);
|
|
}
|
|
|
|
public function test_get_available_periods_returns_correct_options(): void
|
|
{
|
|
$periods = $this->dashboardStatsService->getAvailablePeriods();
|
|
|
|
$this->assertIsArray($periods);
|
|
$this->assertArrayHasKey('today', $periods);
|
|
$this->assertArrayHasKey('week', $periods);
|
|
$this->assertArrayHasKey('month', $periods);
|
|
$this->assertArrayHasKey('year', $periods);
|
|
$this->assertArrayHasKey('all', $periods);
|
|
|
|
$this->assertEquals('Today', $periods['today']);
|
|
$this->assertEquals('All Time', $periods['all']);
|
|
}
|
|
|
|
public function test_get_system_stats_returns_correct_structure(): void
|
|
{
|
|
$stats = $this->dashboardStatsService->getSystemStats();
|
|
|
|
$this->assertIsArray($stats);
|
|
$this->assertArrayHasKey('total_feeds', $stats);
|
|
$this->assertArrayHasKey('active_feeds', $stats);
|
|
$this->assertArrayHasKey('total_platform_accounts', $stats);
|
|
$this->assertArrayHasKey('active_platform_accounts', $stats);
|
|
$this->assertArrayHasKey('total_platform_channels', $stats);
|
|
$this->assertArrayHasKey('active_platform_channels', $stats);
|
|
$this->assertArrayHasKey('total_routes', $stats);
|
|
$this->assertArrayHasKey('active_routes', $stats);
|
|
}
|
|
|
|
public function test_get_system_stats_counts_correctly(): void
|
|
{
|
|
// Create a single feed, channel, and route to test counting
|
|
$feed = Feed::factory()->create(['is_active' => true]);
|
|
$channel = PlatformChannel::factory()->create(['is_active' => true]);
|
|
$route = Route::factory()->create(['is_active' => true]);
|
|
|
|
$stats = $this->dashboardStatsService->getSystemStats();
|
|
|
|
// Verify that all stats are properly counted (at least our created items exist)
|
|
$this->assertGreaterThanOrEqual(1, $stats['total_feeds']);
|
|
$this->assertGreaterThanOrEqual(1, $stats['active_feeds']);
|
|
$this->assertGreaterThanOrEqual(1, $stats['total_platform_channels']);
|
|
$this->assertGreaterThanOrEqual(1, $stats['active_platform_channels']);
|
|
$this->assertGreaterThanOrEqual(1, $stats['total_routes']);
|
|
$this->assertGreaterThanOrEqual(1, $stats['active_routes']);
|
|
|
|
// Verify that active counts are less than or equal to total counts
|
|
$this->assertLessThanOrEqual($stats['total_feeds'], $stats['active_feeds']);
|
|
$this->assertLessThanOrEqual($stats['total_platform_accounts'], $stats['active_platform_accounts']);
|
|
$this->assertLessThanOrEqual($stats['total_platform_channels'], $stats['active_platform_channels']);
|
|
$this->assertLessThanOrEqual($stats['total_routes'], $stats['active_routes']);
|
|
}
|
|
|
|
public function test_get_system_stats_handles_empty_database(): void
|
|
{
|
|
$stats = $this->dashboardStatsService->getSystemStats();
|
|
|
|
$this->assertEquals(0, $stats['total_feeds']);
|
|
$this->assertEquals(0, $stats['active_feeds']);
|
|
$this->assertEquals(0, $stats['total_platform_accounts']);
|
|
$this->assertEquals(0, $stats['active_platform_accounts']);
|
|
$this->assertEquals(0, $stats['total_platform_channels']);
|
|
$this->assertEquals(0, $stats['active_platform_channels']);
|
|
$this->assertEquals(0, $stats['total_routes']);
|
|
$this->assertEquals(0, $stats['active_routes']);
|
|
}
|
|
} |