Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marek7's avatar

how to test mysql specific function in sqlite

Hi guys, I use mysql db in production and dev, but for testing I use sqlite in memory, because is it much faster. IMHO this is common. I did not have any problem until now. Now I need to test complicated custom sql query with some mysql specific functions like (year, month, sec_to_time, time_to_sec, timediff,...)

What is the best approach to test sql query with mysql specific function which are not present in sqlite?

0 likes
4 replies
mabdullahsari's avatar

The (unfortunate) simple answer is: you cannot.

You have 3 options:

  • Use something in common rather than a driver specific functionality (e.g. there is a FIELD function is MySQL which you cannot use in SQLite, but you can mimic that function using CASE WHEN statements)
  • Don't test that piece of code (you can use a phpunit group annotation and then mark that group as skipped)
  • Implement 2 separate solutions each for a particular SQL grammar.

I'd personally go for option 1 if possible, and option 2 if not. 100% test coverage is not a must IMHO.

1 like
Tray2's avatar

I faced that issue and I created a view with the different syntaxes for SQLite and MySQL. So the tests runs the sqlite version and the migrate command runs the mysql version.

 if (App::environment() == 'local') {
 	//Mysql goes here
       } else {
	//Sqlite goes here
}
1 like
marek7's avatar

Thanks, maybe I found the best solution. Don't use sqlite for testing ;-) because there are more differences between mysql and sqlite and in some cases some tests may pass in sqlite but not in mysql. Read this:

https://www.aaronsaray.com/2019/stop-using-sqlite-in-laravel-unit-tests

I know it is little bit slower to test with mysql, but I don't care.

I have 33 tests now. In sqlite in memory they all run 2.3 sec. in mysql 3,4 sec on my notebook. It is not a big deal.

Please or to participate in this conversation.