mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-15 01:03:16 +01:00
Add additional tests for filament
This commit is contained in:
committed by
Constantin Graf
parent
0551c633a3
commit
08f9edbc96
52
tests/Unit/Filament/Widgets/ChartWidgetTestCase.php
Normal file
52
tests/Unit/Filament/Widgets/ChartWidgetTestCase.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Filament\Widgets;
|
||||
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Livewire\Features\SupportTesting\Testable;
|
||||
use Tests\Unit\Filament\FilamentTestCase;
|
||||
|
||||
abstract class ChartWidgetTestCase extends FilamentTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Config::set('auth.super_admins', ['admin@example.com']);
|
||||
$user = User::factory()->withPersonalOrganization()->create([
|
||||
'email' => 'admin@example.com',
|
||||
]);
|
||||
|
||||
$this->actingAs($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the protected getData() method of the chart widget of the given Livewire test.
|
||||
*
|
||||
* @param Testable<ChartWidget> $testable
|
||||
* @return array{datasets: array<int, array{label: string, data: Collection<int, int>}>, labels: Collection<int, string>}
|
||||
*/
|
||||
protected function getChartData(Testable $testable): array
|
||||
{
|
||||
/** @var ChartWidget $widget */
|
||||
$widget = $testable->instance();
|
||||
|
||||
/** @var array{datasets: array<int, array{label: string, data: Collection<int, int>}>, labels: Collection<int, string>} $data */
|
||||
$data = Closure::bind(fn (): array => $this->getData(), $widget, $widget)();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{datasets: array<int, array{label: string, data: Collection<int, int>}>, labels: Collection<int, string>} $data
|
||||
*/
|
||||
protected function sumOfChartData(array $data): int
|
||||
{
|
||||
return (int) $data['datasets'][0]['data']->sum();
|
||||
}
|
||||
}
|
||||
116
tests/Unit/Filament/Widgets/TimeEntriesCreatedWidgetTest.php
Normal file
116
tests/Unit/Filament/Widgets/TimeEntriesCreatedWidgetTest.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Filament\Widgets;
|
||||
|
||||
use App\Filament\Widgets\TimeEntriesCreated;
|
||||
use App\Models\TimeEntry;
|
||||
use Livewire\Livewire;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
|
||||
#[UsesClass(TimeEntriesCreated::class)]
|
||||
class TimeEntriesCreatedWidgetTest extends ChartWidgetTestCase
|
||||
{
|
||||
public function test_widget_renders_with_filters(): void
|
||||
{
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertSee('Time Entries Created');
|
||||
$response->assertSee('Last week');
|
||||
$response->assertSee('Last month');
|
||||
$response->assertSee('Last year');
|
||||
$response->assertSet('filter', 'week');
|
||||
}
|
||||
|
||||
public function test_counts_only_non_imported_time_entries_created_in_the_last_week_by_default(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(2), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(2), 'is_imported' => true],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame('Time Entries Created', $data['datasets'][0]['label']);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
$this->assertSame(now()->subWeek()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_month_filter_counts_time_entries_created_in_the_last_month_per_day(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => true],
|
||||
['created_at' => now()->subMonths(2), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class)
|
||||
->set('filter', 'month');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertDispatched('updateChartData');
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertSame(now()->subMonth()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_year_filter_counts_time_entries_created_in_the_last_year_per_month(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subMonths(3), 'is_imported' => false],
|
||||
['created_at' => now()->subMonths(3), 'is_imported' => true],
|
||||
['created_at' => now()->subYears(2), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class)
|
||||
->set('filter', 'year');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertCount(13, $data['labels']);
|
||||
$this->assertSame(now()->subYear()->format('Y-m'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_unknown_filter_falls_back_to_last_week(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class)
|
||||
->set('filter', 'unknown');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(1, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
}
|
||||
}
|
||||
116
tests/Unit/Filament/Widgets/TimeEntriesImportedWidgetTest.php
Normal file
116
tests/Unit/Filament/Widgets/TimeEntriesImportedWidgetTest.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Filament\Widgets;
|
||||
|
||||
use App\Filament\Widgets\TimeEntriesImported;
|
||||
use App\Models\TimeEntry;
|
||||
use Livewire\Livewire;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
|
||||
#[UsesClass(TimeEntriesImported::class)]
|
||||
class TimeEntriesImportedWidgetTest extends ChartWidgetTestCase
|
||||
{
|
||||
public function test_widget_renders_with_filters(): void
|
||||
{
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesImported::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertSee('Time Entries Imported');
|
||||
$response->assertSee('Last week');
|
||||
$response->assertSee('Last month');
|
||||
$response->assertSee('Last year');
|
||||
$response->assertSet('filter', 'week');
|
||||
}
|
||||
|
||||
public function test_counts_only_imported_time_entries_created_in_the_last_week_by_default(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => true],
|
||||
['created_at' => now()->subDays(2), 'is_imported' => true],
|
||||
['created_at' => now()->subDays(2), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => true],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesImported::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame('Time Entries Imported', $data['datasets'][0]['label']);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
$this->assertSame(now()->subWeek()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_month_filter_counts_time_entries_created_in_the_last_month_per_day(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => true],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => true],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => false],
|
||||
['created_at' => now()->subMonths(2), 'is_imported' => true],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesImported::class)
|
||||
->set('filter', 'month');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertDispatched('updateChartData');
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertSame(now()->subMonth()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_year_filter_counts_time_entries_created_in_the_last_year_per_month(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => true],
|
||||
['created_at' => now()->subMonths(3), 'is_imported' => true],
|
||||
['created_at' => now()->subMonths(3), 'is_imported' => false],
|
||||
['created_at' => now()->subYears(2), 'is_imported' => true],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesImported::class)
|
||||
->set('filter', 'year');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertCount(13, $data['labels']);
|
||||
$this->assertSame(now()->subYear()->format('Y-m'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_unknown_filter_falls_back_to_last_week(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => true],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => true],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesImported::class)
|
||||
->set('filter', 'unknown');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(1, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
}
|
||||
}
|
||||
117
tests/Unit/Filament/Widgets/UserRegistrationsWidgetTest.php
Normal file
117
tests/Unit/Filament/Widgets/UserRegistrationsWidgetTest.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Filament\Widgets;
|
||||
|
||||
use App\Filament\Widgets\UserRegistrations;
|
||||
use App\Models\User;
|
||||
use Livewire\Livewire;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
|
||||
#[UsesClass(UserRegistrations::class)]
|
||||
class UserRegistrationsWidgetTest extends ChartWidgetTestCase
|
||||
{
|
||||
public function test_widget_renders_with_filters(): void
|
||||
{
|
||||
// Act
|
||||
$response = Livewire::test(UserRegistrations::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertSee('User Registrations');
|
||||
$response->assertSee('Last week');
|
||||
$response->assertSee('Last month');
|
||||
$response->assertSee('Last year');
|
||||
$response->assertSet('filter', 'week');
|
||||
}
|
||||
|
||||
public function test_counts_only_non_placeholder_users_registered_in_the_last_week_by_default(): void
|
||||
{
|
||||
// Arrange
|
||||
// The super admin from setUp was created just now and is therefore counted as well.
|
||||
User::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_placeholder' => false],
|
||||
['created_at' => now()->subDays(2), 'is_placeholder' => false],
|
||||
['created_at' => now()->subDays(2), 'is_placeholder' => true],
|
||||
['created_at' => now()->subDays(10), 'is_placeholder' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(UserRegistrations::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame('User Registrations', $data['datasets'][0]['label']);
|
||||
$this->assertSame(3, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
$this->assertSame(now()->subWeek()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_month_filter_counts_users_registered_in_the_last_month_per_day(): void
|
||||
{
|
||||
// Arrange
|
||||
User::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_placeholder' => false],
|
||||
['created_at' => now()->subDays(10), 'is_placeholder' => false],
|
||||
['created_at' => now()->subDays(10), 'is_placeholder' => true],
|
||||
['created_at' => now()->subMonths(2), 'is_placeholder' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(UserRegistrations::class)
|
||||
->set('filter', 'month');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertDispatched('updateChartData');
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(3, $this->sumOfChartData($data));
|
||||
$this->assertSame(now()->subMonth()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_year_filter_counts_users_registered_in_the_last_year_per_month(): void
|
||||
{
|
||||
// Arrange
|
||||
User::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_placeholder' => false],
|
||||
['created_at' => now()->subMonths(3), 'is_placeholder' => false],
|
||||
['created_at' => now()->subMonths(3), 'is_placeholder' => true],
|
||||
['created_at' => now()->subYears(2), 'is_placeholder' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(UserRegistrations::class)
|
||||
->set('filter', 'year');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(3, $this->sumOfChartData($data));
|
||||
$this->assertCount(13, $data['labels']);
|
||||
$this->assertSame(now()->subYear()->format('Y-m'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_unknown_filter_falls_back_to_last_week(): void
|
||||
{
|
||||
// Arrange
|
||||
User::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_placeholder' => false],
|
||||
['created_at' => now()->subDays(10), 'is_placeholder' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(UserRegistrations::class)
|
||||
->set('filter', 'unknown');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user