Codeception PHP acceptance testing using real email address APIs
Write acceptance tests in PHP with real email addresses using Codeception and MailSlurp
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 lets 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
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!
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 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"]');
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:
// 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"]');
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");
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.
Related content
Golang email library for sending and reading emails
Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.
PHP
PHP email API for creating inboxes, sending email, and receiving attachments in code and tests.
Email for testing
Test email accounts for email testing. Alternatives to Mailinator, MailTrap, Mailosaur and more.
How to wait for Selenium to start during Codeception tests
Example tutorial for how to wait until webdriver and Selenium have started during Codeception PHP tests
Using Email APIs to send and receive emails
Generate dynamic email addresses on demand with MailSlurp - a modern email API.
Email API for email marketing and more
APIs for email marketing and social campaign testing. Send, receive, validate and test emails in code and online.
How to test an email address
Test email accounts for testing email addresses in code or online. Create fake email accounts for testing.
How to use Laravel's notification system (PHP tutorial)
Check new notifications queued in PHP using Laravel's email and SMS notification system.
How to start selenium in a background process and wait for it to start
Spawn Selenium server process before tests start for easier acceptance testing.
CypressJS Example
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
CypressJS Email Testing
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Golang mail Library (SMTP)
How to send and receive emails in Go (test email addresses).
Java JVM Examples
Test email sending and receive emails without a mail server.
TestNG Selenium Java Example
Testing user sign up in Java using TestNG and MailSlurp test email accounts
Codeception PHP acceptance testing using real email address APIs
Write acceptance tests in PHP with real email addresses using Codeception and MailSlurp
PHP Email Test Plugins: send and receive email in PHPUnit (example code)
How to send and receive emails in PHPUnit tests.
PyTest Email Testing
Send and receive email in Pytest Python tests.
Java, Selenium
Receive emails in Java test suites using MailSlurp, Junit, and Selenium.
Receive email in PHP: using MailSlurp to send and receive emails
Test email in PHP using real email addresses
Python Robot Framework email test
Python automation email testing Robotframework plugin
Testing authentication using real email addresses in Ruby with Capybara, Cucumber, and Selenium
Cucumber example project using Capybara to test user authentication using real email addresses.
Test applications with real emails using Serenity BDD, JBehave and Selenium
Email acceptance testing with Serenity and MailSlurp. Test applications with real email addresses.
Specflow user sign-up testing with MailSlurp accounts
How to test .NET authentication and sign-up using real email accounts with MailSlurp and SpecFlow.
Jest, Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
.NET Selenium C#
Send and receive email in DotNET Nunit tests using Selenium and MailSlurp.
Cucumber, Ruby
Generate test email accounts with Ruby and Cucumber. Test email sign-up, password verification and more.
Webdriver, JS, WDIO
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
TestCafe end-to-end MFA testing for user sign-up and email verification
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
How To Test Emails Before You Send
There are many free tools to test emails before sending. This can help prevent spam warnings and increase deliverability.
Testing OTP password link username and password for 2 factor authentication (2FA)
Testing OTP password link username and password for 2 factor authentication (2FA)
Test email address
Free test email address for testing emails online with web dashboard or REST API.
How to test 2FA OTP login using SMS codes with Playwright
The ultimate guide to testing OAuth one-time-password flows with real SMS MFA. Use Playwright to automate authentication tests with programmable TXT message APIs.
Testing guide
Integration testing with disposable email accounts using CypressJS, Selenium and many other frameworks. Test OTP password login, transactional emails, notifications and more.
Testing email with Cypress test email accounts
Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.
Testing Webhooks
How to test HTTP webhooks using MailSlurp test hooks.
Send SMTP emails with PHP
Use PHPMailer to send emails with SMTP and MailSlurp
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS