dillscher's avatar

Database table not available in test as service providers run before use DatabaseMigrations?

Hi,

I do some tests with PHPUnit on different views. Part of the view variables, i. e. $topics, is loaded through a service provider like the following:

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {

        if (Schema::hasTable('topics')) {
            $topics = Topic::all();
            View::share(compact('topics'));
        }

    }

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

Unfortunately running the test throws an error as the variable $topics is undefined. Further debugging showed me that the table "topics" is not available within the ComposerServiceProvider yet, when running tests.

What am I doing wrong?

Cheers,

Christian

0 likes
2 replies
dillscher's avatar

As far as I understand, the trait DatabaseMigrations has not run yet, but the service provider has been hit before, so there aren't any tables available at all. Does anybody know how to work around this issue?

Cheers

Christian

dillscher's avatar
dillscher
OP
Best Answer
Level 27

Ok,

sorted it out by calling my View::share within the setup method in TestCase, but still I am interested in an alternative ;-).

Cheers

Christian

Please or to participate in this conversation.