robsykes's avatar

Having both SQLite and MySQL running for Tests.

Hi all,

Thanks for taking the time to review this query.

Basically, we have a situation where we have an application that uses some MySQL only functions, i.e. ANY_VALUE, MAX, etc. It is done in this way because of how complex the requirements are for what we are building. This isn't the problem.

The problem we have is during our tests, which use SQLite, those functions do not exist and as such, the tests fail with an error.

What I am asking is, if it is at all possible to have both SQLite and a MYSQL test databases that we can switch between interchangeably. We wish to do this because SQLite provides better performance for testing over MySQL but we need to have a MySQL suite for cases like this where we do RAW queries that use MySQL specific functions.

I've gotten as far as creating a new connection 'mysql_test' in the database.php file and we are by default specifying sqlite as the default connection in the phpunit.xml as such:

<server name="DB_CONNECTION" value="sqlite"/>

What I would like to do is for specific tests is switch this to mysql_test, so it will run on the MySQL database instead.

I am not sure if this is the best way to do it, and I am open to any and all suggestions to get this working.

Thanks for your time, it's very much appreciated.

0 likes
4 replies
Tray2's avatar

I would make it easy for myself and just use mysql for all the tests.

robsykes's avatar

Hi @tray2.

Thanks for your time!

That would indeed make it easier, however, we do wish to still use sqlite and it seems to be more performant, especially on slower local machines, then running those on MySQL. We have quite a lot of tests.

We have managed to find a solution, well, more of a workaround.

We created another phpunit.xml file, named phpunit-mysql.xml and a new database connection, testing. We also created a new MySQL unit/feature test directory structure. We can then run this separately on the command line with:

.vendor/bin/phpunit --configuration phpunit-mysql.xml

That will run against that connection.

It's not what we were hoping for, as we would have to run two commands to do a full test suite, but, we can always tie that up into a shell script or something similar.

Thanks.

Tray2's avatar
Tray2
Best Answer
Level 74

@robsykes That is a pretty good solution.

Just make a small script with both commands in

On Mac and linux

phpunit --configuration phpunit-mysql.xml && phpunit --configuration phpunit.xml

Then it will run them with just one command.

1 like
robsykes's avatar

@Tray2 Thank you! The idea was actually that of my boss, so I can't take the credit for it.

Thanks so much for that script. Much appreciated.

1 like

Please or to participate in this conversation.