From 7086e84c3ca69cff1f1b301cd9a5aa818bc06c0f Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Thu, 10 Oct 2024 23:24:07 +0200 Subject: [PATCH] migrating tests (Metrics, NotificationChannels, PHP, Profile and Projects) --- .../Monitoring/UpdateMetricSettings.php | 13 +- .../NotificationChannels/AddChannel.php | 22 ++-- app/Policies/MetricPolicy.php | 15 ++- app/Web/Pages/Servers/Metrics/Index.php | 38 +++++- app/Web/Pages/Servers/PHP/Index.php | 51 ++++---- .../Settings/NotificationChannels/Index.php | 8 +- .../Widgets/NotificationChannelsList.php | 2 +- app/Web/Pages/Settings/Projects/Index.php | 15 +-- app/Web/Pages/Settings/Projects/Settings.php | 15 ++- .../Projects/Widgets/ProjectsList.php | 2 + config/app.php | 1 - tests/Feature/MetricsTest.php | 20 +-- tests/Feature/NotificationChannelsTest.php | 116 +++++++++++------- tests/Feature/PHPTest.php | 98 +++++---------- tests/Feature/ProfileTest.php | 43 ++++--- tests/Feature/ProjectsTest.php | 45 ++++--- 16 files changed, 292 insertions(+), 212 deletions(-) diff --git a/app/Actions/Monitoring/UpdateMetricSettings.php b/app/Actions/Monitoring/UpdateMetricSettings.php index 31077ad..d4f5d63 100644 --- a/app/Actions/Monitoring/UpdateMetricSettings.php +++ b/app/Actions/Monitoring/UpdateMetricSettings.php @@ -3,15 +3,11 @@ namespace App\Actions\Monitoring; use App\Models\Server; -use Illuminate\Support\Facades\Validator; -use Illuminate\Validation\Rule; class UpdateMetricSettings { public function update(Server $server, array $input): void { - $this->validate($input); - $service = $server->monitoring(); $data = $service->handler()->data(); @@ -20,13 +16,14 @@ public function update(Server $server, array $input): void $service->save(); } - private function validate(array $input): void + public static function rules(): array { - Validator::make($input, [ + return [ 'data_retention' => [ 'required', - Rule::in(config('core.metrics_data_retention')), + 'numeric', + 'min:1', ], - ])->validate(); + ]; } } diff --git a/app/Actions/NotificationChannels/AddChannel.php b/app/Actions/NotificationChannels/AddChannel.php index 5e3ef93..0104cfb 100644 --- a/app/Actions/NotificationChannels/AddChannel.php +++ b/app/Actions/NotificationChannels/AddChannel.php @@ -4,6 +4,7 @@ use App\Models\NotificationChannel; use App\Models\User; +use Exception; use Illuminate\Validation\Rule; use Illuminate\Validation\ValidationException; @@ -11,6 +12,7 @@ class AddChannel { /** * @throws ValidationException + * @throws Exception */ public function add(User $user, array $input): void { @@ -23,18 +25,24 @@ public function add(User $user, array $input): void $channel->data = $channel->provider()->createData($input); $channel->save(); - if (! $channel->provider()->connect()) { - $channel->delete(); + try { + if (! $channel->provider()->connect()) { + $channel->delete(); + + if ($channel->provider === \App\Enums\NotificationChannel::EMAIL) { + throw ValidationException::withMessages([ + 'email' => __('Could not connect! Make sure you configured `.env` file correctly.'), + ]); + } - if ($channel->provider === \App\Enums\NotificationChannel::EMAIL) { throw ValidationException::withMessages([ - 'email' => __('Could not connect! Make sure you configured `.env` file correctly.'), + 'provider' => __('Could not connect'), ]); } + } catch (Exception $e) { + $channel->delete(); - throw ValidationException::withMessages([ - 'provider' => __('Could not connect'), - ]); + throw $e; } $channel->connected = true; diff --git a/app/Policies/MetricPolicy.php b/app/Policies/MetricPolicy.php index f358e2c..2ca7f5b 100644 --- a/app/Policies/MetricPolicy.php +++ b/app/Policies/MetricPolicy.php @@ -14,31 +14,36 @@ class MetricPolicy public function viewAny(User $user, Server $server): bool { return ($user->isAdmin() || $server->project->users->contains($user)) && - $server->service('monitoring'); + $server->service('monitoring') && + $server->isReady(); } public function view(User $user, Metric $metric): bool { return ($user->isAdmin() || $metric->server->project->users->contains($user)) && - $metric->server->service('monitoring'); + $metric->server->service('monitoring') && + $metric->server->isReady(); } public function create(User $user, Server $server): bool { return ($user->isAdmin() || $server->project->users->contains($user)) && - $server->service('monitoring'); + $server->service('monitoring') && + $server->isReady(); } public function update(User $user, Metric $metric): bool { return ($user->isAdmin() || $metric->server->project->users->contains($user)) && - $metric->server->service('monitoring'); + $metric->server->service('monitoring') && + $metric->server->isReady(); } public function delete(User $user, Metric $metric): bool { return ($user->isAdmin() || $metric->server->project->users->contains($user)) && - $metric->server->service('monitoring'); + $metric->server->service('monitoring') && + $metric->server->isReady(); } } diff --git a/app/Web/Pages/Servers/Metrics/Index.php b/app/Web/Pages/Servers/Metrics/Index.php index 3a22588..74216cd 100644 --- a/app/Web/Pages/Servers/Metrics/Index.php +++ b/app/Web/Pages/Servers/Metrics/Index.php @@ -2,8 +2,13 @@ namespace App\Web\Pages\Servers\Metrics; +use App\Actions\Monitoring\UpdateMetricSettings; use App\Models\Metric; use App\Web\Pages\Servers\Page; +use Filament\Actions\Action; +use Filament\Forms\Components\Select; +use Filament\Notifications\Notification; +use Filament\Support\Enums\MaxWidth; class Index extends Page { @@ -26,6 +31,37 @@ public function getWidgets(): array protected function getHeaderActions(): array { - return []; + return [ + Action::make('data-retention') + ->button() + ->color('gray') + ->icon('heroicon-o-trash') + ->label('Data Retention') + ->modalWidth(MaxWidth::Large) + ->form([ + Select::make('data_retention') + ->options([ + 7 => '7 days', + 14 => '14 days', + 30 => '30 days', + 60 => '60 days', + 90 => '90 days', + 180 => '180 days', + 365 => '365 days', + ]) + ->rules(UpdateMetricSettings::rules()['data_retention']) + ->label('Data Retention') + ->default($this->server->monitoring()?->type_data['data_retention'] ?? 30), + ]) + ->modalSubmitActionLabel('Save') + ->action(function (array $data) { + app(UpdateMetricSettings::class)->update($this->server, $data); + + Notification::make() + ->success() + ->title('Data retention updated') + ->send(); + }), + ]; } } diff --git a/app/Web/Pages/Servers/PHP/Index.php b/app/Web/Pages/Servers/PHP/Index.php index 7d3a155..1ad5475 100644 --- a/app/Web/Pages/Servers/PHP/Index.php +++ b/app/Web/Pages/Servers/PHP/Index.php @@ -7,8 +7,9 @@ use App\Web\Pages\Servers\Page; use App\Web\Pages\Servers\PHP\Widgets\PHPList; use Filament\Actions\Action; -use Filament\Actions\ActionGroup; -use Filament\Support\Enums\IconPosition; +use Filament\Forms\Components\Select; +use Filament\Notifications\Notification; +use Filament\Support\Enums\MaxWidth; class Index extends Page { @@ -30,31 +31,35 @@ public function getWidgets(): array protected function getHeaderActions(): array { - $phps = []; - foreach (config('core.php_versions') as $version) { - if (! $this->server->service('php', $version) && $version !== 'none') { - $phps[] = Action::make($version) - ->label($version) - ->requiresConfirmation() - ->modalHeading('Install PHP '.$version) - ->modalSubmitActionLabel('Install') - ->action(function () use ($version) { - app(InstallNewPHP::class)->install($this->server, ['version' => $version]); - - $this->dispatch('$refresh'); - }); - } - } + $installedPHPs = $this->server->installedPHPVersions(); return [ - ActionGroup::make($phps) + Action::make('install') ->authorize(fn () => auth()->user()?->can('create', [Service::class, $this->server])) ->label('Install PHP') - ->icon('heroicon-o-chevron-up-down') - ->iconPosition(IconPosition::After) - ->dropdownPlacement('bottom-end') - ->color('primary') - ->button(), + ->icon('heroicon-o-archive-box-arrow-down') + ->modalWidth(MaxWidth::Large) + ->form([ + Select::make('version') + ->options( + collect(config('core.php_versions')) + ->filter(fn ($version) => ! in_array($version, $installedPHPs)) + ->mapWithKeys(fn ($version) => [$version => $version]) + ->toArray() + ) + ->rules(InstallNewPHP::rules($this->server)['version']), + ]) + ->modalSubmitActionLabel('Install') + ->action(function (array $data) { + app(InstallNewPHP::class)->install($this->server, $data); + + Notification::make() + ->success() + ->title('Installing PHP...') + ->send(); + + $this->dispatch('$refresh'); + }), ]; } } diff --git a/app/Web/Pages/Settings/NotificationChannels/Index.php b/app/Web/Pages/Settings/NotificationChannels/Index.php index 4bd74b6..97cce9f 100644 --- a/app/Web/Pages/Settings/NotificationChannels/Index.php +++ b/app/Web/Pages/Settings/NotificationChannels/Index.php @@ -43,9 +43,13 @@ protected function getHeaderActions(): array ->authorize('create', NotificationChannel::class) ->modalWidth(MaxWidth::Large) ->action(function (array $data) { - Actions\Create::action($data); + try { + Actions\Create::action($data); - $this->dispatch('$refresh'); + $this->dispatch('$refresh'); + } catch (\Exception) { + $this->halt(); + } }), ]; } diff --git a/app/Web/Pages/Settings/NotificationChannels/Widgets/NotificationChannelsList.php b/app/Web/Pages/Settings/NotificationChannels/Widgets/NotificationChannelsList.php index 5a401c1..5bfbabd 100644 --- a/app/Web/Pages/Settings/NotificationChannels/Widgets/NotificationChannelsList.php +++ b/app/Web/Pages/Settings/NotificationChannels/Widgets/NotificationChannelsList.php @@ -70,7 +70,7 @@ public function getTable(): Table ->modalHeading('Delete Notification Channel') ->authorize(fn (NotificationChannel $record) => auth()->user()->can('delete', $record)) ->using(function (array $data, NotificationChannel $record) { - // + $record->delete(); }), ]); } diff --git a/app/Web/Pages/Settings/Projects/Index.php b/app/Web/Pages/Settings/Projects/Index.php index 1cd0444..b89dc96 100644 --- a/app/Web/Pages/Settings/Projects/Index.php +++ b/app/Web/Pages/Settings/Projects/Index.php @@ -5,7 +5,7 @@ use App\Actions\Projects\CreateProject; use App\Models\Project; use App\Web\Components\Page; -use Filament\Actions\CreateAction; +use Filament\Actions\Action; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; use Filament\Support\Enums\MaxWidth; @@ -42,13 +42,11 @@ public function getWidgets(): array protected function getHeaderActions(): array { return [ - CreateAction::make() + Action::make('create') ->label('Create Project') ->icon('heroicon-o-plus') ->authorize('create', Project::class) - ->using(function (array $data) { - return app(CreateProject::class)->create(auth()->user(), $data); - }) + ->modalWidth(MaxWidth::Large) ->form(function (Form $form) { return $form->schema([ TextInput::make('name') @@ -56,8 +54,11 @@ protected function getHeaderActions(): array ->rules(CreateProject::rules()['name']), ])->columns(1); }) - ->createAnother(false) - ->modalWidth(MaxWidth::Large), + ->action(function (array $data) { + app(CreateProject::class)->create(auth()->user(), $data); + + $this->dispatch('$refresh'); + }), ]; } } diff --git a/app/Web/Pages/Settings/Projects/Settings.php b/app/Web/Pages/Settings/Projects/Settings.php index 29e5009..2b938a8 100644 --- a/app/Web/Pages/Settings/Projects/Settings.php +++ b/app/Web/Pages/Settings/Projects/Settings.php @@ -9,7 +9,7 @@ use App\Web\Pages\Settings\Projects\Widgets\ProjectUsersList; use App\Web\Pages\Settings\Projects\Widgets\UpdateProject; use Exception; -use Filament\Actions\DeleteAction; +use Filament\Actions\Action; use Filament\Notifications\Notification; use Illuminate\Contracts\Support\Htmlable; @@ -54,17 +54,24 @@ public function getTitle(): string|Htmlable protected function getHeaderActions(): array { return [ - DeleteAction::make() + Action::make('delete') ->record($this->project) + ->color('danger') ->label('Delete Project') ->icon('heroicon-o-trash') ->modalHeading('Delete Project') ->modalDescription('Are you sure you want to delete this project? This action will delete all associated data and cannot be undone.') - ->using(function (Project $record) { + ->requiresConfirmation() + ->action(function (Project $record) { try { app(DeleteProject::class)->delete(auth()->user(), $record); - $this->redirectRoute(Index::getUrl()); + Notification::make() + ->success() + ->title('Project deleted successfully.') + ->send(); + + $this->redirect(Index::getUrl()); } catch (Exception $e) { Notification::make() ->title($e->getMessage()) diff --git a/app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php b/app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php index 12eb839..094a017 100644 --- a/app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php +++ b/app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php @@ -12,6 +12,8 @@ class ProjectsList extends Widget { + protected $listeners = ['$refresh']; + protected function getTableQuery(): Builder { return Project::query(); diff --git a/config/app.php b/config/app.php index 80f17a5..560e364 100644 --- a/config/app.php +++ b/config/app.php @@ -185,7 +185,6 @@ /* * Package Service Providers... */ - Illuminate\Concurrency\ConcurrencyServiceProvider::class, /* * Application Service Providers... diff --git a/tests/Feature/MetricsTest.php b/tests/Feature/MetricsTest.php index ce12713..1cfbc86 100644 --- a/tests/Feature/MetricsTest.php +++ b/tests/Feature/MetricsTest.php @@ -4,7 +4,9 @@ use App\Enums\ServiceStatus; use App\Models\Service; +use App\Web\Pages\Servers\Metrics\Index; use Illuminate\Foundation\Testing\RefreshDatabase; +use Livewire\Livewire; use Tests\TestCase; class MetricsTest extends TestCase @@ -23,7 +25,7 @@ public function test_visit_metrics(): void 'status' => ServiceStatus::READY, ]); - $this->get(route('servers.metrics', ['server' => $this->server])) + $this->get(Index::getUrl(['server' => $this->server])) ->assertSuccessful() ->assertSee('CPU Load') ->assertSee('Memory Usage') @@ -34,8 +36,8 @@ public function test_cannot_visit_metrics(): void { $this->actingAs($this->user); - $this->get(route('servers.metrics', ['server' => $this->server])) - ->assertNotFound(); + $this->get(Index::getUrl(['server' => $this->server])) + ->assertForbidden(); } public function test_update_data_retention(): void @@ -50,14 +52,18 @@ public function test_update_data_retention(): void 'status' => ServiceStatus::READY, ]); - $this->post(route('servers.metrics.settings', ['server' => $this->server]), [ - 'data_retention' => 30, - ])->assertSessionHas('toast.type', 'success'); + Livewire::test(Index::class, [ + 'server' => $this->server, + ]) + ->callAction('data-retention', [ + 'data_retention' => 365, + ]) + ->assertSuccessful(); $this->assertDatabaseHas('services', [ 'server_id' => $this->server->id, 'type' => 'monitoring', - 'type_data->data_retention' => 30, + 'type_data->data_retention' => 365, ]); } } diff --git a/tests/Feature/NotificationChannelsTest.php b/tests/Feature/NotificationChannelsTest.php index bc0e739..6e092db 100644 --- a/tests/Feature/NotificationChannelsTest.php +++ b/tests/Feature/NotificationChannelsTest.php @@ -3,9 +3,12 @@ namespace Tests\Feature; use App\Enums\NotificationChannel; +use App\Web\Pages\Settings\NotificationChannels\Index; +use App\Web\Pages\Settings\NotificationChannels\Widgets\NotificationChannelsList; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Support\Facades\Http; +use Livewire\Livewire; use Tests\TestCase; class NotificationChannelsTest extends TestCase @@ -17,12 +20,14 @@ public function test_add_email_channel(): void { $this->actingAs($this->user); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::EMAIL, - 'email' => 'email@example.com', - 'label' => 'Email', - 'global' => 1, - ])->assertSessionDoesntHaveErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::EMAIL, + 'email' => 'email@example.com', + 'label' => 'Email', + 'global' => true, + ]) + ->assertSuccessful(); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -42,11 +47,14 @@ public function test_cannot_add_email_channel(): void $this->actingAs($this->user); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::EMAIL, - 'email' => 'email@example.com', - 'label' => 'Email', - ])->assertSessionHasErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::EMAIL, + 'email' => 'email@example.com', + 'label' => 'Email', + 'global' => true, + ]) + ->assertNotified('Could not connect! Make sure you configured `.env` file correctly.'); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -63,11 +71,13 @@ public function test_add_slack_channel(): void Http::fake(); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::SLACK, - 'webhook_url' => 'https://hooks.slack.com/services/123/token', - 'label' => 'Slack', - ])->assertSessionDoesntHaveErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::SLACK, + 'webhook_url' => 'https://hooks.slack.com/services/123/token', + 'label' => 'Slack', + ]) + ->assertSuccessful(); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -86,11 +96,13 @@ public function test_cannot_add_slack_channel(): void 'slack.com/*' => Http::response(['ok' => false], 401), ]); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::SLACK, - 'webhook_url' => 'https://hooks.slack.com/services/123/token', - 'label' => 'Slack', - ])->assertSessionHasErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::SLACK, + 'webhook_url' => 'https://hooks.slack.com/services/123/token', + 'label' => 'Slack', + ]) + ->assertNotified('Could not connect'); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -106,11 +118,13 @@ public function test_add_discord_channel(): void Http::fake(); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::DISCORD, - 'webhook_url' => 'https://discord.com/api/webhooks/123/token', - 'label' => 'Discord', - ])->assertSessionDoesntHaveErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::DISCORD, + 'webhook_url' => 'https://discord.com/api/webhooks/123/token', + 'label' => 'Discord', + ]) + ->assertSuccessful(); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -129,11 +143,13 @@ public function test_cannot_add_discord_channel(): void 'discord.com/*' => Http::response(['ok' => false], 401), ]); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::DISCORD, - 'webhook_url' => 'https://discord.com/api/webhooks/123/token', - 'label' => 'Discord', - ])->assertSessionHasErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::DISCORD, + 'webhook_url' => 'https://discord.com/api/webhooks/123/token', + 'label' => 'Discord', + ]) + ->assertNotified('Could not connect'); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -149,12 +165,14 @@ public function test_add_telegram_channel(): void Http::fake(); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::TELEGRAM, - 'bot_token' => 'token', - 'chat_id' => '123', - 'label' => 'Telegram', - ])->assertSessionDoesntHaveErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::TELEGRAM, + 'bot_token' => 'token', + 'chat_id' => '123', + 'label' => 'Telegram', + ]) + ->assertSuccessful(); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -174,12 +192,14 @@ public function test_cannot_add_telegram_channel(): void 'api.telegram.org/*' => Http::response(['ok' => false], 401), ]); - $this->post(route('settings.notification-channels.add'), [ - 'provider' => NotificationChannel::TELEGRAM, - 'bot_token' => 'token', - 'chat_id' => '123', - 'label' => 'Telegram', - ])->assertSessionHasErrors(); + Livewire::test(Index::class) + ->callAction('add', [ + 'provider' => NotificationChannel::TELEGRAM, + 'bot_token' => 'token', + 'chat_id' => '123', + 'label' => 'Telegram', + ]) + ->assertNotified('Could not connect'); /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::query() @@ -193,11 +213,12 @@ public function test_see_channels_list(): void { $this->actingAs($this->user); + /** @var \App\Models\NotificationChannel $channel */ $channel = \App\Models\NotificationChannel::factory()->create(); - $this->get(route('settings.notification-channels')) + $this->get(Index::getUrl()) ->assertSuccessful() - ->assertSee($channel->provider); + ->assertSee($channel->label); } public function test_delete_channel(): void @@ -206,8 +227,9 @@ public function test_delete_channel(): void $channel = \App\Models\NotificationChannel::factory()->create(); - $this->delete(route('settings.notification-channels.delete', $channel->id)) - ->assertSessionDoesntHaveErrors(); + Livewire::test(NotificationChannelsList::class) + ->callTableAction('delete', $channel->id) + ->assertSuccessful(); $this->assertDatabaseMissing('notification_channels', [ 'id' => $channel->id, diff --git a/tests/Feature/PHPTest.php b/tests/Feature/PHPTest.php index 78238cd..81a4796 100644 --- a/tests/Feature/PHPTest.php +++ b/tests/Feature/PHPTest.php @@ -6,7 +6,10 @@ use App\Enums\ServiceStatus; use App\Facades\SSH; use App\Models\Service; +use App\Web\Pages\Servers\PHP\Index; +use App\Web\Pages\Servers\PHP\Widgets\PHPList; use Illuminate\Foundation\Testing\RefreshDatabase; +use Livewire\Livewire; use Tests\TestCase; class PHPTest extends TestCase @@ -19,10 +22,11 @@ public function test_install_new_php(): void $this->actingAs($this->user); - $this->post(route('servers.php.install', [ - 'server' => $this->server, - 'version' => '8.1', - ]))->assertSessionDoesntHaveErrors(); + Livewire::test(Index::class, ['server' => $this->server]) + ->callAction('install', [ + 'version' => '8.1', + ]) + ->assertSuccessful(); $this->assertDatabaseHas('services', [ 'server_id' => $this->server->id, @@ -52,26 +56,17 @@ public function test_uninstall_php(): void ]); $php->save(); - $this->delete(route('servers.php.uninstall', [ + Livewire::test(PHPList::class, [ 'server' => $this->server, - 'version' => '8.1', - ]))->assertSessionDoesntHaveErrors(); + ]) + ->callTableAction('uninstall', $php->id) + ->assertSuccessful(); $this->assertDatabaseMissing('services', [ 'id' => $php->id, ]); } - public function test_cannot_uninstall_php(): void - { - $this->actingAs($this->user); - - $this->delete(route('servers.php.uninstall', [ - 'server' => $this->server, - 'version' => '8.2', - ]))->assertSessionHasErrors(); - } - public function test_change_default_php_cli(): void { SSH::fake(); @@ -87,12 +82,14 @@ public function test_change_default_php_cli(): void 'name' => 'php', 'version' => '8.1', 'status' => ServiceStatus::READY, + 'is_default' => false, ]); - $this->post(route('servers.php.default-cli', [ + Livewire::test(PHPList::class, [ 'server' => $this->server, - 'version' => '8.1', - ]))->assertSessionDoesntHaveErrors(); + ]) + ->callTableAction('default-php-cli', $php->id) + ->assertSuccessful(); $php->refresh(); @@ -105,38 +102,19 @@ public function test_install_extension(): void $this->actingAs($this->user); - $this->post(route('servers.php.install-extension', [ + Livewire::test(PHPList::class, [ 'server' => $this->server, - 'version' => '8.2', - 'extension' => 'gmp', - ]))->assertSessionDoesntHaveErrors(); + ]) + ->callTableAction('install-extension', $this->server->php()->id, [ + 'extension' => 'gmp', + ]) + ->assertSuccessful(); $php = $this->server->php('8.2'); $this->assertContains('gmp', $php->type_data['extensions']); } - public function test_extension_already_installed(): void - { - SSH::fake(); - - $this->actingAs($this->user); - - $this->server->php('8.2')->update([ - 'type_data' => [ - 'extensions' => [ - 'gmp', - ], - ], - ]); - - $this->post(route('servers.php.install-extension', [ - 'server' => $this->server, - 'version' => '8.2', - 'extension' => 'gmp', - ]))->assertSessionHasErrors(); - } - /** * @dataProvider php_ini_data */ @@ -146,31 +124,13 @@ public function test_get_php_ini(string $version, string $type): void $this->actingAs($this->user); - $this->get(route('servers.php.get-ini', [ + Livewire::test(PHPList::class, [ 'server' => $this->server, - 'version' => $version, - 'type' => $type, - ]))->assertSessionHas('ini'); - } - - /** - * @dataProvider php_ini_data - */ - public function test_update_php_ini(string $version, string $type): void - { - SSH::fake(); - - $this->actingAs($this->user); - - $this->post(route('servers.php.update-ini', [ - 'server' => $this->server, - 'version' => $version, - 'type' => $type, - 'ini' => 'new ini', - ])) - ->assertSessionDoesntHaveErrors() - ->assertSessionHas('toast.type', 'success') - ->assertSessionHas('toast.message', __('PHP ini (:type) updated!', ['type' => $type])); + ]) + ->callTableAction('php-ini-'.$type, $this->server->php()->id, [ + 'ini' => 'new-ini', + ]) + ->assertSuccessful(); } public static function php_ini_data(): array diff --git a/tests/Feature/ProfileTest.php b/tests/Feature/ProfileTest.php index d0309d2..8234212 100644 --- a/tests/Feature/ProfileTest.php +++ b/tests/Feature/ProfileTest.php @@ -2,8 +2,12 @@ namespace Tests\Feature; +use App\Web\Pages\Settings\Profile\Index; +use App\Web\Pages\Settings\Profile\Widgets\ProfileInformation; +use App\Web\Pages\Settings\Profile\Widgets\UpdatePassword; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Hash; +use Livewire\Livewire; use Tests\TestCase; class ProfileTest extends TestCase @@ -15,7 +19,7 @@ public function test_profile_page_is_displayed(): void $this->actingAs($this->user); $this - ->get(route('profile')) + ->get(Index::getUrl()) ->assertSuccessful() ->assertSee('Profile Information') ->assertSee('Update Password') @@ -26,11 +30,13 @@ public function test_profile_information_can_be_updated(): void { $this->actingAs($this->user); - $this->post(route('profile.info'), [ - 'name' => 'Test', - 'email' => 'test@example.com', - 'timezone' => 'Europe/Berlin', - ]); + Livewire::test(ProfileInformation::class) + ->fill([ + 'name' => 'Test', + 'email' => 'test@example.com', + 'timezone' => 'Europe/Berlin', + ]) + ->call('submit'); $this->user->refresh(); @@ -43,11 +49,13 @@ public function test_password_can_be_updated(): void { $this->actingAs($this->user); - $this->post(route('profile.password'), [ - 'current_password' => 'password', - 'password' => 'new-password', - 'password_confirmation' => 'new-password', - ]); + Livewire::test(UpdatePassword::class) + ->fill([ + 'current_password' => 'password', + 'password' => 'new-password', + 'password_confirmation' => 'new-password', + ]) + ->call('submit'); $this->assertTrue(Hash::check('new-password', $this->user->refresh()->password)); } @@ -56,10 +64,13 @@ public function test_correct_password_must_be_provided_to_update_password(): voi { $this->actingAs($this->user); - $this->post(route('profile.password'), [ - 'current_password' => 'wrong-password', - 'password' => 'new-password', - 'password_confirmation' => 'new-password', - ])->assertSessionHasErrors('current_password'); + Livewire::test(UpdatePassword::class) + ->fill([ + 'current_password' => 'wrong-password', + 'password' => 'new-password', + 'password_confirmation' => 'new-password', + ]) + ->call('submit') + ->assertHasErrors('current_password'); } } diff --git a/tests/Feature/ProjectsTest.php b/tests/Feature/ProjectsTest.php index aa37568..176abb6 100644 --- a/tests/Feature/ProjectsTest.php +++ b/tests/Feature/ProjectsTest.php @@ -3,7 +3,11 @@ namespace Tests\Feature; use App\Models\Project; +use App\Web\Pages\Settings\Projects\Index; +use App\Web\Pages\Settings\Projects\Settings; +use App\Web\Pages\Settings\Projects\Widgets\UpdateProject; use Illuminate\Foundation\Testing\RefreshDatabase; +use Livewire\Livewire; use Tests\TestCase; class ProjectsTest extends TestCase @@ -14,9 +18,11 @@ public function test_create_project(): void { $this->actingAs($this->user); - $this->post(route('settings.projects.create'), [ - 'name' => 'test', - ])->assertSessionDoesntHaveErrors(); + Livewire::test(Index::class) + ->callAction('create', [ + 'name' => 'test', + ]) + ->assertSuccessful(); $this->assertDatabaseHas('projects', [ 'name' => 'test', @@ -31,7 +37,7 @@ public function test_see_projects_list(): void $this->user->projects()->attach($project); - $this->get(route('settings.projects')) + $this->get(Index::getUrl()) ->assertSuccessful() ->assertSee($project->name); } @@ -44,8 +50,11 @@ public function test_delete_project(): void $this->user->projects()->attach($project); - $this->delete(route('settings.projects.delete', $project)) - ->assertSessionDoesntHaveErrors(); + Livewire::test(Settings::class, [ + 'project' => $project, + ]) + ->callAction('delete') + ->assertSuccessful(); $this->assertDatabaseMissing('projects', [ 'id' => $project->id, @@ -60,9 +69,14 @@ public function test_edit_project(): void $this->user->projects()->attach($project); - $this->post(route('settings.projects.update', $project), [ - 'name' => 'new-name', - ])->assertSessionDoesntHaveErrors(); + Livewire::test(UpdateProject::class, [ + 'project' => $project, + ]) + ->fill([ + 'name' => 'new-name', + ]) + ->call('submit') + ->assertSuccessful(); $this->assertDatabaseHas('projects', [ 'id' => $project->id, @@ -74,11 +88,14 @@ public function test_cannot_delete_last_project(): void { $this->actingAs($this->user); - $this->delete(route('settings.projects.delete', [ + Livewire::test(Settings::class, [ 'project' => $this->user->currentProject, - ])) - ->assertSessionDoesntHaveErrors() - ->assertSessionHas('toast.type', 'error') - ->assertSessionHas('toast.message', 'Cannot delete the last project.'); + ]) + ->callAction('delete') + ->assertNotified('Cannot delete the last project.'); + + $this->assertDatabaseHas('projects', [ + 'id' => $this->user->currentProject->id, + ]); } }