Codeception PHP acceptance testing using real email address APIs

codeception email testing

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:

mkdir testsuite && cd $_

Install Composer

Next, install Composer by running the following script:

#!/bin/sh

EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

This will create a composer.phar 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:

curl -L --output selenium-server.jar https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar

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.

curl -s -L "https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz" | tar -xz
chmod +x geckodriver

Require dependencies

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

{
    "require": {
        "mailslurp/mailslurp-client-php": "dev-master",
        "codeception/codeception": "4.1.x-dev",
        "codeception/module-webdriver": "^1.2.0",
        "codeception/module-asserts": "^1.0.0"
    }
}

Next run php composer.phar install to install the dependencies locally.

Bootstrap Codeception tests

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

php vendor/bin/codecept bootstrap

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

tests/
├── acceptance
├── acceptance.suite.yml
├── _data
├── functional
├── functional.suite.yml
├── _output
├── _support
│   ├── AcceptanceTester.php
│   ├── FunctionalTester.php
│   ├── _generated
│   │   ├── AcceptanceTesterActions.php
│   │   ├── FunctionalTesterActions.php
│   │   └── UnitTesterActions.php
│   ├── Helper
│   │   ├── Acceptance.php
│   │   ├── Functional.php
│   │   └── Unit.php
│   └── UnitTester.php
├── unit
└── unit.suite.yml

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

paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor: AcceptanceTester

Configure acceptance test commands

Next edit tests/acceptance.suite.yml and add configure Selenium and Geckodriver to run before tests run:

actor: AcceptanceTester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
        # the lines will run the selenium server before tests start and wait for the process to answer on port 4444
        - Codeception\Extension\RunProcess:
            - java -Dwebdriver.gecko.driver=./geckodriver -jar selenium-server.jar
            - "timeout 300 bash -c 'while [[ \"$(curl -s -o /dev/null -w ''%{http_code}'' localhost:4444)\" != '200' ]]; do sleep 5; done' || false"
modules:
    enabled:
        - WebDriver:
           url: "https://playground.mailslurp.com"
           browser: firefox
        - \Helper\Acceptance
step_decorators: ~        

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 cest 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:

$ php vendor/bin/codecept generate:cest acceptance SignUpTest
Test was created in ~/testsuite/tests/acceptance/SignUpTestCest.php

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:

<?php

class SignUpTestCest
{
    public function canLoadApplicatio(AcceptanceTester $I)
    {
        $I->amOnPage('/');
        $I->seeElement('[data-test="sign-in-header-section"]');
    }
}

Running the tests

We can run our initial test with php vendor/bin/codecept run --steps. We should see passing tests with the output:

Codeception PHP Testing Framework v4.1.20
Powered by PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

Acceptance Tests (1) --------------------------------------------------------------------------------------------------------------------------------------

  [RunProcess] Starting java -Dwebdriver.gecko.driver=./geckodriver -jar selenium-server.jar
  [RunProcess] Starting timeout 300 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:4444)" != '200' ]]; do sleep 5; done' || false
SignUpTestCest: Can load application
Signature: SignUpTestCest:canLoadApplication
Test: tests/acceptance/SignUpTestCest.php:canLoadApplication
Scenario --
 I am on page "/"
 I see element "[data-test="sign-in-header-section"]"
 PASSED 

-----------------------------------------------------------------------------------------------------------------------------------------------------------
  [RunProcess] Stopping java -Dwebdriver.gecko.driver=./geckodriver -jar selenium-server.jar

Functional Tests (0) --------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------

Unit Tests (0) --------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 00:03.858, Memory: 10.00 MB

OK (1 test, 1 assertion)

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:

 public function canTestUserSignUp(AcceptanceTester $I)
 {
    // configure mailslurp client
    $apiKey = getenv("API_KEY");
    if (!$apiKey) {
        throw new Exception("No MailSlurp API_KEY environment variable set");
    }
    $config = MailSlurp\Configuration::getDefaultConfiguration()->setApiKey('x-api-key', $apiKey);

    // create a test inbox
    $inboxController = new MailSlurp\Apis\InboxControllerApi(null, $config);
    $inbox = $inboxController->createInbox();
}

An inbox has an ID and an emailAddress 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:

// load the app
$I->amOnPage('/');
$I->seeElement('[data-test="sign-in-header-section"]');
// click sign-up
$I->click('[data-test="sign-in-create-account-link"]');
// sign up with email and password
$I->fillField('email', $inbox->getEmailAddress());
$I->fillField('password', "test-password");
$I->click('[data-test="sign-up-create-account-button"]');

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

// now we need to receive email
$waitForController = new MailSlurp\Apis\WaitForControllerApi(null, $config);
$email = $waitForController->waitForLatestEmail($inbox_id = $inbox->getId(), $timeout = 30000, $unread_only = true);

// extract the confirmation code
preg_match("/verification code is ([0-9]{6})/", $email->getBody(), $matches);
$code = $matches[1];

Confirm the user

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

// submit the confirmation code
$I->fillField('code', $code);
$I->click('[data-test="confirm-sign-up-confirm-button"]');

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:

// now login
$I->amOnPage("/");
$I->fillField('username', $inbox->getEmailAddress());
$I->fillField('password', "test-password");
$I->click('[data-test="sign-in-sign-in-button"]');

// can see authenticated welcome
$I->waitForElement('h1', 30);
$I->see("Welcome", "h1");

codeception example test

Running the tests

By running API_KEY=your-mailslurp-api-key php vendor/bin/codecept run --steps 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.