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

welcomattic's avatar

Test a select2 field with PHPUnit

I'm trying to test a form with a select2 field with this code :

$this->actingAs($this->loggedUser)
            ->visit('admin/conference/create')
            ->submitForm('Add', [
                'name' => 'Forum PHP 2016',
                'location' => 'Paris',
                'start_at' => '2016-10-27 09:00:00',
                'end_at' => '2016-10-28 18:00:00',
                'cfp_link' => 'http://event.afup.org/forum-php-2016/appel-a-conferenciers/',
                'cfp_ends_at' => '2016-07-31 23:59:59',
                'tags' => ['PHP'],
            ])
            ->see('Forum PHP 2016');

But the output error is :

1) ConferencesTest::logged_user_can_post_a_new_conference
InvalidArgumentException: Input "tags" cannot take "PHP" as a value (possible values: ).
0 likes
6 replies
welcomattic's avatar

It's a select tag. Note that I use select2 js lib on this tag, I can't define if it cause a problem.

tomopongrac's avatar

Does that filed is multiselect or single select

If it single select than you try:

'tags' => 'PHP',
welcomattic's avatar

It's a multiple select. I have a tags table with n-n relation to a conferences table.

tomopongrac's avatar

In you controller try this with selected value 'PHP'

dd($request->input('tags'));
welcomattic's avatar
welcomattic
OP
Best Answer
Level 1

Well, I solved my problem.

I had not any tag records in my test database, so, thanks to captain obvious!

For future readers here : don't forget to seed your test database ;)

1 like

Please or to participate in this conversation.