The test works when you comment out the __construct() because you're not logged in as a user (in the test).
One thing you could try with that line is $this->loggedInUserId = $request->user()?->userId; and see what it outputs for your knowledge.
But since you're testing if someone is authenticated and they're not authenticated in your test, then you should use the actingAs function.
public function testShowOneItemReturnsOnePayroll() {
$user = factory('App\User')->create();
$this->withoutMiddleware();
$this->actingAs($user)->json('GET', "intranetapps/v1/payroll/3");
$this->seeJsonStructure([
'data' => [
'prId',
'userId',
'firstName',
'lastName',
...
]
]);
}
This is also assuming that the id column on your users table is labeled 'userId' and not 'id'
But the fact remains is that your test is withoutMiddleware which removes the authentication middleware and in your controller you're explicitly asking for a user although one isn't set - and it's not gracefully handled when you try to access an attribute on null.