Neon Tsunami

"Too many connections" using PHPUnit for testing Laravel 5.1

July 30, 2015

I wrote a post a year or so ago about dealing with the too many connections error when testing Laravel 4. Now having just migrated a project to Laravel 5.1 I've found that my solution there no longer works, and we need to hook into Laravel's test callback system in order to actually disconnect from the database after a test.

Simply add the following tearDown() method to your TestCase, which will queue up a database disconnect to be performed during a tear down.

public function tearDown()
{
    $this->beforeApplicationDestroyed(function () {
        DB::disconnect();
    });

    parent::tearDown();
}

A blog about Laravel & Rails, written by Dwight Watson.