Error in Filament with PestPHP using form select in resource and enum
I'm creating a project using Filament, Laravel 11 and PestPHP. I'm having problems running the test. the following error appears: " FAILED Tests\Feature\EntesTest >
CREATE - admin/entes/create
→ it must test the creation of an entity
Component has errors: "data.nature"
Failed asserting that false is true.".
I don't understand the reason for the error, as I am sending the 'nature' field in the fillForm. How to resolve this error?
=======>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Follow the code in the attached file.Solution:Jump to solution
The
fillForm
function is a helper that calls another method, fillFormDataForTesting
(after V3.2.77). This method checks the runningUnitTests()
status. According to your test configuration, it was returning false
.
However, when you update the connection information in phpunit.xml
as previously suggested, runningUnitTests()
returns true...69 Replies
It's easier for people to help if you post code here instead of upload a file without an extension. Especially if they are on mobile.
What's the value of
$newEnteData->toArray()
?
Btw. you can use. Select::make()->enum()
method for enumsThank you for your help.
Hmmmmmm. Ah yes. Sorry for the file, but when posting, discord limited the characters.
This one is $newEnteData = Ente::factory()->make([
'nature' => NatureEnum::M->value, // Or any other valid nature value
'uf' => StateEnum::MA,
'id_ente_federated' => 454,
'complementary_pension_date' => Carbon::createFromFormat('Y-m-d H:i:s', '2024-05-21 00:00:00')->toDateTimeString(),
'status' => true,
'obs' => 'asdhasg asdlflsad fgasd lkflaksd klf',
]);
And look, I didn't know about this option of working with Enum. I will use it.
I am sending the 'nature' key in the body of the array. But when I run the test, it insists on returning this error. I don't understand why this is.
aqui o teste completo
it('deve testar a criação de um ente', function () {
// Ensure nature is set with a valid value
$newEnteData = Ente::factory()->make([
'nature' => NatureEnum::M->value, // Or any other valid nature value
'uf' => StateEnum::MA,
'id_ente_federated' => 454,
'complementary_pension_date' => Carbon::createFromFormat('Y-m-d H:i:s', '2024-05-21 00:00:00')->toDateTimeString(),
'status' => true,
'obs' => 'asdhasg asdlflsad fgasd lkflaksd klf',
]);
// Inspect data before filling the form (optional)
// dd($newEnteData);
Livewire(CreateEnte::class) ->fillForm($newEnteData->toArray()) // Use toArray() for all data ->call('create') ->assertHasNoErrors(); }); Here the resource public static function form(Form $form): Form { return $form ->schema([ Forms\Components\Select::make('nature') ->label('Natureza') ->reactive() ->options( collect(NatureEnum::cases())->mapWithKeys(fn($case) => [$case->value => $case->getLabel()])->toArray() ) ->required(), Forms\Components\Select::make('uf') ->reactive() ->label('UF') ->options(StateEnum::class) ->hidden(fn ($get) => $get('nature') !== NatureEnum::M->value) ->afterStateUpdated(function (callable $set) { $set('id_ente_federated', null); }), Forms\Components\Select::make('id_ente_federated') ->label('Ente Federado') ->reactive() ->hidden(fn ($get) => $get('uf') === null || $get('uf') === '') ->options(function (callable $get) { $uf = $get('uf'); if ($uf) { return City::where('sigla', $uf)->pluck('city', 'id'); } return []; }), Forms\Components\Checkbox::make('status') ->reactive() ->label('Previdência Complementar Implementada'), Forms\Components\DatePicker::make('complementary_pension_date') ->label('Data') ->hidden(fn ($get) => ! $get('status')), Forms\Components\Textarea::make('obs') ->rows(5) ->cols(15), ]); }
Livewire(CreateEnte::class) ->fillForm($newEnteData->toArray()) // Use toArray() for all data ->call('create') ->assertHasNoErrors(); }); Here the resource public static function form(Form $form): Form { return $form ->schema([ Forms\Components\Select::make('nature') ->label('Natureza') ->reactive() ->options( collect(NatureEnum::cases())->mapWithKeys(fn($case) => [$case->value => $case->getLabel()])->toArray() ) ->required(), Forms\Components\Select::make('uf') ->reactive() ->label('UF') ->options(StateEnum::class) ->hidden(fn ($get) => $get('nature') !== NatureEnum::M->value) ->afterStateUpdated(function (callable $set) { $set('id_ente_federated', null); }), Forms\Components\Select::make('id_ente_federated') ->label('Ente Federado') ->reactive() ->hidden(fn ($get) => $get('uf') === null || $get('uf') === '') ->options(function (callable $get) { $uf = $get('uf'); if ($uf) { return City::where('sigla', $uf)->pluck('city', 'id'); } return []; }), Forms\Components\Checkbox::make('status') ->reactive() ->label('Previdência Complementar Implementada'), Forms\Components\DatePicker::make('complementary_pension_date') ->label('Data') ->hidden(fn ($get) => ! $get('status')), Forms\Components\Textarea::make('obs') ->rows(5) ->cols(15), ]); }
Select::make('nature')->options(NatureEnum::class)
Is it cast correctly?Yes. The select field receives all the enum options.
dd($newEnteData);
what is the output?
I have the following rules. When selecting Municipality ('M'), you must enable another select. In this case, 'UF' and only when selecting a UF option should you enable the other select which is 'id_ente_federated'.
I made these rules there in the resource. I don't know if this could have an impact in any way.
?
The intention of the test is to simulate the creation of a new 'entity'. Save this data and check if it has been saved in the bank. But I did this '->assertHasNoFormErrors();'. I'm following the filament documentation. I still don't know for sure if I'm doing it correctly. There is a rule in select that is 'required()'. This could be the error. When I remove required, it returns an insertion error in the database. The 'nature' field is mandatory in the database, hence the error.
is nature fillable in the model?
Yes
are you able to create a new ente in the browser? Does it work?
Yes. I can create
Try going through the test with a debugger and check what's the actual value.
Or check inside
beforeCreate
method to see the datadd($newEnteData->toArray());
what is the output?weird..
did you try it using a simple array?
use assertFormSet before
create
to check that a form has data..
are you using hooks in the create page?
Did you add something in the provider, appserviceprovider?Shouldn't
Livewire
be the pest helper function use function Pest\Livewire\livewire;
?
maybe it doesn't matter, just the only thing standing out to me as different.
unless ->value
on the enum in the make is doing something weird with the cast.it makes sense because if you are using the Livewire class, the test would be different
Livewire::test()
just seems to me like it should be
livewire(CreateEnte::class)
instead of Livewire(CreateEnte::class)
yes, if you are using the function..
@Leandro Ferreira
are you using hooks on the create page? No. Did you add something to the provider, appserviceprovider? No. would it be something like that?
are you using hooks on the create page? No. Did you add something to the provider, appserviceprovider? No. would it be something like that?
Adam is right, try
livewire()
instead of Livewire
Use this function use function Pest\Livewire\livewire;
https://pestphp.com/docs/plugins#content-livewirePlugins | Pest - The elegant PHP Testing Framework
In this section, we will discuss the official and community developed plugins that we endorse. Plugins primarily offer namespaced functions, console commands, custom expectations, and additional command-line options to augment the default Pest experience.
It doesn't matter
@awcodes
I modified Livewire, but the same error persisted
I tested both ways, but it didn't make any difference regarding the error.
well, it did, you are getting a different error now.
the new error is saying that you're form field for 'nature' has an error.
@awcodes
This error in 'nature' is the reason I opened the post
'nature' comes from the resource form
yea, for some reason when you fill the form the select isn't picking up 'M' as the selected option, so when it calls 'save' it's triggering the 'required' validation
Might
->reactive()
be an issue?@Dennis Koch
I have the following rules. When selecting Municipality ('M'), you must enable another select. In this case, 'UF' and only when selecting a UF option should you enable the other select which is 'id_ente_federated'.
I made these rules there in the resource. I don't know if this could have an impact in any way.
@awcodes
It works on the web. I can register normally
Can you check what the actual validation error is?
what is the output?
@Leandro Ferreira
what is the output?
@Dennis Koch
I didn't understand well. The validation error is 'data.nature' which occurs due to a ->required() in the Select in the resource
try
->set('data.nature', 'M')
@Leandro Ferreira
That way he passed the test
fillForm isn't working... Filament version?
php artisan about --only=Filament
run the command
run
composer update -W
and try again with ->fillForm(['nature' => 'M'])
let me know if it works
I'll also try fillForm in a local project and I let you know..I ran composer. Updated but the error continues
"I'll also try fillForm in a local project and I let you know.."
Ok. Thank you very much for your help. If you know anything please let me know
are you using this in the
phpunit.xml
?
<env name="APP_ENV" value="testing"/>
Yes. In my case it is 'local'
are you using
sqlite
for tests?I already tested it as testing and it gives the same error
I don't use sqlite
I just changed it to testing
try it using this
use
RefreshDatabase::class
in the tests/Pest.php
file
add this in the test
dd(app()->runningUnitTests());
what is the output?use
->fillForm($newEnteData)
now the error has changed
ohh sorry,
$newEnteData->toArray()
I admire Leandro's endurance. But I think at this point it would help to have a minimal reproduction repository to help
almost done... 😅 I think this will work
hahahaha. All good.
Now it's gone.
However, I removed the enum this afternoon. I was trying to make it work without using the enum
let me try to explain what was the issue:
I'm curios, too 😅
😂
Let me grab some popcorn
I returned with the enums and it worked
Solution
The
fillForm
function is a helper that calls another method, fillFormDataForTesting
(after V3.2.77). This method checks the runningUnitTests()
status. According to your test configuration, it was returning false
.
However, when you update the connection information in phpunit.xml
as previously suggested, runningUnitTests()
returns true@Dennis Koch @Marwam Malta
That is it! 😅
🙈
Well spotted!
Lesson today: Never change your test environment to something not testing 😅
I agree 😂
🎉 🎉 🎉 🎉 🎉
Congratulations.
Thank you all very much for your help.
Ufaaaaaaaaaaaaaaaa
Thank you @Leandro Ferreira , @Dennis Koch , @awcodes