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?

11
  • You could debug in your code, before your condition, to check if $idfarmacia is not empty, and/or debug the process in the Middleware Commented Dec 21, 2021 at 9:39
  • Ok, I did it. $route = $request->route() gets '/' value in middleware. I don't know the reason. Commented Dec 21, 2021 at 9:46
  • How should I buld the route? Or how should I build the test to verify that route? Commented Dec 21, 2021 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 Commented Dec 21, 2021 at 10:28
  • stackoverflow.com/questions/26887666/… Commented Dec 21, 2021 at 10:31

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.