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:
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...
Jump to solution
69 Replies
Dennis Koch
Dennis Koch7mo ago
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 enums
Marwam Malta
Marwam MaltaOP7mo ago
Thank 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), ]); }
LeandroFerreira
LeandroFerreira7mo ago
Select::make('nature')->options(NatureEnum::class) Is it cast correctly?
Marwam Malta
Marwam MaltaOP7mo ago
Yes. The select field receives all the enum options.
LeandroFerreira
LeandroFerreira7mo ago
dd($newEnteData); what is the output?
Marwam Malta
Marwam MaltaOP7mo ago
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.
LeandroFerreira
LeandroFerreira7mo ago
->call('create')
->assertHasNoFormErrors();
->call('create')
->assertHasNoFormErrors();
?
Marwam Malta
Marwam MaltaOP7mo ago
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.
LeandroFerreira
LeandroFerreira7mo ago
is nature fillable in the model?
Marwam Malta
Marwam MaltaOP7mo ago
Yes
LeandroFerreira
LeandroFerreira7mo ago
are you able to create a new ente in the browser? Does it work?
Marwam Malta
Marwam MaltaOP7mo ago
Yes. I can create
Dennis Koch
Dennis Koch7mo ago
Try going through the test with a debugger and check what's the actual value. Or check inside beforeCreate method to see the data
LeandroFerreira
LeandroFerreira7mo ago
dd($newEnteData->toArray()); what is the output?
Marwam Malta
Marwam MaltaOP7mo ago
LeandroFerreira
LeandroFerreira7mo ago
weird.. did you try it using a simple array?
Livewire(CreateEnte::class)
->fillForm([
'nature' => 'M'
...
])
Livewire(CreateEnte::class)
->fillForm([
'nature' => 'M'
...
])
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?
awcodes
awcodes7mo ago
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.
LeandroFerreira
LeandroFerreira7mo ago
it makes sense because if you are using the Livewire class, the test would be different Livewire::test()
awcodes
awcodes7mo ago
just seems to me like it should be livewire(CreateEnte::class) instead of Livewire(CreateEnte::class)
LeandroFerreira
LeandroFerreira7mo ago
yes, if you are using the function..
Marwam Malta
Marwam MaltaOP7mo ago
@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?
LeandroFerreira
LeandroFerreira7mo ago
Adam is right, try livewire() instead of Livewire Use this function use function Pest\Livewire\livewire; https://pestphp.com/docs/plugins#content-livewire
Plugins | 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.
Dennis Koch
Dennis Koch7mo ago
It doesn't matter
Marwam Malta
Marwam MaltaOP7mo ago
@awcodes I modified Livewire, but the same error persisted I tested both ways, but it didn't make any difference regarding the error.
awcodes
awcodes7mo ago
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.
Marwam Malta
Marwam MaltaOP7mo ago
@awcodes This error in 'nature' is the reason I opened the post 'nature' comes from the resource form
awcodes
awcodes7mo ago
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
Dennis Koch
Dennis Koch7mo ago
Might ->reactive() be an issue?
Marwam Malta
Marwam MaltaOP7mo ago
@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
Dennis Koch
Dennis Koch7mo ago
Can you check what the actual validation error is?
LeandroFerreira
LeandroFerreira7mo ago
livewire(CreateEnte::class)
->fillForm([
'nature' => 'M'
])
->assertFormSet([
'nature' => 'M'
]);
livewire(CreateEnte::class)
->fillForm([
'nature' => 'M'
])
->assertFormSet([
'nature' => 'M'
]);
what is the output?
Marwam Malta
Marwam MaltaOP7mo ago
@Leandro Ferreira
LeandroFerreira
LeandroFerreira7mo ago
livewire(CreateEnte::class)
->set('data.nature', 'M')
->assertFormSet([
'nature' => 'M'
]);
livewire(CreateEnte::class)
->set('data.nature', 'M')
->assertFormSet([
'nature' => 'M'
]);
what is the output?
Marwam Malta
Marwam MaltaOP7mo ago
@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
LeandroFerreira
LeandroFerreira7mo ago
try ->set('data.nature', 'M')
Marwam Malta
Marwam MaltaOP7mo ago
@Leandro Ferreira That way he passed the test
LeandroFerreira
LeandroFerreira7mo ago
fillForm isn't working... Filament version? php artisan about --only=Filament
Marwam Malta
Marwam MaltaOP7mo ago
LeandroFerreira
LeandroFerreira7mo ago
run the command
Marwam Malta
Marwam MaltaOP7mo ago
LeandroFerreira
LeandroFerreira7mo ago
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..
Marwam Malta
Marwam MaltaOP7mo ago
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
LeandroFerreira
LeandroFerreira7mo ago
are you using this in the phpunit.xml? <env name="APP_ENV" value="testing"/>
Marwam Malta
Marwam MaltaOP7mo ago
Yes. In my case it is 'local'
LeandroFerreira
LeandroFerreira7mo ago
are you using sqlite for tests?
Marwam Malta
Marwam MaltaOP7mo ago
I already tested it as testing and it gives the same error I don't use sqlite I just changed it to testing
LeandroFerreira
LeandroFerreira7mo ago
try it using this
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
Marwam Malta
Marwam MaltaOP7mo ago
LeandroFerreira
LeandroFerreira7mo ago
use RefreshDatabase::class in the tests/Pest.php file
uses(
Tests\TestCase::class,
Illuminate\Foundation\Testing\RefreshDatabase::class,
)->in('Feature')
uses(
Tests\TestCase::class,
Illuminate\Foundation\Testing\RefreshDatabase::class,
)->in('Feature')
Marwam Malta
Marwam MaltaOP7mo ago
LeandroFerreira
LeandroFerreira7mo ago
add this in the test dd(app()->runningUnitTests()); what is the output?
Marwam Malta
Marwam MaltaOP7mo ago
LeandroFerreira
LeandroFerreira7mo ago
use ->fillForm($newEnteData)
Marwam Malta
Marwam MaltaOP7mo ago
now the error has changed
LeandroFerreira
LeandroFerreira7mo ago
ohh sorry, $newEnteData->toArray()
Dennis Koch
Dennis Koch7mo ago
I admire Leandro's endurance. But I think at this point it would help to have a minimal reproduction repository to help
LeandroFerreira
LeandroFerreira7mo ago
almost done... 😅 I think this will work
Marwam Malta
Marwam MaltaOP7mo ago
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
LeandroFerreira
LeandroFerreira7mo ago
let me try to explain what was the issue:
Dennis Koch
Dennis Koch7mo ago
I'm curios, too 😅
Marwam Malta
Marwam MaltaOP7mo ago
😂
Dennis Koch
Dennis Koch7mo ago
Let me grab some popcorn
Marwam Malta
Marwam MaltaOP7mo ago
I returned with the enums and it worked
Solution
LeandroFerreira
LeandroFerreira7mo ago
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
LeandroFerreira
LeandroFerreira7mo ago
@Dennis Koch @Marwam Malta That is it! 😅
Dennis Koch
Dennis Koch7mo ago
🙈 Well spotted!
Dennis Koch
Dennis Koch7mo ago
Lesson today: Never change your test environment to something not testing 😅
LeandroFerreira
LeandroFerreira7mo ago
I agree 😂
Marwam Malta
Marwam MaltaOP7mo ago
🎉 🎉 🎉 🎉 🎉 Congratulations. Thank you all very much for your help. Ufaaaaaaaaaaaaaaaa Thank you @Leandro Ferreira , @Dennis Koch , @awcodes
Want results from more Discord servers?
Add your server