Keith Brink
Keith Brink

Reputation: 11

Laravel DatabaseMigrations produces error: Call to a member function call() on null

I'm trying to run DatabaseMigrations on my unit tests, but I get the following error:

1) VisitStaffPagesTest::testLogBills
Error: Call to a member function call() on null

/Users/x/Documents/office/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:312
/Users/x/Documents/office/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:12

From DatabaseMigrations:

public function runDatabaseMigrations()
{
    $this->artisan('migrate'); // This is line 12

    $this->beforeApplicationDestroyed(function () {
        $this->artisan('migrate:rollback');
    });
}

From ApplicationTrait:

public function artisan($command, $parameters = [])
{
    return $this->code = $this->app['Illuminate\Contracts\Console\Kernel']->call($command, $parameters);
}

Any ideas why I'm getting this error?

Upvotes: 0

Views: 1096

Answers (2)

Keith Brink
Keith Brink

Reputation: 11

I ended up solving this using this code in my TestCase.php file:

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    $this->code = $app['Illuminate\Contracts\Console\Kernel']->call('migrate');
    $this->beforeApplicationDestroyed(function () use ($app) {
        $app['Illuminate\Contracts\Console\Kernel']->call('migrate:rollback');
    });

    return $app;
}

Essentially I'm just calling the migration and rollbacks manually. Not sure why it works and the other doesn't.

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

I think you should modify createApplication method in your TestCase from

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    return $app;
}

to:

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    $this->app = $app;  // line added

    return $app;
}

Upvotes: 0

Related Questions