0

I'm trying to make a feature test for the start page of my Laravel application.

Route:

Route::domain('{subdominio}.'.env("APP_DOMAIN"))->middleware('mapNumserie')->group(function () {
    Route::get('/', 'FarmaciaController@show')->middleware('modomtofarmacia')->name('inicio');
})

For some reasons, I use a middleware mapNumserie where I map the subdomain aganist a serial number $idfarmacia in order to use it after in controller:

public function handle($request, Closure $next){
    try{
        $route = $request->route();
        $idfarmacia = \App\Farmacia::where('subdominio', $route->subdominio)->value('numserie');
        $route->setParameter('idFarmacia', $idfarmacia);
        $route->forgetParameter('subdominio');
    }
    catch (\Exception $e){
        abort(404);
    }
    return $next($request);
}

In my ModoMtoFarmacia middleware I just check that an app instance is not in maintenance mode or disabled:

public function handle(Request $request, Closure $next){
        $farmacia = \App\Farmacia::find($request->idFarmacia);
        if($farmacia->inactiva){
            abort(404);
        }
        if(!$farmacia->modomantenimiento){
            return $next($request);
        }
        else{
            return response()->view('errors.503')->setStatusCode(503);
        }
    }

In my controller symply get some data using this $idfarmacia and return a view:

public function show(Request $request, $idfarmacia) {
    $data = 
        //Query...
    if($data){
        return view('inicio', ['data' => $data]);
    }
    else{
        abort(404);
    }

}

With a very simple test method I hope to validate a 200 response :

public function test_example(){
    $response = $this->get('http://mysubdomain.domain.prj');
    $response->assertStatus(200);
}

But when I run the test I get:

Expected response status code [200] but received 404.

The used URL works correctly in web environment, but the test fails. Where am I wrong?

jssDev
  • 923
  • 5
  • 18
  • You could debug in your code, before your condition, to check if $idfarmacia is not empty, and/or debug the process in the Middleware – Noweh Dec 21 '21 at 09:39
  • Ok, I did it. $route = $request->route() gets '/' value in middleware. I don't know the reason. – jssDev Dec 21 '21 at 09:46
  • How should I buld the route? Or how should I build the test to verify that route? – jssDev Dec 21 '21 at 10:05
  • I tested your middleware "mapNumserie" and I have "/" all the time. I don't understand how it works. I think I am missing some information – Noweh Dec 21 '21 at 10:28
  • https://stackoverflow.com/questions/26887666/how-to-get-route-in-middleware-in-laravel – jssDev Dec 21 '21 at 10:31
  • Thanks for your time. As I said, if I debug the same request launched from the browser, the $route variable takes the appropriate value, being the same use case as in the test – jssDev Dec 21 '21 at 10:33
  • To ensure you are hitting the correct url, what does route('inicio', ['subdominio' => 'mydomain']); give you in php artisan tinker? – mrhn Dec 21 '21 at 10:57
  • Your code works for me, in browser and in test. I however had to modify env ("...") in config::get("...") because I use "artisan config:cache" – Noweh Dec 21 '21 at 11:02
  • route('inicio', ['subdominio' => 'mydomain']); in artisan tinker returns "http://mysubdomain.domain.prj" – jssDev Dec 21 '21 at 11:06
  • instead of hard coding the url in the test can you replace it with route('inicio', ['subdominio' => 'mydomain']) and see if the variables are set then – mrhn Dec 21 '21 at 12:52
  • Thanks, I have tried it but the result is the same – jssDev Dec 21 '21 at 12:56

1 Answers1

0

Finally a found the problem in my phpunit.xml settings, which caused an error in the first query (also in any other), in this case the one found in the MapNumserie middleware:

 $idfarmacia = \App\Farmacia::where('subdominio', $route->subdominio)->value('numserie');

I had to set up properly the values of DB_DATABASE and DB_CONNECTION

jssDev
  • 923
  • 5
  • 18