vendredi 27 octobre 2017

Lumen - seeder in Unit tests

I'm trying to implement unit tests in my company's project, and I'm running into some weird trouble trying to use a separate set of data in my database.

As I want tests to be performed in a confined environment, I'm looking for the easiest way to input data in a dedicated database. Long story short, to this extent, I decided to use a MySQL dump of inserted data.

This is basically my seeder code:

public function run()
{
    \Illuminate\Support\Facades\DB::unprepared(file_get_contents(__DIR__ . '/data1.sql'));
}

Now here's the problem. In my unit test, I can call the seeder, but :

  • If I call the seeder in the setUpBeforeClass(), it work. Although it doesn't fit my needs as I want to be able to invoke different sets of data for different tests
  • If I call the seeder within a test, the data is never inserted in the database (either with or without the transaction trait).
  • If I use DB::insert instead of ::raw or ::unprepared or ::statement without using a raw sql file, it works. But my inserts are too complicated for that.

Here's a few things I tried with the same results :

    DB::raw(file_get_contents(__DIR__.'/database/data1.sql'));
    DB::statement(file_get_contents(__DIR__ . '/database/data1.sql'));

    $seeder = new CheckTestSeeder();
    $seeder->run();

    \Illuminate\Support\Facades\Artisan::call('db:seed', ['--class' => 'CheckTestSeeder']);

    $this->seeInDatabase('jackpot.progressive', [
        'name_progressive' => 'aaa'
    ]);

Any pointers on how to proceed and why I have different behaviors if I do that in the setUpBeforeClass() and within the test would be appreciated!

Aucun commentaire:

Enregistrer un commentaire