F
Filament17mo ago
*

redirect() in action, strange behaviour

I've got the following test:
it('bla', function () {
$invitationData = \App\Models\UserInvitation::factory()->make()->toArray();

data_forget($invitationData, 'uuid');

$invitationData['name'] = fake()->name;
$invitationData['email'] = fake()->email;
$invitationData['company'] = fake()->company;
$invitationData['address'] = fake()->address;
$invitationData['postcode'] = fake()->postcode;

$invitation = (new StoreUserInvitation())->execute(invitationData: $invitationData);

livewire(UserInvitationResource\Pages\ViewUserInvitation::class, [
'record' => $invitation->id,
])->callPageAction('resend');

$expectedNewInvitationData = data_get($invitation, [
'name',
'email',
'company',
'address',
'postcode',
]);

$expectedNewInvitationData['status'] = UserInvitationStatus::PENDING;

$this->assertDatabaseCount(\App\Models\UserInvitation::class, 2);
});
it('bla', function () {
$invitationData = \App\Models\UserInvitation::factory()->make()->toArray();

data_forget($invitationData, 'uuid');

$invitationData['name'] = fake()->name;
$invitationData['email'] = fake()->email;
$invitationData['company'] = fake()->company;
$invitationData['address'] = fake()->address;
$invitationData['postcode'] = fake()->postcode;

$invitation = (new StoreUserInvitation())->execute(invitationData: $invitationData);

livewire(UserInvitationResource\Pages\ViewUserInvitation::class, [
'record' => $invitation->id,
])->callPageAction('resend');

$expectedNewInvitationData = data_get($invitation, [
'name',
'email',
'company',
'address',
'postcode',
]);

$expectedNewInvitationData['status'] = UserInvitationStatus::PENDING;

$this->assertDatabaseCount(\App\Models\UserInvitation::class, 2);
});
ViewUserInvitation.php related parts are:
protected function getActions(): array
{
return [
Action::make('resend')->action('resendInvitation'),
];
}
protected function getActions(): array
{
return [
Action::make('resend')->action('resendInvitation'),
];
}
and
public function resendInvitation()
{
$newInvitationData = Arr::except($this->record->toArray(), [
'id',
'uuid',
'token',
'expires_at',
'status',
'created_at',
'updated_at',
]);

$invitation = (new StoreUserInvitation())->execute(invitationData: $newInvitationData);

return redirect()->route('filament.resources.user-invitations.view', [
'record' => $invitation,
]);
}
public function resendInvitation()
{
$newInvitationData = Arr::except($this->record->toArray(), [
'id',
'uuid',
'token',
'expires_at',
'status',
'created_at',
'updated_at',
]);

$invitation = (new StoreUserInvitation())->execute(invitationData: $newInvitationData);

return redirect()->route('filament.resources.user-invitations.view', [
'record' => $invitation,
]);
}
The first assertion in the test passes. The second fails with count being 3 !== 2 But, when I remove the return redirect().... , the test passes as expected.
3 Replies
*
*OP17mo ago
Also tested manually. Works as expected with or without the redirect.
LeandroFerreira
LeandroFerreira17mo ago
not sure, but you can try to move the redirect to the after method
->action('resendInvitation')
->after('redirectInvitation')
->action('resendInvitation')
->after('redirectInvitation')
*
*OP17mo ago
return [
Action::make('resend')->action(function () {
$invitation = $this->resendInvitation();
$this->invitation = $invitation;
})->after(function () {
return redirect()->route('filament.resources.user-invitations.view', [
'record' => $this->invitation,
]);
}),
];
return [
Action::make('resend')->action(function () {
$invitation = $this->resendInvitation();
$this->invitation = $invitation;
})->after(function () {
return redirect()->route('filament.resources.user-invitations.view', [
'record' => $this->invitation,
]);
}),
];
Tried it this way, but no luck, still works as expected, but the test fails. If I redirect to the index page rather than view the test passes. So strange.. Voila! If I do the redirect with:
return $this->getResource()::getUrl('view', ['record' => $this->invitation]);
return $this->getResource()::getUrl('view', ['record' => $this->invitation]);
instead of:
return redirect()->route('filament.resources.user-invitations.view', [
'record' => $this->invitation,
]);
return redirect()->route('filament.resources.user-invitations.view', [
'record' => $this->invitation,
]);
It works. Don't know why particularly, but it's resolves my issue.
Want results from more Discord servers?
Add your server