I have been waiting for Jeffery Way's Laravel Testing book since I heard he was writing one. On Tuesday both Laravel 4 and his book was released (along with Dayle Rees book Code Bright).

I read the first 7 chapters while waiting for backups to download. And started testing a simple application model.

I got the first test written no problem. Then I started trying to make sure that the unique fields were actually unique and I ran into a database writing error. The test code was generating items that were not unique. I refreshed the database and my test passed. So since I have more unique areas I needed to figure out how to automatically clear the test database upon the start of the test.

First I tried Artisan::call('migrate:refresh'); But that ended up in an error Artisan class not found

So I tried Stack Overflow for an answer and no one had responded this morning, so I went to IRC, nothing helpful there. So I dug into source again to see what I could find. And still nothing stuck me as the correct approach. I hit the docs under Facades and sure there was a way to call the facades. The problem was the $app variable wasn't there to use. So I looked through the TestCase file again and saw exactly what happened again. It hit me that I could use the only function in there to create the $app variable and then I could use the method described in the documentation.

Yay for being able to figure out the problem. Here is the final code:


    public function setUp()
    {
        $app = $this->createApplication();

        $app->make('artisan')->call('migrate:refresh');

    }

The following also works now:


    public function setUp()
    {
        $app = $this->createApplication();

        Artisan::call('migrate:refresh');

    }

Dragon Hunting

Published on September 30, 2015

First Public Writing

Published on September 30, 2015