Symfony Unit Tests

Now that our JobApplicationNotifier is responsible for sending a confirmation email, it’s time to write a unit test for it.

In this lesson, we create a dedicated test class and use PHPUnit’s createMock() method to isolate dependencies - in this case:

  • The MailerInterface, which we mock so we can intercept the send() method without actually sending an email.
  • The LoggerInterface, which we’ll use in the next test to verify logging behaviour.

We use $this->callback() to assert that the email was constructed correctly:

  • The recipient address is set (to)
  • The subject matches our expectations

We’ve also stubbed a second test (markTestIncomplete) for verifying that errors are logged when email sending fails - we’ll complete that in the next lesson.

This is a great example of a pure unit test:

✅ No external services

✅ No framework bootstrapping

✅ Just one class, tested in isolation

Let’s walk through how it works.

Complete and Continue  
Discussion

0 comments