Lumen testing environment with MongoDB

In a service provider I set the Mongo database name I am using within the application like this:

    $this->app->bind('MongoDB', function() {
        $client = new MongoClient();
        return $client->selectDB('myproductiondatabase');
    });

When running phpunit to run my tests I want to use a different database that gets recreated on every test. What ive done so far is:

   $db = $this->app->environment('production') ? 'myproductiondatabase' : 'mytestingdatabase';

   $this->app->bind('MongoDB', function() {
        $client = new MongoClient();
        return $client->selectDB($db);
    });

This doesn’t seem quite right. I understand I can make multiple .env files for testing and such. Not sure how when running phpunit from the cmd line it will know which .env file to load.

Whats the best way?


Source: laravel

Leave a Reply