3

I have run into a major problem while writing tests. I am using Laravel 5.6.0 as framework and PHPUnit 7.0 for testing. As a testing DB I have used sqlite with storage in memory. This is from my database.php:

'sqlite_testing' => [
    'driver'   => 'sqlite',
    'database' => ':memory:',
    'prefix'   => '',
],

But my problem is that I have several places where I use whereRaw, for example $query->whereRaw('STR_TO_DATE(CONCAT(date, " ",from), "%Y-%m-%d %k") < ?', [$before]);. The problem here is that sqlite does not have the STR_TO_DATE or CONCAT functions that MySQL has. So PHPUnut throws a bunch of errors because of that.

My solution was instead using a MySQL DB as testing DB. But this doesn't seem to work since I get several different errors, mostly I have several tests where foreign key constraint fails.

One exaple for this is that I have the following in my Base TestCase setUp method:

if (Schema::hasTable('gym_schedule') && !empty(GymSchedule::createGymSchedules())) {
    $this->artisan('db:seed', ['--class' => 'GymScheduleTableSeeder']);
}

This fails every time except the first because it says that a schedùle with id 1 already exists (id is my primary key). I did try to truncate all tables between each test class using tearDown, but that did not help at all, and also the testing became reeeeally slow, like 3 seconds for each test.

So basically that approach does not work either.

I am at a loss. I have tried googling this and searching through StackOverflow. I am open to any suggestion that is not too complicated (either remove all MySQL functions somehow, solve usage of MySQL or anything else really).

Does anyone have a good idea?

6
  • 1
    Did you try resetting the database with the RefreshDatabase trait ? Commented Nov 19, 2018 at 15:15
  • @RamyHerrira yes I saw now that the previous developer on this project used that in every test, so that is not working. Commented Nov 19, 2018 at 15:31
  • How so ? Could you be more specific ? Is it displaying an error ? Commented Nov 19, 2018 at 15:41
  • 1
    I would argue that testing your application in a way that isn't realistic is kind of redundant Commented Nov 20, 2018 at 17:03
  • 1
    Honestly I don't know if there is anyone who can successfully test a complex laravel application with sqlite queries. Facing the same problems right now with sqlite. No such function CONCAT Commented Sep 30, 2019 at 7:26

1 Answer 1

5

Regarding the first part of the question, unit testing a Laravel app when using whereRaw(), this is how I wrote the methods:

    $raw_condition = "CONCAT(`country_code`, `number`) = ?"; 

    if (env('DB_CONNECTION') === 'sqlite') {
        $raw_condition = "(`country_code` || `number`) = ?";
    }

    return Model::whereRaw($raw_condition, [$number])->firstOrFail();
Sign up to request clarification or add additional context in comments.

Comments

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.