I'm testing a repository I'm writing, and I'd like to create a test DB (in memory) to make some tests.
Basically, I would like to create a Schema which would be migrated in my test constructor, for instance:
Schema::create('empty_test_models', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Currently, here is the error: Fatal error: Uncaught Error: Call to a member function connection() on null. Maybe it's not the right way, is there a way other than a raw statement?
You can run raw statements using DB::statement('your query here');. However I don't think that is what you need. When you run your tests Laravel will read out the phpunit.xml file. If you want you can you can configure an in memory database there and use that in your tests! As long as you migrate the database during your test of course!
@Max13 Why would you want to create a separate migration for your tests? Your tests should test things in the real world right? How much value does a test add if the test database is different from your production database?
@bobbybouwmann Short answer, no offense, simply because I want.
Long answer: As I introduced, I'm writing a repository (and an abstract base class) and I'm trying to write some tests. Tests should test units (or small portions) of my app, and while my base repo test tests the interactions between the repo and the underlying builder, I'd also like to test (on a simple dummy in-memory table) that the base repo will return expected results.
In this case, I don't need to test real world interactions, but rather an expected behavior no matter the table/model structure. Which lead me to create a dummy table (see Schema in my first post).
You can create a separate directory where you store the migrations for your tests. Then you simply run the artisan command in your tests specifying the location of the migration.