Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

olliejjc16's avatar

Auth::id null when running tests

Hi I'm running tests for a post request to register a user with a teacher role. Registering outside of testing works fine but when I run the test it appears that this line is causing an issue when creating a teacher user

$id = Auth::id();

This gets the id of the logged in user which in this case should be the user id of a logged in SchoolAdmin user. In the app only a logged in user with SchoolAdmin role can create a teacher. The Auth::id gets the logged in id of the SchoolAdmin when the app runs regularly but this causes issues with testing. I tried moving the route to web.php thinking it might be a session issue but didn't solve it

public function test_teachers_registration(){
        $this->withoutExceptionHandling();

        $user = User::factory()
                    ->has(SchoolAdmin::factory()->count(1), 'schoolAdmin')
                    ->create();

        $this->assertGuest();

        $response = $this->post('/login', [
            'email' => $user->email,
            'password' => 'password',
        ]);

        $this->assertAuthenticated();
        
        $response = $this->actingAs($user)->post('/teacher/register', [
            'fullName' => 'Test Teacher',
            'userName' => 'Test Teacher UserName',
            'email' => '[email protected]',
            'password' => 'password',
        ]);

        $this->assertDatabaseHas('users', [
            'name' => 'Test Teacher',
            'username' => 'Test Teacher UserName',
            'email' => '[email protected]',
        ]); 

This is my current test case, need to figure out how to have an authenticated SchoolAdmin user in my test so Auth::id will work and the teacher can be registered.

0 likes
7 replies
olliejjc16's avatar

@silencebringer

my initial test didn't have that in it, it was just

$response = $this->post('/teacher/register', [
            'fullName' => 'Test Teacher',
            'userName' => 'Test Teacher UserName',
            'email' => '[email protected]',
            'password' => 'password',
        ]);

I added acting as when I was trying to figure out a solution and saw that I might have to pass an authenticated user as part of the post request using acting as, it didn't solve the issue though

olliejjc16's avatar

@tykus the error is

Call to a member function currentTeachers() on null

which comes from this code

		$id = Auth::id();
        $schoolAdmin = SchoolAdmin::where('user_id', $id)->first();
        $schoolId =  $schoolAdmin->school_id;
        $school = School::where('id', $schoolId)->first();

        $teacher = Teacher::create([
        ]);
        $schoolAdmin->currentTeachers()->save($teacher);
        $school->currentTeachers()->save($teacher);
        $user->teacher()->save($teacher);

        return $teacher;

I believe its when the Auth::id() is called when running the test is what is returning null which in turn makes the $schoolAdmin variable null which currentTeachers is called on

tykus's avatar
tykus
Best Answer
Level 104

@olliejjc16 It could be either of these

$schoolAdmin->currentTeachers()->save($teacher);
$school->currentTeachers()->save($teacher);

I suspect the latter; where in your test do you set up a School?

Otherwise, your logic is potentially going to result in this same type of error in production; both of these assignments are optimistic that a Model instance will be found:

$schoolAdmin = SchoolAdmin::where('user_id', $id)->first();
// ...
$school = School::where('id', $schoolId)->first();

Coding defensively using firstOrFail would improve matters (IMHO); a relationship between SchoolAdmin and School might be useful either;

$schoolAdmin = SchoolAdmin::where('user_id', $id)->firstOrFail();
$school = School::findOrFail($schoolAdmin->id);
olliejjc16's avatar

Hi Tykus thanks for your help you were right it was the line below that was the issue rather then the Auth::id line it wasn't finding an associated school because none existed

$school->currentTeachers()->save($teacher);

I needed to setup a school automatically in the SchoolAdminFactory so it would have an associated school like this:

public function definition()
    {
        return [
            'membership_status' => 0,
            'school_id' => School::factory(),
        ];
    }

Updated test was this

public function test_teachers_registration(){

        $this->withoutExceptionHandling();

        $schoolAdmin = SchoolAdmin::factory();
        $user = User::factory()
                    ->has($schoolAdmin, 'schoolAdmin')
                    ->create();

        $this->assertGuest();

        $response = $this->actingAs($user)->post('/api/teacher/register', [
            'fullName' => 'Test Teacher',
            'userName' => 'Test Teacher UserName',
            'email' => '[email protected]',
            'password' => 'password',
        ]);

        $this->assertAuthenticated();

        $this->assertDatabaseHas('users', [
            'name' => 'Test Teacher',
            'username' => 'Test Teacher UserName',
            'email' => '[email protected]',
        ]);

        $this->assertDatabaseHas('school', [
            'id' => $response->getData()->school_id
        ]); 
    }

Thanks again!

Please or to participate in this conversation.