I don't know exactly how you have your Auth process set up. I have mine as a middleware as follows:
public function handle($request, Closure $next)
{
try {
$user = JWTAuth::parseToken()->authenticate();
Auth::setUser($user);
}
catch (TokenExpiredException $e) {
return response()->json([
'error' => 'Token Expired!',
'statusCode' => (int)401
], 401);
} catch (TokenInvalidException $e) {
return response()->json([
'error' => 'Not Authorized!',
'statusCode' => (int)401
], 401);
} catch (JWTException $e) {
return response()->json([
'error' => 'Not Authorized!',
'statusCode' => (int)401
], 401);
}
return $next($request);
}
Then for testing I do something like this:
JWTAuth::shouldReceive('parseToken->authenticate')
->andReturn($user);
You can do things similarly for the bad data cases. E.g. change the andReturn to andThrow.
Its been a bit since I set this up, but it seems the trouble I had before was with 'parseToken->authenticate'. Since I wasn't following the public method chain and just had 'parseToken' originally it was not working properly. Like I said though it has been a while so I may be mistaken on that.
Also as a side note, the JWT package actually has a built in middleware you can use, if I am not mistaken. I wanted control over the response so I did not go that route, but it is there if it works for you.