Testing Magento 1.x Modules / 2: Automated Docker Setup

Gianpiero Addis
4 min readOct 30, 2019

This post is #2 of a series on testing Magento 1 modules. In case you missed it, check out the first post as well.

In the previous article we learned how to set up a Magento shop dedicated to unit and integration testing. Let’s follow up on this idea and simplify the configuration further, so that we can start testing with almost zero effort.

Installing web applications on a local dev machine once used to have a huge cost. To begin with, you had to install and configure at least a web server, PHP and MySQL (and any other technology your stack required) yourself on your host system. Then you had to configure the virtual hosts. Then… well, the list of TODOs is always very long.

Fortunately, there are now better ways to set up a dev environment. With Docker, for instance, it’s just a matter of pulling ready-made images and building the right configuration for your purposes. In our case we also have to install Magento inside the container, initialize modman, deploy the module we want to test in the shop, install PHPUnit, configure the test database… still quite a lot of work, if we had to do it for every module we want to test.

That’s why I prepared a GitHub repository with a ready-to-use configuration. You can simply clone it and start testing immediately. Here is how it works: you can develop your module with or without a shop (maybe inside another container!) while running the tests in the dedicated and isolated testing container. Among other things, this ensures that the tests will pass everywhere and that the module does not rely on shop-specific dependencies.

Setup

On Mac and Linux, the setup is pretty straightforward. First, make sure you have Docker installed, then clone the repository anywhere on your disk:

git clone https://github.com/gpaddis/magento1-docker-phpunit.git

Move to the repository directory, then run the setup script

cd magento1-docker-phpunit
./setup.sh

The script creates the container, installs Magento and EcomDev PHPUnit, configures modman and links the script phpunit-magento to /usr/local/bin (you will have to enter your sudo password).

phpunit-magento is now available globally on your host system. With this command, you can run a test suite from any module directory — directly in the testing container.

Running the tests

phpunit-magento must be executed in a module directory with a modman file available. The script deploys the module in the test shop with modman, then runs the test suite inside the container. After the test run, the module is removed to clean up the test environment.

By default, phpunit-magento will run all tests available in the suite. However, you can also specify which test method or class you want to run. Just pass the name as an argument to the command:

phpunit-magento it_runs_only_this_test_methodphpunit-magento Namespace_Module_Test_Model_Observer

This can help you save some time if you have a big test suite. Integration tests in Magento tend to be quite slow, since the whole framework has to boot up — not exactly a featherweight.

What if you mess up the database?

Maybe you accidentally forgot to delete some test data in the tearDown method and your tests no longer work as expected, or a setup script broke the database once the module was removed… There are many reasons why you might end up with a corrupted test database.

In this case, just run the script setup.sh one more time in the repository directory. This will destroy the container and recreate it from scratch, providing you with a fresh database each time.

Speeding up the tests (Mac)

While I was developing the scripts on a Mac, I noticed that the tests were running way too slow in the container. I have optimized the performance for volume mounts in the docker-compose.yml, but there’s something more you can do to speed up the tests (in my test case, ca. 10 integration tests with intensive database interaction went down from 45s to 9s): just increase the memory allocated for Docker (default: 2GB).

Increase the memory to at least 4 GB

I hope you enjoy your new Magento 1 testing environment. Now go and write some tests!

Credits

A big thank you goes to WebDevOps for their great docker images! You should definitely check these out if you want to create your own custom container.

--

--