Testing Laravel Service Providers

I (we) create a package that acts as the main component for our future CMS, and, of course, the package needs some unit tests. When a package is registered, the first thing it does is set the back / frontend context as follows:

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

    public function register()
    {
        // Switch the context.
        // Url 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 am in the / admin url, the app ('build.context') variable will be set to backend, otherwise it will be set to `frontend.

To test this, I 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 run the code in the browser (go to / admin), the context will be picked up and the call app('build.context')will return backend, but when I run this test I always get a "frontend".

Is there something I didn't notice, or some kind of wrong code when using phpunit?

Thanks in advance

+4
1

, . , laravel - , , - . / TestCase.php.

, , (, /admin ). , - .

, , /admin/test _context,

$this->visit('admin/test_context')->see(Context::BACKEND);

, . , laravel, , .

+3

All Articles