jgravois's avatar

How to test extra values in the ->pivot()

I have an additional column in my permission_user table of permission_level.

I am trying to TTD a helper method but don't know how to test the ->pivot().

This is where I am:

/** @test */
    public function a_user_can_be_assigned_a_permission () {
        $user = create(User::class);
        $permission = create(Permission::class, [
            'name' => 'Returns',
            'display' => 'RMAs'
        ]);

        $pivot = create(PermissionUser::class, [
           'user_id' => $user->id,
           'permission_id' => $permission->id,
           'permission_level' => 4
        ]);

        dump($user->permissions);

        $this->assertCount(1, $user->permissions);
    } // end test
0 likes
2 replies
successdav's avatar

If I did understand you well, you want to test the pivot table if it holds a record with the user id, permission_id and permission_level = 4.

When you dump($user->permissions); what get return?

jgravois's avatar

what I am TRYING to do is to come up with a helper method to add a permission to a user like $user->canDo($permission, ['permission_level' => 4]).

this is the dump:

Illuminate\Database\Eloquent\Collection^ {#1319
  #items: array:1 [
    0 => App\Models\Permission^ {#1242
      #attributes: array:3 [
        "id" => "1"
        "name" => "Returns"
        "display" => "RMAs"
      ]
    // REST DELETED DUE TO FIELD CONSTRAINTS
}

UPDATE I changed the dump to dump($user->permissions->pivot); and get

1) Tests\Unit\PermissionsTest::a_user_can_be_assigned_a_permission
Exception: Property [pivot] does not exist on this collection instance.

Please or to participate in this conversation.