lundi 26 avril 2021

How to create a test for a pivot table?

I want to create a test for the controller. There are three tables, Roles, Categories and Categories_roles, the connection is many to many.

Controller:

/**
 * Update connections.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function update(Request $request) {

    $input = $request->get('category', []);
    foreach (Category::all() as $category) {
        $category->roles()->sync(Arr::get($input, $category->id, []));
    }
    return view('home');
}

I can not figure out how to write a test, there is a view in my head but I don't know how to implement. How I started to do.

Test:

/** @test */
public function test_admin_user_can_edit_categories_roles()
{
    $this->actingAs(\App\Models\User::factory()->create(['role_id' => 1]));
    $input = [
        "1" => [
            "1" => "1"
        ],
        "2" => [
            "1" => "1"
       ],
    ];
    $this->post('/admin/connections', $input);
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3dUkE1S
via IFTTT

Aucun commentaire:

Enregistrer un commentaire