mardi 19 novembre 2019

Function not firing when I run a test

I am new to Laravel so I only have a vague idea of what I am doing. I am doing a feature tests and a function that I know fires when I use postman to test the api, but doesn't during the test. Here is the test

public function testVerify(){
        $this->createTestUserParams();
        $response = $this->post(route('register'), $this->user_params);
        $response->assertOk();
        $user = User::where('email','test@gmail.com')->first();
        if($user){ 
            $token = $user->verifyUser->token;
            $id = $user->verifyUser->user_id;
            $response2 = $this->post(route('email.customVerify'), ['user_id' => $id, 'token' => $token]);
            $response2->assertOk();

            //$user->markEmailAsVerified();
            $this->assertNotNull($user->email_verified_at); 
        }else{
            $this->fail('should find a user');
        }
    }


and here is the code for the function the route controller points to

public function customVerify(Request $request){
        if(!isset($request->user_id)){
            return response()->json(['message' => 'No user ID'],400);
        }
        if(!isset($request->token)){
            return response()->json(['message' => 'No user token'],400);
        }
        $user = User::where('id',$request->user_id)->first();

        if($user == null){
            return response()->json(['message' => 'Bad User Id'],400);
        }
        if ($user->hasVerifiedEmail()) {
            return response()->json(['message' => 'Already verified'],400);
        }

        if($request->token == $user->verifyUser->token){
            if($user->markEmailAsVerified()){
                event(new Verified($user));
                VerifyUser::where('user_id',$user->verifyUser->user_id)->first()->delete();
                return response()->json(['message' => 'Everything is swell'],200);

            }
        }else{
            return response()->json(['message' => 'Bad token'],400);

        }
    }

My problem is that the field email_verified_at is coming back null when it shouldn't.

The strange thing there is an $response->assertOk(); and response will only be OK if the markEmailAsVerified() function fires successfully, otherwise the response will not be code 200. And the markEmailAsVerified() function is doing what it is supposed to because when I invoke it byitself in the test where it is commented out, the test comes back fine.

I am using the passport library for auth if that helps.

Aucun commentaire:

Enregistrer un commentaire