The token could not be parsed from the request
For some magic reasons(simulating requests) phpunit can’t pass tests with
Solution is just add few lines to middleware or anywhere else
Got it work with this code in my middleware:
1 2 3 |
if (env('APP_ENV') === 'testing') { JWTAuth::setRequest($request); } |
Result middleware for api auth
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; use JWTAuth; class AuthenticateApi { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */ public function handle(Request $request, Closure $next, $guard = null) { if (env('APP_ENV') === 'testing') { JWTAuth::setRequest($request); } JWTAuth::parseToken(); $user = JWTAuth::parseToken()->authenticate(); return $next($request); } } |