• About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
  • About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
March 18, 2015

Integration Testing with Swift Mailer

Posted by Christopher Davis

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

External Code, Integration Tests

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.

Transports and Spools

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.

Swift Mailer Test Goals

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.

Some Code, Finally

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.

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.

PHPswift mailer
Previous
Next

Latest White Papers

  • Shifting Plans for 2020 & Beyond
  • Game On: How Brands Can Log Into A Diverse Multi-Billion Dollar Industry
  • What CCPA Means For Brands
  • How Google is Improving Consumer Data Privacy
  • Ways to Prepare for the Cookieless Future
  • See all White Papers

Featured Posts

  • Ad Age Names PMG #1 Best Place to Work in 2021
  • MediaPost Names PMG Independent Agency of the Year
  • PMG Client Portfolio Trends During Amazon Prime Day 2020
  • A Closer Look at the Congressional Big Tech Market Power Report
  • What to Know About Reddit

Categories

  • Consumer Insights
  • Content
  • Creative Design
  • Data Analytics
  • Development
  • Digital TV & Video
  • Ecommerce
  • Industry News
  • Local
  • Mobile
  • Paid Search
  • PMG Culture
  • Programmatic & Display
  • SEO
  • Social Media
  • Structured Data
Fort Worth

2845 West 7th Street
Fort Worth, TX 76107

Dallas

3102 Oak Lawn Avenue
Suite 650
Dallas, TX 75219

Austin

823 Congress Avenue
Suite 800
Austin, TX 78701

London

33 Broadwick Street
London
W1F 0DQ

New York

120 East 23rd Street
New York, NY 10010

Get in touch

(817) 420 9970
info@pmg.com

Subscribe to the PMG Newsletter
© 2021 PMG Worldwide, LLC, All Rights Reserved
  • Contact
  • Privacy Policy
 Tweet
 Share
 Tweet
 Share
 Tweet
 Share
 LinkedIn
We and our partners use cookies to personalize content, analyze traffic, and deliver ads. By using our website, you agree to the use of cookies as described in our Cookie Policy.