Automated Tests in PHP/Lumen + MySQL: How to Configure GitHub Actions

In this blog post, I am going to walk you through how to create GitHub actions that will allow you to utilize MySQL/MariaDB as a testing base to automatically make and run unit tests. Much like the procedure for using Laravel for this type of project, Lumen can be used with only a few added steps, so I decided to use Lumen 6.x-dev for this particular tutorial.

MySQL or SQLite?

Theoretically, SQLite would have been an ideal database to run automated tests, but because it doesn’t have native support for enum types, SQLite won’t work for this type of project. Another reason to implement MySQL instead of SQLite for these automated tests is because the full text search implementation in MySQL is perfect for our purposes. If you were to use SQLite for this type of integration the migrations would fail. Finally, there is a “gotcha” that you need to be aware of if you choose to try this type of integration using SQLite which I will make you aware of at the end of the article.

Make a GitHub workflow file

The first thing you need to do is make a GitHub workflow file. Use the .github/workflows directory to create the file.Before you run the following commands, double check that your are working from your project’s root directory.

github code

The next step is to copy and paste the code below in the file you created.

github code

Look back through the code and pay special attention to the MySQL service under services. This particular code uses a Docker container to use a MySQL image runner. To set up MySQL within the container, you will need to provide a service env. You must duplicate the one in .env.github file, it is extremely important that it is exactly the same. Next, make a .env.github file and copy the following code to paste within the folder.

SQL code

The final part of the setup consists of creating a file called config/database.php in the root of your project (you may already have this type of file within your environment).

HTML code

Moving on to Lumen, you will need to upload this database config file by including it into the bootstrap/app.php file. By default, Lumen will use the database configuration file found in the vendor directory, but since we have made a brand new db config file we have to direct Lumen to use it in lieu of the default option.

app lumen code

In addition, your PHPUnit configuration file needs to look like this:

PHPUnit configuration xml

That’s it, you’re all set! Each time you initiate a push or begin a pull request, you should expect a new workflow to be triggered and this is the process you should:

  • Spin up a new Ubuntu operating system
  • Install PHP
  • Spin up MySQL service inside a Docker container
  • Update/install Composer and install project dependencies
  • Finally, run tests

You can now push the changes to the remote repository. If you would like to view the current run you should be able to see it in the actions tab of your repository. In the event that you worked from a feature branch and utilized a pull request to the main branch, you will be able to see the progress of the build in the pull request. At this point the operation will run and you will be notified if the build passed or failed.

Here is the “gotcha” mentioned earlier

It is extremely likely that you have used the Database migrations throughout your tests. It is possible that you will run into a “too many connections error.” The reason for this is that for all test cases, PHPUnit will tear down the migration and attempt to run it again. If you do come across this error, try the following operation to fix the problem. Use the following code to disconnect the db connection in your TestCase teardown method.

app lumen code

I hope this tutorial was helpful for you!

Are you a Laravel developer interested in growing your software engineering career? Apply to join the Andela Talent Network today.

Related posts

The latest articles from Andela.

Visit our blog

Customer-obsessed? 4 Steps to improve your culture

If you get your team's culture right, you can create processes that will allow you to operationalize useful new technologies. Check out our 4 steps to transform your company culture.

How to Build a RAG-Powered LLM Chat App with ChromaDB and Python

Harness the power of retrieval augmented generation (RAG) and large language models (LLMs) to create a generative AI app. Andela community member Oladimeji Sowole explains how.

Navigating the future of work with generative AI and stellar UX design

In this Writer's Room blog, Carlos Tay discusses why ethical AI and user-centric design are essential in shaping a future where technology amplifies human potential.

We have a 96%+
talent match success rate.

The Andela Talent Operating Platform provides transparency to talent profiles and assessment before hiring. AI-driven algorithms match the right talent for the job.