Writing tests is kinda like eating healthy food. You know that’s good for you and still, you’ll find a perfectly good excuse not to do it. Yes, I am looking at you. I am going to show you a very powerful PHPUnit and Docker setup that not only saves me a lot of time, is extremely simple to implement but on top of that takes away all the excuses.
What we will achieve?
We will create a simple bash script for our project that will communicate for us with the Docker container to run tests. Powerful PHPUnit and Docker setup does not have to be complicated. It just has to do its job.
This script will also be a perfect place for even more functionality you might need in the future. This is how I like to run my tests but of course, this is not the only way to handle this particular issue.
How I did it before the script?
Before I had this magical script in place I would usually open a terminal and log into my container. From there it is just a regular
It’s not the end of the world. However, having to log in to the container every time I wanted to run something in it or writing long and awkward docker commands to execute something in the container without really getting into it became annoying. I wanted to be able to run what I need quickly without typing a lot.
I do realize that there are other ways to do it but what I am about to present is my preferred approach.
The bash script simply communicates with the docker container for us. It executes certain hardcoded commands with arguments we will pass to it.
This way, we will save some time and make the whole process of running tests and even generating the coverage reports super easy. This script lives in the root directory of the project.
#!/bin/bash # test type TESTTYPE=$1 # name of the test to run TESTNAME=$2 case $TESTTYPE in filter) docker exec -t webapp vendor/bin/phpunit --filter=$TESTNAME ;; full) docker exec -t webapp vendor/bin/phpunit $TESTNAME ;; report) docker exec -t webapp vendor/bin/phpunit --coverage-html reports ;; esac
We can do three things here: filter one particular test by its name, filter the entire file with tests and generate the test report.
You might be wondering about the
-t option. It allows us to keep the color in the command output. It is not a requirement but if the PHPUnit results are not in green it kills all the joy.
Although it looks a little bit better now because instead of running a somewhat complicated docker command we can just run the script with two arguments it is not what we wanted just yet. We can do better.
To make it simpler we need to introduce some aliases. I am using MacBook Pro so my example will be based on MacOs. To create aliases for our bash script we need to edit
~/.bash_profile file and add the following content to it.
alias tf="~/Sites/publisher/app.sh filter" alias ft="~/Sites/publisher/app.sh full" alias tr="~/Sites/publisher/app.sh report"
Of course, you need to replace the path to the project. If you find these aliases difficult to remember or confusing you can always change them to something else.
To be honest they were confusing for few days. Now that I am used to them that is not a problem at all. Just keep that in mind. You can get used to things.
Also, keep in mind that you have to reload the profile file, you can do it like this
How to use it?
That’s the easiest part. Just run your new aliases with arguments if any are required. You should be able to run them from any place in your system too. Just look at this awesome example.
Thanks to just a little bit of extra effort you just got yourself an easy and flexible solution for quickly running your tests without having to even look at the Docker stuff.
Is this the only way of doing that? No, it is not, and feel free to explore other options. However, as promised it is a powerful PHPUnit and Docker setup that allows you to run any test or batch of tests you want with a very short and simple command. Let’s take a look at the list of befits.
- No more excuses for not writing your tests! It’s much easier to run them now.
- From a long period of time perspective, you actually do save a lot of time.
- You learned a little bit about the docker.
- You are now separated from the docker a little more to focus more on the development.
- This is an amazing base for other extra functionality you might need to run often for your project.
- Takes very little time to set it all up.