PMG Digital Made for Humans

Integration Testing with Swift Mailer

2 MINUTE READ | March 18, 2015

Integration Testing with Swift Mailer

Author's headshot

Christopher Davis

Christopher Davis has written this article. More details coming soon.

Swift Mailer is PHP library for sending emails. It’s a fantastic library, and highly recommended, but let’s talk testing.

Whenever you use external code, you should have tests that use that code for real. Use mocks or other test doubles with external code makes assumptions about how that external library works. Those assumptions are probably wrong. The answer is a small set of integration tests where your code touches the outside library.

Swift Mailer, being a library, deserves some of that integration testing treatment. We don’t want to really send emails, but we do want to verify that our messages make it through the swift mailer system correctly.

Swift Mailer’s main entry point is a thin wrapper around a transport.

Transports can do just about anything: send messages via a SMTP server or sendmail, write things to a file, or collect messages for later use. Collecting messages is called, in swift mailer terms, a spool. There’s already a transport that collects messages into a spool. If you’re familiar with Symfony, its swift mailer integration collects messages until the kernel terminate event fires then flushes them all with a real transport.

Our goal here isn’t to really send emails. We just want to…

  1. Make sure we’re using swift mailer correctly.

  2. Ensure the expected email is sent through the system.

Using real objects takes care of the first item, and spools take care of the second. We’ll tell our objects to do stuff and collect messages along the way. Once the action is done, we can inspect our spool and the messages it contains.

Swift has a built in memory spool, but it doesn’t give us access to the messages. So let’s extend it to make it countable and provide a getter for the messages.

Now we just need some set up code to create a mailer that uses our new spool. Here that as a PHPUnit test case.

Now we can use our mailer in objects that require it and verify that messages are sent after the fact. Here’s an example.

Stay in touch

Bringing news to you

Subscribe to our newsletter

By clicking and subscribing, you agree to our Terms of Service and Privacy Policy

If you’re using the full stack Symfony framework, there’s a built in way to check emails in acceptance (functional) tests. This method is meant to help with a bit lower level of testing and doesn’t rely on the full stack framework’s test system.


Related Content

thumbnail image

AlliPMG CultureCampaigns & Client WorkCompany NewsDigital MarketingData & Technology

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

thumbnail image

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

thumbnail image

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

thumbnail image

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Testing React

13 MINUTES READ | March 12, 2019

thumbnail image

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

ALL POSTS