Mastering Test-Driven Development for Magento
Usually, automated software testing is an activity that is rather postponed. During my 15-year career as a Magento developer, I see that testing is skipped altogether. Only some projects with rather huge budgets and multiple teams can allow writing tests.
I remember days when I learned about tests back in 2010-2011. At that time, I worked with Magento 1 in the Professional Services department, at Varien company. I was excited to add tests, that I used to call “Unit” for the Magento classes and static method calls e.g. Mage::getModel()
. It was rather a hack, but possible to add tests when you workaround a construct method with the conditional object instantiation. At the same time, allow yourself the possibility to pass a mocked object via __construct()
a method under test.
PHPUnit library was the only one. Nowadays, there are multiple tools and libraries a PHP developer can use to write automated tests.
Later, when I started working with Magento 2, the real use of test-driven development came to my life.
What is Test-Driven Development?
It is a way of writing code that involves writing an automated unit-level test case that fails, then writing just enough code to make the test pass, refactoring both the test code and the production code, and then repeating with another new test case.
It is rather hard to imagine a software developer, without prior knowledge of Magento 2 framework to dive into the TDD. This article is for those who would like to understand TDD, even though, it might be a tough one.
TDD Requirements
All you need to have when it comes to writing tests is an installed copy of Magento Open Source or Adobe Commerce. Once you have it, you can leverage the phpunit/phpunit
library dependency from the main composer.json
file.
Usually, Magento 2 module’s tests come together with the module. I’ve seen a few occurrences when tests are managed as a standalone project, however, this is not a practice I would recommend to follow. Tests should come with a Magento 2 module.
Types of Tests for TDD
In the PHP world, developers tend to call integration tests unit tests. I am not sure why this confusion exists. Maybe because of the popular frameworks such as Laravel, or WordPress. In Laravel, every call to a class method is static, and in WordPress, the idea behind adding or extending functionality is based on PHP functions and direct calls to WordPress functions that handle logic.
Unit test is a type of test that covers execution and logic inside a method of a class under test. Only public methods are allowed to be used when testing a method. All protected and private methods have to be tested via public method calls. Any calls to other PHP classes have to be mocked.
Integration test, in comparison to unit test, does not have to have mocks for other PHP classes. It would depend on an integration test scenario and the level of component testing. Integration tests can involve 2 or more PHP objects during their execution. It is also possible to connect to a database and read/write something if required.
The more straightforward type of test for TDD is a Unit. In case you are completely new to TDD, Unit Tests are the ones to create in a Magento 2 module.
What should be covered with TDD?
Test-driven development can cover all new and existing PHP classes. It is easy to add tests for a non-existent class. There are scenarios when you have to modify an existing PHP class to fix a bug. In this case, it is still recommended to follow TDD. The complexity can be in case a class has 5 or more dependencies. An integration test, in this case, maybe of better preference.
Benefits of TDD
There are lots of benefits when it comes to following a test-driven development approach. Starting from saving teams time in the long run, to getting immediate test results when a change has occurred. For more benefits, have a look at the presentation I’ve had a chance to share during the Meet Magento UK conference.
Meet Magento UK
I had the honor of speaking at #MeetMagentoUK 2024, where I presented on Mastering Test-Driven Development (TDD) for Magento. In my session, we explored why adopting the TDD approach is crucial for Magento 2 teams when developing new features and maintaining existing ones.
Discover more about Test-Driven Development and learn how to master it in your Magento 2 projects.
About Max Pronko
Max is a Magento consultant and teacher at Mage Mastery with 15 years of practical development experience building and customizing Magento. Max teaches teams to extend the Magento 2 application by providing practices that allow quick and correct implementation of Magento 2 customizations. Max also shares his knowledge through Magento 2 online courses.