Dwight Watson's blog

"Too many connections" using PHPUnit for testing Laravel 5.1

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

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 by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.