background

February 16, 2022

A Magento 2 integration testing helper

Yireo Blog Post

In the past, I worked with Integration Tests frequently. In the past few months, my work on Magento modules fell behind a little bit. And instead of just working on new features (and fixing bugs) without tests, I decided to get my tests straight first. And instead of repeating myself, I found it useful to extract common scenarios in a separate module: Yireo_IntegrationTestHelper.

Assertions

When working with PHPUnit, using assertions is the fundament of your tests. Your custom logic deserves custom assertions (or simply assertions combined with customm logic). But more generic tests end up to be the same again and again. I'm no kata man. I'd like to get up and running fast with stupid stuff. So the integration testing helper ships with various assertions:

  • assertDiFileIsLoaded
  • assertInterceptorPluginIsRegistered
  • assertModuleIsEnabled
  • assertModuleIsRegistered
  • assertModuleIsRegisteredForReal
  • assertPreferenceOf
  • assertStoreConfigValueEquals

There are also some more GraphQL specific assertions like assertGraphQlDataHasData and assertGraphQlDataHasError. And as I'm working on using this helper in more and more places, more assertions will definitely come.

AbstractTestCase parent

All assertions are added as traits: Each trait has a method with the same name. This makes it really simple to use the assertions in your own code, while not relying upon anything else. However, in my cases, I needed so many of the assertions already, that I decided to create a parent testcase class that sits in between the PHPUnit parent and your own test.

Likewise there is a GraphQL parent class as well. Both classes contain few more methods that might or might not be helpful. Check the code for a description.

Toggling TESTS_CLEANUP

One thing you will do frequently when running integration tests is toggle the PHPUnit variable TESTS_CLEANUP in your phpunit.xml file. I repeatedly found myself closing the folder dev/tests/integration/ and reopening it again, just to get that file and change the constant. Instead of this steps, I decided to create a Magento CLI command bin/magento integration_tests:toggle_tests_cleanup toggle the value for me. Earlier this was already part of a DevHacks module but I moved this now to this new Yireo_IntegrationTestHelper module.

And I'm now actively using this Magento command by configuring it in PHPStorm under the Run options as a separate CLI script. In fact, I've got a Run action for enabling TESTS_CLEANUP and another action for disabling it. This alone has been a lifesaver for me already.

Dig it

Hope you like it. I've already been using it in quite a few extensions and helped out other developers during training with it. The module can be found on GitHub: github.com/yireo/Yireo_IntegrationTestHelper

Posted on February 16, 2022

Learn everything there is to learn about Magento 2 development with our courses, starting for backenders with our Magento 2 Backend Development I on-demand training

Read more

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Looking for a training in-house?

Let's get to it!

We schrijven niet te commerciële dingen, we richten ons op de technologie (waar we dol op zijn) en we komen regelmatig met innovatieve oplossingen. Via onze nieuwsbrief kun je op de hoogte blijven van al deze coolness. Inschrijven kost maar een paar seconden.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We schrijven niet te commerciële dingen, we richten ons op de technologie (waar we dol op zijn) en we komen regelmatig met innovatieve oplossingen. Via onze nieuwsbrief kun je op de hoogte blijven van al deze coolness. Inschrijven kost maar een paar seconden.