lundi 6 juin 2016

Testing Laravel Service Providers

I'm (we're) creating a package that acts as a core component for our future CMS and of course that package needs some unit tests. When the package registeres, the first thing it does is set the back/frontend context like this:

class FoundationServiceProvider extends ServiceProvider
{
    // ... stuff ...

    public function register()
    {
        // Switch the context.
        // Url's containing '/admin' will get the backend context
        // all other urls will get the frontend context.
        $this->app['build.context'] = request()->segment(1) === 'admin'
                ? Context::BACKEND
                : Context::FRONTEND;
    }
}

So when I visit the /admin url, the app('build.context') variable will be set to backend otherwise it will be set to `frontend.

To test this I've created the following test:

class ServiceProviderTest extends \TestCase
{

    public function test_that_we_get_the_backend_context()
     {
        $this->visit('admin');
        $this->assertEquals(Context::BACKEND, app('build.context'));
    }
}

When I'm running the code in the browser (navigating to /admin) the context will get picked up and calling app('build.context') will return backend, but when running this test, I always get 'frontend'.

Is there something I did not notice or some incorrect code while using phpunit?

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire