Lestah's avatar
Level 11

Array to string conversion Feature Test


/**
     * Add. test pass
     *
     * @test
     * @group  feature
     * @group  feature:setting
     * @group  settings.index
     * @return void
     */
    public function a_user_can_save_preferred_date_format()
    {
        // Arrangements
        $this->withoutExceptionHandling();
        $this->actingAs($user = $this->asNonSuperAdmin(['settings.store', 'settings.preferences']));
        $this->withPermissionsPolicy();

        // Actions
        $attributes = [ 'format:date' => [
            'key' => 'format:date',
            'value' => settings('format:date')
        ]];

        $response = $this->post(
            route('settings.store'), $attributes,
            ['HTTP_REFERER' => route('settings.preferences')]
        );

        // Assertions
        $response->assertRedirect(route('settings.preferences'));
        $this->assertDatabaseHas($this->service->getTable(), $attributes);
    }

I'm not sure on this line


  $response = $this->post(
            route('settings.store'), $attributes,
            ['HTTP_REFERER' => route('settings.preferences')]
        );

I got an error that says ErrorException: Array to string conversion what could be wrong? can someone have an idea thanks

0 likes
5 replies
lightyagami's avatar

this slice is a array ['HTTP_REFERER' => route('settings.preferences')] he's trying convert the whole array to string, not sure what your test does

mware's avatar

Could the issue be with the $attributes variable? Does the controller that handles the post method expect an array within an array - or just a normal array?

Lestah's avatar
Level 11

Here's what i did on my controller


  /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->service()->store($request->except(['_token']));

        return back();
    }

just the normal array

mware's avatar

I'm referring to

$attributes = [ 'format:date' => [
            'key' => 'format:date',
            'value' => settings('format:date')
        ]];

Typically the $attributes array would look like this

$attributes = [
            'key' => 'format:date',
            'value' => settings('format:date')
        ];

If the store method is expecting an array that is only one layer deep, but your sending an array that is two layers deep, then you would get that error because its expecting the value to be a string, but your passing another array.

Lestah's avatar
Level 11

yes I've but my table expects

"value": "format:date",

so if i did write this code


$attributes = [
            'key' => 'format:date',
            'value' => settings('format:date')
        ];


i will get Failed asserting that a row in the table [settings] matches the attributes {
    "key": "format:date",
    "value": ":human:"
}

Please or to participate in this conversation.