Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mr_reboot's avatar

Very odd testing error

I am posting this under filament since that's where I'm testing but I think this might be an issue with laravel. Has anyone seen this before?

Here is my test:

it('can edit a user profile', function () {
    $userProfile = UserProfile::factory()
                              ->create([
                                  'tenant_id' => $this->tenant->id,
                              ]);

    Livewire::test(EditUserProfile::class, [
        'record' => $userProfile->id,
    ])
            ->fillForm([
                'first_name' => 'Updated',
                'last_name'  => 'Name',
                'email'      => '[email protected]',
                'phone'      => '0987654321',
            ])
            ->call('save')
            ->assertHasNoFormErrors();

    $this->assertDatabaseHas('user_profiles', [
        'id'         => $userProfile->id,
        'first_name' => 'Updated',
        'last_name'  => 'Name',
        'email'      => '[email protected]',
        'phone'      => '0987654321',
    ]);
});

and the error I get:

Failed asserting that a row in the table [user_profiles] matches the attributes {
    "id": 2,
    "first_name": "Updated",
    "last_name": "Name",
    "email": "[email protected]",
    "phone": "0987654321"
}.

Found similar results: [
    {
        "id": 2,
        "first_name": "Updated",
        "last_name": "Name",
        "\"email\"": "email", // <---- Issue is here. WTF???
        "phone": "0987654321"
    }
].

As you can see the result from the assertDatabaseHas is adding extra escape strings to the email key. This makes no sense to me. Anyone have any idea where these escaped strings are coming from?

I have same exact issue on the create profile test as well.

0 likes
2 replies
mr_reboot's avatar

@tykus Hi Tykus, this is the standard default Filament component for the edit page. I have not modified it in anyway nor have I even published the components so I'm using it straight from the package.

I am doing some "advanced" things around the email field which is why I'm sure I have the problem. I set it up so it ignores the email field when making an update since I don't want the user to be able to change the email.

I'm not really sure why it would result in double quotes.

My edit page for the resource:

protected function handleRecordUpdate(Model $record, array $data): Model
    {
        $result = DB::transaction(function () use ($data, $record) {
            // Update the user profile
            $record->update(
                collect($data)
                    ->except(['email'])
                    ->toArray()
            );

            return [
                'user_profile' => $record,
            ];
        });

        return $result['user_profile'];
    }

and here is the Form component definition for the email input:

Forms\Components\TextInput::make('email')
                ->disabledOn(['edit', 'view.edit'])
                ->afterStateHydrated(function ($component, $state, $record) {
                    // This ensures the email is loaded properly on edit forms
                    if ($record && $record->user) {
                        $component->state($record->user->email);
                    }
                })
                ->required()
                ->rules([
                    // Check if user exists in the tenant already
                    fn (): Closure => static function (
                        string $attribute,
                        $value,
                        Closure $fail,
                        Validator $validator
                    ) {
                        $new_data = $validator->validated()['data'];
                        $record = $validator->attributes()['record'];
                        if (
                            ! ($record && $record['user']['email'] === $new_data['email'])
                        ) {
                            self::check_if_user_exists_in_tenant(
                                value: $value,
                                fail : $fail
                            );
                        }
                    },
                ]),

Everything else is default filament form stuff.

Please or to participate in this conversation.