238 lines
7.7 KiB
PHP
238 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Livewire;
|
|
|
|
use App\Livewire\Dashboard;
|
|
use App\Models\Article;
|
|
use App\Models\Feed;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Carbon;
|
|
use Livewire\Features\SupportTesting\Testable;
|
|
use Livewire\Livewire;
|
|
use Tests\TestCase;
|
|
|
|
class DashboardTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_defaults_to_the_current_day(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->assertSet('from', '2026-07-15')
|
|
->assertSet('to', '2026-07-15');
|
|
}
|
|
|
|
public function test_it_recomputes_stats_when_the_range_changes(): void
|
|
{
|
|
Article::factory()->count(3)->create(['created_at' => Carbon::parse('2026-07-10 12:00:00')]);
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-01')
|
|
->set('to', '2026-07-31')
|
|
->assertSee('3')
|
|
->set('from', '2026-08-01')
|
|
->set('to', '2026-08-31')
|
|
->assertSee('0');
|
|
}
|
|
|
|
public function test_it_populates_the_range_when_a_preset_is_applied(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->call('applyPreset', 'month')
|
|
->assertSet('from', '2026-07-01')
|
|
->assertSet('to', '2026-07-31');
|
|
}
|
|
|
|
public function test_it_applies_the_all_preset_as_a_bounded_range(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->call('applyPreset', 'all')
|
|
->assertSet('to', '2026-07-15');
|
|
}
|
|
|
|
public function test_it_ignores_an_unknown_preset(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->call('applyPreset', 'fortnight')
|
|
->assertSet('from', '2026-07-15')
|
|
->assertSet('to', '2026-07-15')
|
|
->assertHasNoErrors();
|
|
}
|
|
|
|
public function test_it_reports_an_inverted_range_against_the_end_date(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-31')
|
|
->set('to', '2026-07-01')
|
|
->assertHasErrors('to');
|
|
}
|
|
|
|
public function test_it_blames_the_start_date_when_the_start_date_is_malformed(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', 'not-a-date')
|
|
->assertHasErrors('from')
|
|
->assertHasNoErrors('to');
|
|
}
|
|
|
|
public function test_it_blames_the_end_date_when_the_end_date_is_malformed(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('to', 'not-a-date')
|
|
->assertHasErrors('to')
|
|
->assertHasNoErrors('from');
|
|
}
|
|
|
|
public function test_it_reports_zero_stats_for_an_invalid_range(): void
|
|
{
|
|
Article::factory()->create(['created_at' => Carbon::parse('2026-07-10 12:00:00')]);
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-01')
|
|
->set('to', 'not-a-date')
|
|
->assertSet('rangeIsValid', false);
|
|
}
|
|
|
|
public function test_it_marks_a_well_formed_range_as_valid(): void
|
|
{
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-01')
|
|
->set('to', '2026-07-31')
|
|
->assertSet('rangeIsValid', true);
|
|
}
|
|
|
|
public function test_it_renders_the_preset_buttons(): void
|
|
{
|
|
Livewire::test(Dashboard::class)
|
|
->assertSee('This Month')
|
|
->assertSee('All Time');
|
|
}
|
|
|
|
public function test_it_renders_each_panel_in_its_own_island(): void
|
|
{
|
|
$html = Livewire::test(Dashboard::class)->html();
|
|
|
|
foreach (['system-overview', 'article-statistics'] as $island) {
|
|
$this->assertSame(
|
|
1,
|
|
substr_count($html, "[if FRAGMENT:type=island|name={$island}|"),
|
|
"Expected exactly one [{$island}] island.",
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Testable<Dashboard> $component
|
|
*/
|
|
private function islandFragments(Testable $component): string
|
|
{
|
|
/** @var array<int, string> $fragments */
|
|
$fragments = $component->effects['islandFragments'] ?? [];
|
|
|
|
return implode("\n", $fragments);
|
|
}
|
|
|
|
public function test_it_re_renders_the_article_statistics_island_when_the_range_changes(): void
|
|
{
|
|
Article::factory()->count(3)->create(['created_at' => Carbon::parse('2026-07-10 12:00:00')]);
|
|
|
|
$fragments = $this->islandFragments(
|
|
Livewire::test(Dashboard::class)->call('applyRange', '2026-07-01', '2026-07-31')
|
|
);
|
|
|
|
$this->assertMatchesRegularExpression('/Articles Fetched.*?>\s*3\s*</s', $fragments);
|
|
}
|
|
|
|
public function test_it_reflects_a_narrowed_range_in_the_re_rendered_island(): void
|
|
{
|
|
Article::factory()->count(3)->create(['created_at' => Carbon::parse('2026-07-10 12:00:00')]);
|
|
|
|
$fragments = $this->islandFragments(
|
|
Livewire::test(Dashboard::class)->call('applyRange', '2026-08-01', '2026-08-31')
|
|
);
|
|
|
|
$this->assertMatchesRegularExpression('/Articles Fetched.*?>\s*0\s*</s', $fragments);
|
|
}
|
|
|
|
public function test_it_refreshes_only_the_range_dependent_island(): void
|
|
{
|
|
$fragments = $this->islandFragments(
|
|
Livewire::test(Dashboard::class)->call('applyRange', '2026-07-01', '2026-07-31')
|
|
);
|
|
|
|
$this->assertStringContainsString('name=article-statistics|', $fragments);
|
|
$this->assertStringNotContainsString('name=system-overview|', $fragments);
|
|
$this->assertStringNotContainsString('Active Feeds', $fragments);
|
|
}
|
|
|
|
public function test_it_renders_the_articles_per_feed_breakdown_on_mount(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
$feed = Feed::factory()->create(['name' => 'Example Feed']);
|
|
|
|
Article::factory()->count(2)->create([
|
|
'feed_id' => $feed->id,
|
|
'created_at' => Carbon::parse('2026-07-15 09:00:00'),
|
|
]);
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->assertSee('Example Feed')
|
|
->assertSee('Articles per Feed');
|
|
}
|
|
|
|
public function test_it_counts_articles_per_feed_for_the_selected_range(): void
|
|
{
|
|
$feed = Feed::factory()->create(['name' => 'Example Feed']);
|
|
|
|
Article::factory()->count(2)->create([
|
|
'feed_id' => $feed->id,
|
|
'created_at' => Carbon::parse('2026-07-10 12:00:00'),
|
|
]);
|
|
|
|
$fragments = $this->islandFragments(
|
|
Livewire::test(Dashboard::class)->call('applyRange', '2026-07-01', '2026-07-31')
|
|
);
|
|
|
|
$this->assertMatchesRegularExpression('/Example Feed.*?>\s*2\s*/s', $fragments);
|
|
}
|
|
|
|
public function test_it_refreshes_the_articles_per_feed_island_when_the_range_changes(): void
|
|
{
|
|
Feed::factory()->create(['name' => 'Example Feed']);
|
|
|
|
$fragments = $this->islandFragments(
|
|
Livewire::test(Dashboard::class)->call('applyRange', '2026-07-01', '2026-07-31')
|
|
);
|
|
|
|
$this->assertStringContainsString('name=articles-per-feed|', $fragments);
|
|
$this->assertStringContainsString('Example Feed', $fragments);
|
|
}
|
|
|
|
public function test_it_re_renders_the_article_statistics_island_when_a_preset_is_applied(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Article::factory()->count(2)->create(['created_at' => Carbon::parse('2026-07-15 09:00:00')]);
|
|
|
|
$fragments = $this->islandFragments(
|
|
Livewire::test(Dashboard::class)->call('applyPreset', 'month')
|
|
);
|
|
|
|
$this->assertMatchesRegularExpression('/Articles Fetched.*?>\s*2\s*</s', $fragments);
|
|
}
|
|
}
|