Codeception PHP acceptance testing using real email address APIs

How to Test User Sign Up with Real Emails using Codeception and MailSlurp for PHP Applications

  • Table of contents

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:

```php
 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.