Skip to main content

Setup Drupal Commerce for CI and Behat testing

Published on

I can proudly say that we have been on top of our test coverage in Drupal Commerce. Back in June of 2016 we had removed any trace of Simpletest based tests and moved over to PHPUnit Unit, Kernel, Functional, and FunctionalJavascript tests. Once using PhantomJS for JavaScript testing landed in core we jumped ship. Test coverage is great for the individual project because we can ensure that we ship an (assumedly, mostly) bug-free product. But I believe we should do more than that. So I built my own commerce-project-template.

What is a project template? Well you can pass it to Composer and have a set up Drupal 8 project skeleton. You'd run something like

composer create-project mglaman/commerce-project-template some-dir --stability dev --no-interaction

The end result is a built Drupal 8 site, with Drupal Commerce. You will also have a configuration for using Behat testing out of the box, with existing Drupal Commerce coverage provided. This means you can just tweak and add along the way. I have also added CircleCI and TravisCI integration, providing an example of how to ship your Drupal Commerce project with continuous integration to make sure you deliver a functioning project.

Running Tests

The project comes with a phpunit.xml.dist which has been set up to allow you to run any PHPUnit tests provided by Drupal or contrib from the root directory. Here's an example to how to run the Commerce Unit and Kernel test

./bin/phpunit --testsuite unit --group commerce
./bin/phpunit --testsuite kernel --group commerce

This makes it simpler for you to write your own PHPUnit tests for client code. The PHPUnit file shipped with Drupal core assumes it'll say in the root core directory, meaning it can get lost on any Drupal core update. Which is annoying. I use this setup to provide basic unit and kernel tests for API integrations on our Drupal Commerce projects.

The best part is Behat, of course!

  Scenario: Anonymous users can access checkout
    When anonymous checkout is enabled
      And I am on "/product/1"
      Then I should see "Commerce Guys Hoodie"
    When I press "Add to cart"
      Then I should see "Commerce Guys Hoodie - Cyan, Small added to your cart."
      And I click "your cart"
    Then I press "Checkout"

This allows us to make sure a user can visit the product and add it to cart and reach the checkout. It's obviously quite simple but is also an important check. You can see more examples here:…

Docker ready

In order to have a reproducible testing environment, the repository also contains my Docker setup. It is contained in a docker-composer.yml.dist so that it can be modified and changed. The config/docker directory contains the PHP, nginx, and MariaDB configurations. It ships with MailHog as an SMTP server so that you can debug emails easily. I used the MailHog SMTP server when working on the order receipts we provide in Drupal Commerce 2. And customer communication is a big deal with e-commerce.

Docker also provides a simpler way to ship a way to test Search API backed by Solr.

A way to provide a demo

The project has a script to install my mglaman/commerce_demo project, which provides base products and other configuration to try out Drupal Commerce. This is the base content for the Behat tests. So, if you want to try out Drupal Commerce 2 or pitch it to a client, CxO, or a friend this project makes it pretty simple to spin up an example Drupal Commerce 2 site.

What's next?

Next steps are to add an example catalog backed by Search API into the demo module using the database storage. Once that's set I'll work to have it using Solr as storage and test that, along with custom Solr configuration examples. I'd also like to show some deployment step examples in circleci.yml .

I'm available for one-on-one consulting calls – click here to book a meeting with me 🗓️