jeudi 3 mai 2018

Laravel Testing Request - Service Provider - Middleware Issue

I have a Laravel 5.5 App where I have a Service Provider which I use to put some stuff in the request->attributes to access it everywhere (simplified):

    namespace App\Providers;

    use App\Models\Domain;
    use Illuminate\Http\Request;
    use Illuminate\Support\ServiceProvider;

    class GlobalVarsServiceProvider extends ServiceProvider
    {
        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }

        /**
         * Bootstrap the application services.
         *
         * @param Request $request
         *
         * @return void
         */
        public function boot(Request $request)
        {
            $domain = .. get domain with language and  some logic and cache because of multiple domains ..
            $request->attributes->add(['domain' => $domain]);
        }
    }

I do this in a Service Provider, because then I can already use it in other Service Providers like my ViewComposerServiceProvider, where I compose some stuff for the Views. I'm able to access $domain everywhere like this:

$this->domain = $request->attributes->get('domain');

It works great. BUT not in testing. When I want to access $domain in a Unit Test in a middleware the $request->attributes are empty (In UnitTests as in DuskTests either).

It looks like the testing environment uses a different Request Lifecycle? If yes, what else is different in the testing environment?

What am I doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire