2

I have a Laravel 4 application with sqlite db configured for testing.

I am working in a workbench package

I have a problem testing my models in a PHPUnit Test, because i defined some unique properties on my model. I run Artisan::call('migrate', array('--bench' => 'vendor/webshop')); from my Basic Test Class from which i extend other Tests.

I think this runs the database migrations, but in my opinion it does not delete the models in the database.

Because if i do

public function setUp() {

    parent::setUp();

    $this->order = Order::create(array(
        "uniquekey" = "123"
    ));
}

I get an error saying, can not insert model because of violation of unique key rule.

How should i clean the database before every test?

1
  • i am simply deleting my model by doing $this->order->forceDelete();, but this is not a really solution to the problem Commented Jul 24, 2013 at 10:23

1 Answer 1

3

You should define environment for testing purposes.

Actually Laravel does have one for you - notice testing folder inside your app/config.

Create app/config/testing/database.php (or modify if exists) and place this:

return array(

    'default' => 'sqlite',

    'connections' => array(

        'sqlite' => array(
            'driver'   => 'sqlite',
            'database' => ':memory:', // this will do the trick ;)
            'prefix'   => '',
        ),
    ),
);

When you run tests, Laravel sets it's environment to 'testing' automaticaly - no need to play with this. SQLite in-memory DB is memory stored withing RAM, thus it's fast - and yes, tests start with empty database as well.

Sign up to request clarification or add additional context in comments.

2 Comments

as i mentioned in the first line i am already doing this, but the database is only cleared for each Class, not for each test, i guess
there is no option to migrate:refresh as discussed here : github.com/laravel/framework/issues/1267#issuecomment-21468659

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.