The checkout is usually one of the most heavily adjusted parts of an online store. Besides the visual appearance and the UI, adjustments typically cover some backend parts too. For example: you adjust the e-mails which are being sent at checkout success, or you send some custom data to a message queue or put it into the database.
As this is a highly critical part of every online store, it makes sense to cover the checkout with automated tests. For the frontend, you may use MFTF (Magento Functional Testing Framework) or cypress.io. This blog posts is about the backend part of the checkout, the PHP part which handles all the data comingh through. We use Integration Tests for testing this backend part of the checkout.

Some background information about Integration Tests in Magento 2

There are several layers of automated tests in Magento 2. Let’s list the most important ones here:

  1. Unit Tests – they test the behaviour of a single component (i.e. a class) without external dependencies.
  2. Integration Tests – they test how different components, including framework and resources like databases, work together.
  3. MFTF (Frontend Tests) – they test how the frontend behaves.

Magento 2 comes with a full integration testsuite which covers all functionalities. It is based on a testing framework, which is again based on PhpUnit. This is also a good foundation for writing your own tests. Please read my Introduction to Integration Testing including a setup guide and some small example tests if you want to know more about Magento 2 integration tests.

The approach to testing the checkout

There are two parts we want to test for:

  1. Assert that the checkout works and isn’t blocked on the server side
  2. Assert that the resulting order contains the data we expect

The Magento 2 checkout is built with Javascript on the frontend, communicating with the Magento 2 server via REST APIs. As we don’t do Javascript frontend tests, we directly emulate the API requests and call the underlying PHP methods.

In this blog post, we cannot cover all possible checkout options in Magento 2. To simplify the examples, we are using the Guest Checkout. Depending on your requirements, you should add additional tests covering the checkout for logged in customers and possibly different products, shipping methods, payment methods and other checkout options.

1. Preparing the API classes

At the beginning of the test class, we initialize the needed objects:

2. Creating fixtures

The integration tests need some data in the database. In our case, it’s only one product which can be put into the shopping cart. In other cases, the fixtures can also contain customers or more products of different types.

For more simplicity, we use my colleague Fabian‘s Fixture Framework and add the following code to the setUp method:

3. Create a new empty cart

We now start to emulate the different checkout steps. The first step is to create an empty cart (a.k.a. quote).

The method we use will return a cart ID, which is a random string. This cart ID will be used throughout the other API methods in order to identify the cart.

The public method “testCreateOrder” we create is the method which will be executed by the test framework in the end. It will be extended in the upcoming steps.

4. Add a product to the cart

We look up the SKU of the product which we have created as a fixture and add a quantity of two of this product to the cart.

5. Set addresses and shipping method

We need to add a billing address, a shipping address, and a shipping method. We choose “freeshipping” here as it is the simplest option.

6. Set payment information

We choose the “checkmo” payment method because of simplicity. It doesn’t require any more information than the method code.

7. Place order

We need the cart ID and the payment object from the previous step here. After executing the “placeOrder” method, we will have an order in our database. We do a simple check if the order creation has succeeding by asserting that the order ID is a number greater than or equal to 1.

Checking the order data

We don’t only want to assert that the order has been created, but also that its data is correct. Here is a simple example of how to check that the grand total has been collected correctly:

Full example

After we have explained the individual steps in detail above, you can find below the complete example:

Advanced: Check if a method has been called and its parameters

In our use case, we were exporting order data to a message queue. As we don’t want to access a RabbitMQ server directly in our integration tests, we are just checking if the method to write the order data to the queue is being called, and that the order data is correct. Thus, we added this part to the “testCreateOrder” method before the “placeOrder” call.

Please note that you need to tell the object manager to use the mock instead of the original class before instantiation, i.e. during setup. That’s what the “addSharedInstance” call is for.

Alternative: create the full order as a fixture

This example implements all the different steps of checkout. In many cases, it may be enough to just generate an order as a fixture and test how the created order behaves after checkout.

The latest version TddWizard_Fixtures supports just that: create an order as a fixture.

And now I hope you can put this information on integration tests to good use in your Magento project.

About integer_net

integer_net is one of the leading service providers for developing Magento shops, based in Aachen, Germany. Our team holds the world-wide record with 7 “Magento Master” awards sind 2017. We are an official Magento solution partner with a strong focus on quality and reliability. Since 2012, we successfully create online stores and support them in the long term.

You can find more information about integer_net on our team page.

Andreas von Studnitz

Author: Andreas von Studnitz

Andreas von Studnitz is a Magento developer and one of the Managing Directors at integer_net. His main areas of interest are backend development, Magento consulting and giving developer trainings. He is a Magento 2 Certified Professional Developer Plus and holds several other Magento certifications for both Magento 1 and Magento 2. Andreas was selected as a Magento Master in 2019 and 2020.

More Information · Twitter · GitHub · LinkedIn