bbkbbk's avatar

Livewire test: Array () does not match expected type "string".

I trying to build tests on a Livewire project that uses FilamentPHP. I'm getting the above error in my test and I have no idea why it's getting an array. I'm 100% sure this happens on the "thumbnail" and "featured_image" fields.

This is what my test looks like:

    /** @test */
    public function an_admin_can_see_saved_fields_for_edit_screen_for_a_post_from_filament(): void
    {        
        $post = Post::factory()->create([
            'is_active' => true
        ]);

        Livewire::test(PostResource\Pages\EditPost::class, [
            'record' => $post->getRouteKey(),
        ])
        ->assertFormSet([
            'title' => $post->title,
            'slug' => $post->slug,
            'thumbnail' => $post->thumbnail,
            'featured_image' => $post->featured_image,
            'excerpt' => $post->excerpt,
            'content' => $post->content,
            'is_active' => $post->is_active,
            'category_id' => $post->category_id,
        ]);
    }

This is what the relevant part of my migration file looks like:

$table->string('thumbnail');
$table->string('featured_image');

Here's what my factory looks like:

    public function definition(): array
    {
        $cat = Category::factory()->create();

        return [
            'title' => $this->faker->userName,
            'slug' => $this->faker->slug,
            'thumbnail' => $this->faker->image(storage_path('app/public/blog')),
            'featured_image' => $this->faker->image(storage_path('app/public/blog')),
            'excerpt' => $this->faker->sentence,
            'content' => $this->faker->sentence,
            'category_id' => $cat->id,
        ];
    }

If I run the factory in tinker the thumbnail and featured_image are both saved as paths in a string.

If I Log::info(gettype($post->thumbnail)); in the test before the assertFormTest it logs "string".

I've also tried setting the $casts property in the Post model to be string for thumbnail and featured_image but it doesn't make a difference.

I'm not sure if it makes a difference but the real databases is MySQL but for my tests I'm using (in phpunit.xml):

        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>

Please can anyone advise why I get an " Array () does not match expected type "string"." error when I run my test?

0 likes
0 replies

Please or to participate in this conversation.