Skip to content

Commit

Permalink
refactor: Rector update
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 17, 2024
1 parent 655c0b0 commit bdb1e4a
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions tests/Feature/BackupTasks/Livewire/UpdateBackupTaskFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
use App\Models\User;
use Livewire\Livewire;

beforeEach(function () {
beforeEach(function (): void {
$this->data = createUserWithBackupTaskAndDependencies();
$this->actingAs($this->data['user']);
});

test('form can be rendered', function () {
test('form can be rendered', function (): void {
Livewire::test(UpdateBackupTaskForm::class, [
'backupTask' => $this->data['backupTask'],
'remoteServers' => $this->data['user']->remoteServers,
])->assertOk();
});

describe('backup task update', function () {
test('can be updated by the owner', function () {
describe('backup task update', function (): void {
test('can be updated by the owner', function (): void {
$tags = Tag::factory(2)->create(['user_id' => $this->data['user']->id]);
$tagIds = $tags->pluck('id')->toArray();

Expand Down Expand Up @@ -82,9 +82,9 @@
]);
}

foreach ($notificationStreamIds as $streamId) {
foreach ($notificationStreamIds as $notificationStreamId) {
$this->assertDatabaseHas('backup_task_notification_streams', [
'notification_stream_id' => $streamId,
'notification_stream_id' => $notificationStreamId,
'backup_task_id' => $this->data['backupTask']->id,
]);
}
Expand All @@ -94,7 +94,7 @@
->and($updatedBackupTask->notificationStreams)->toHaveCount(2);
});

test('can be updated by the owner with custom cron', function () {
test('can be updated by the owner with custom cron', function (): void {
$testable = Livewire::test(UpdateBackupTaskForm::class, [
'backupTask' => $this->data['backupTask'],
'remoteServers' => $this->data['user']->remoteServers,
Expand Down Expand Up @@ -125,7 +125,7 @@
]);
});

test('cannot be updated by another user', function () {
test('cannot be updated by another user', function (): void {
$anotherUser = User::factory()->create();

$this->actingAs($anotherUser);
Expand All @@ -150,8 +150,8 @@
});
});

describe('validation rules', function () {
test('backup task has required validation rules', function () {
describe('validation rules', function (): void {
test('backup task has required validation rules', function (): void {
$testable = Livewire::test(UpdateBackupTaskForm::class, [
'backupTask' => $this->data['backupTask'],
'remoteServers' => RemoteServer::all(),
Expand All @@ -168,7 +168,7 @@
]);
});

test('the store path needs to be a valid unix path', function () {
test('the store path needs to be a valid unix path', function (): void {
$testable = Livewire::test(UpdateBackupTaskForm::class, [
'backupTask' => $this->data['backupTask'],
'remoteServers' => RemoteServer::all(),
Expand All @@ -181,7 +181,7 @@
]);
});

test('the excluded database tables must be a valid comma separated list', function () {
test('the excluded database tables must be a valid comma separated list', function (): void {
$testable = Livewire::test(UpdateBackupTaskForm::class, [
'backupTask' => $this->data['backupTask'],
'remoteServers' => RemoteServer::all(),
Expand All @@ -197,8 +197,8 @@
});
});

describe('time and timezone handling', function () {
test('the time to run at is converted from the users timezone to UTC', function () {
describe('time and timezone handling', function (): void {
test('the time to run at is converted from the users timezone to UTC', function (): void {
$this->data['user']->update(['timezone' => 'America/New_York']);

$testable = Livewire::test(UpdateBackupTaskForm::class, [
Expand All @@ -207,7 +207,7 @@
]);

$testable->set('timeToRun', '12:00') // 12:00 PM in America/New_York
->set('description', '')
->set('description', '')
->set('sourcePath', '/var/www/html')
->call('submit')
->assertHasNoErrors();
Expand All @@ -217,7 +217,7 @@
]);
});

test('a task cannot share the same time as another task on the same server', function () {
test('a task cannot share the same time as another task on the same server', function (): void {
$this->withoutExceptionHandling();
$user = User::factory()->create();

Expand Down Expand Up @@ -252,7 +252,7 @@
->assertHasErrors('timeToRun');
});

test('a task retains its set time without validation errors', function () {
test('a task retains its set time without validation errors', function (): void {
$user = User::factory()->create();

$remoteServer = RemoteServer::factory()->create([
Expand Down Expand Up @@ -283,8 +283,8 @@
});
});

describe('tag handling', function () {
test('users cannot set tags that do not belong them', function () {
describe('tag handling', function (): void {
test('users cannot set tags that do not belong them', function (): void {
$tag = Tag::factory()->create();

$testable = Livewire::test(UpdateBackupTaskForm::class, [
Expand All @@ -299,7 +299,7 @@
]);
});

test('users cannot set tags that do not exist', function () {
test('users cannot set tags that do not exist', function (): void {
$testable = Livewire::test(UpdateBackupTaskForm::class, [
'backupTask' => $this->data['backupTask'],
'remoteServers' => RemoteServer::all(),
Expand All @@ -312,7 +312,7 @@
]);
});

test('a user can update their already existing tags', function () {
test('a user can update their already existing tags', function (): void {
$user = User::factory()->create();

$remoteServer = RemoteServer::factory()->create([
Expand Down Expand Up @@ -366,8 +366,8 @@
});
});

describe('notification stream handling', function () {
test('users cannot set notification streams that do not belong to them', function () {
describe('notification stream handling', function (): void {
test('users cannot set notification streams that do not belong to them', function (): void {
$notificationStream = NotificationStream::factory()->create();

$testable = Livewire::test(UpdateBackupTaskForm::class, [
Expand All @@ -382,7 +382,7 @@
]);
});

test('users cannot set streams that do not exist', function () {
test('users cannot set streams that do not exist', function (): void {
$testable = Livewire::test(UpdateBackupTaskForm::class, [
'backupTask' => $this->data['backupTask'],
'remoteServers' => RemoteServer::all(),
Expand All @@ -395,18 +395,18 @@
]);
});

test('a user can update their existing backup task notification streams', function () {
test('a user can update their existing backup task notification streams', function (): void {
$user = User::factory()->create();
$remoteServer = RemoteServer::factory()->create(['user_id' => $user->id]);

$streams = NotificationStream::factory()->email()->count(3)->create([
'user_id' => $user->id,
])->each(function ($stream, $index) {
])->each(function ($stream, $index): void {
$stream->update(['label' => 'Stream ' . ($index + 1)]);
});

$backupTask = BackupTask::factory()->create(['user_id' => $user->id]);
$backupTask->notificationStreams()->attach([$streams[0]->id, $streams[1]->id]);
$backupTask->notificationStreams()->attach([$streams[0]->getAttribute('id'), $streams[1]->getAttribute('id')]);

$this->actingAs($user);

Expand All @@ -416,7 +416,7 @@
'availableStreams' => $user->notificationStreams,
])
->set('remoteServerId', $remoteServer->id)
->set('selectedStreams', [$streams[2]->id])
->set('selectedStreams', [$streams[2]->getAttribute('id')])
->set('sourcePath', '/var/www/html')
->set('description', 'Updated description')
->call('submit')
Expand All @@ -431,19 +431,17 @@

$this->assertDatabaseHas('backup_task_notification_streams', [
'backup_task_id' => $backupTask->id,
'notification_stream_id' => $streams[2]->id,
'notification_stream_id' => $streams[2]->getAttribute('id'),
]);

foreach ([$streams[0]->id, $streams[1]->id] as $detachedStreamId) {
foreach ([$streams[0]->getAttribute('id'), $streams[1]->getAttribute('id')] as $detachedStreamId) {
$this->assertDatabaseMissing('backup_task_notification_streams', [
'backup_task_id' => $backupTask->id,
'notification_stream_id' => $detachedStreamId,
]);
}

expect($backupTask->fresh()->notificationStreams)->toHaveCount(1)
->and($backupTask->fresh()->notificationStreams->first()->id)->toBe($streams[2]->id);
->and($backupTask->fresh()->notificationStreams->first()->id)->toBe($streams[2]->getAttribute('id'));
});
});


0 comments on commit bdb1e4a

Please sign in to comment.