If you write PHP applications then you've probably heard of Codeception - a comprehensive acceptance testing framework for PHP. With Codeception you can test applications written in Laravel, Symfony, Wordpress and more using automated browsers. With the addition of MailSlurp you can test user behavior like sign-up and password reset using real email addresses. MailSlurp is a free PHP library that let's you create test email accounts on demand and the send and receive emails and attachments from code and tests. This post will show you how to test user sign up with real emails using a simple demonstration app.

See full example code on GitHub

A sample application

test password reset php We will use a basic React App hosted at playground.mailslurp.com to demonstrate Codeception and MailSlurp behavior. This web app has a common authentication flow:

  • a user signs up with email address and password
  • a confirmation code is sent to the user's email address
  • the user enters the verification code into the app
  • the user's account is confirmed and the user can log in
  • logging into the app displays a picture of a friendly dog!

codeception example test

Setting up Codeception

Codeception is easier to install using Composer. First, create a new directory:

Install Composer

Next, install Composer by running the following script:

This will create a file in our directory.

Download Selenium and Geckodriver

Codeception runs acceptance tests in real browsers. For this test we will use Selenium to driver Firefox. We need to download the Selenium Standalone Server jar and a Geckodriver binary.

Selenium server

To download Selenium run the following command:

Note you will need Java installed to run the server during tests.

Geckodriver

To automate testing with Firefox and Selenium we need a Geckodriver. This script will install it locally for linux machines. For other platforms see the releases page.

Require dependencies

Now we can specify our dependencies in a file. We will need Codeception for acceptance testing and MailSlurp for creating emails:

Next run to install the dependencies locally.

Bootstrap Codeception tests

Codeception comes with many helpful commands. We can use the vendored dependency to scaffold our app.

This will create a `codeception.yml' file plus a number of test directories:

Edit the file to add the playground url and configure the AcceptanceTester actor. The end result should look like this:

Configure acceptance test commands

Next edit and add configure Selenium and Geckodriver to run before tests run:

Obtain MailSlurp API Key

To test user sign up in our tests we will create test email accounts for each user. MailSlurp is free but you need to an API Key to configure the client.

create api key

Create a free account to get your API Key.

Creating Codeception tests (cests)

Codeception using the term for tests. Let's create one for testing user sign up in our playground app. Again we can use the Codeception dependency in the command line:

Let's add some basic methods.

Loading the app and testing the page

To test our application loads in Firefox let's write a basic test method:

Running the tests

We can run our initial test with . We should see passing tests with the output:

Testing user sign up and confirmation

Now that we have setup some tests and seen how to run them let's test the entire user sign up process end-to-end using a test email account.

Creating test email addresses

We can create real emails in tests using MailSlurp's InboxControllerApi:

An inbox has an and an property. We will use them to sign up a user.

Signing up a user

We can sign up a user by loading the playground app and submitting the email address and a test password:

test authentication

Wait for confirmation and extract code

Next we can wait for the user to receive a confirmation email and parse the verification code from the body:

code

Confirm the user

Once extracted we can use the code to confirm the user.

code

Login and see a welcome

After successfully confirming a user we can login and see a picture of a happy dog. Let's do that:

codeception example test

Running the tests

By running we should see an automated sign up and confirmation

Conclusion

Codeception is a powerful framework for writing end-to-end acceptance tests in PHP. It used with Laravel, Symfony, Wordpress and many other application. By combining Codeception with MailSlurp we can test applications using real email addresses. As you can see in the example above we were able to create real email accounts for each test user and receive confirmation codes. Use MailSlurp and Codeception today to test your own application. Sign up now.