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);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire