Summer Sale! All accounts are 50% off this week.

emartel's avatar

Codeception Email Testing

Hi guys,

First off, let me introduce myself... my name is Eric and I'm a video games developer. I don't know much about web programming but thanks to Laracasts I keep learning :)

While working on a little home project, I felt the need to test the emails I was sending. I tried to create a set of modules for Codeception to interact with Mailhog, Mailtrap and Mailcatcher.

I would like, if some of you guys have time, some feedback on the way I implemented it. The main testing "interface" (using a trait) is available here: https://github.com/ericmartel/codeception-email while the Mailhog Module is available here https://github.com/ericmartel/codeception-email-mailhog (I should be able to commit a working version for Mailtrap either tonight or in the next few days, leaving MailCatcher for later)

Thanks in advance, hopefully this is useful for some of you guys!

Cheers

Eric

0 likes
9 replies
ifpingram's avatar

Hi Eric

Thanks for posting all this. It's probably not that anyone cares, it is more likely that there is less interest as it is Codeception based, which will appeal to a smaller segment of an already small community (i.e. the Laracasts testing community).

I really like the looks of what you have written; it all makes good sense; I use MailTrap and it is a nuisance having to write the code to query it every time I need to use it in my tests.

Unfortunately I only use PHPUnit, so won't be able to use your code. Have you thought about porting it to the other test frameworks like PHPUnit, or even writing it to work with the Laravel testing framework?

1 like
ohffs's avatar

I'm using codeception with a legacy app - this looks really handy! Nice work - thank you! I'll give it a try over the weekend :-)

As @ifpingram says - you might get a bigger response if you ported it to phpunit with possibly a laravel add-on. But I've zero idea how much work that is :-/

1 like
emartel's avatar

Hi!

Thanks for the answers! :)

I guess porting it to PHP Unit would make sense, and it's possibly quite easy to do so as all of the tests are interfaced in a trait.

Great idea guys! Thanks! I went with Codeception as I was following the Larabook tutorial which uses Codeception, had I known it wasn't more popular I would have probably went with something else :)

ohffs's avatar

Thank you - really! This will save me a load of fiddly code :-)

MikeHopley's avatar

I would have been really pleased to find this, if it had been available when I was starting email testing myself.

As it is, I've already written much the same thing just for myself. However, I wouldn't be surprised if yours was better -- it looks like you've put more thought/structure into it.

If I ever have problems with my implementation, I'll probably try yours. :)

emartel's avatar

@ohffs you're welcome!

@MikeHopley sounds good! let me know if you see any issue in my code! Being a C++ guy myself, I wasn't too sure about the design using two traits, but it seems to work just fine

yatsenkolesh's avatar

Hi Hi, I would suggest github.com/proxied-mail/proxiedmail-php-client to test emails with Codeception. It's a package of my product, but I spent some time making it good for email testing, especially with Codeception. Also, I made the example of using this package with Codeception - github.com/proxied-mail/codeception-email-testing-example. There is also a link to the article if you need a detailed guide.

It will look like this

        $config = new Config();
        $config->setApiToken($proxiedMailApiToken);
        $api = PxdMailApinitializer::init($config);
        $proxyEmail = $api->createProxyEmail();
        $I->amOnPage('/email-playground/index.html');
        $I->fillField(['id' => 'name'], 'Tester');
        $I->fillField(['id' => 'email'], $proxyEmail->getProxyAddress());
        $I->executeJS('document.getElementById("submit").click()');
        sleep(3);

        $I->canSee('Check your mailbox');

        $firstEmail = $api->waitUntilFirstEmail($proxyEmail->getId());

        $I->assertSame($firstEmail->getSubject(), 'Code confirmation');
        $text = $firstEmail->getPayload()['stripped-text'];
        //find code after "Your confirmation code is"
        preg_match('/Your confirmation code is ([0-9]+)/', $text, $matches);
        $code = $matches[1];

Also, I'm here and I'm ready to help just in case.

Please or to participate in this conversation.