Last active
February 8, 2023 16:12
-
-
Save chrisshennan/9f2c9f6314ad316cd8aa2d74fdcbce40 to your computer and use it in GitHub Desktop.
Test an email has been sent using behat and the Symfony web profiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
mink.context: | |
class: 'Behat\MinkExtension\Context\MinkContext' | |
calls: | |
- [setMink, ['@__behat__.mink']] | |
tags: | |
- { name: fob.context_service } | |
dashboard.features_context.profiler_context: | |
class: DashboardBundle\Features\Context\ProfilerContext | |
arguments: | |
- '@__symfony_shared__.profiler' | |
- '@mink.context' | |
tags: | |
- { name: fob.context_service } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DashboardBundle\Features\Context; | |
use Behat\Behat\Context\Context; | |
use Behat\MinkExtension\Context\MinkContext; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
use Symfony\Component\HttpKernel\Profiler\Profiler; | |
/** | |
* Defines application features from the specific context. | |
*/ | |
class ProfilerContext implements Context | |
{ | |
/** | |
* @var Profiler | |
*/ | |
private $profiler; | |
/** | |
* @var MinkContext | |
*/ | |
private $mink; | |
/** | |
* ProfilerContext constructor. | |
* @param Profiler $profiler | |
* @param MinkContext $mink | |
*/ | |
public function __construct(Profiler $profiler, MinkContext $mink) | |
{ | |
$this->profiler = $profiler; | |
$this->mink = $mink; | |
} | |
/** | |
* @param string $email | |
*/ | |
public function iReceiveAnActivationEmail(string $emailAddress) | |
{ | |
// Load the debug profile | |
// Must be a POST request within the last 30 seconds | |
$start = date('Y-m-d H:i:s', time() - 10); | |
$end = date('Y-m-d H:i:s'); | |
$tokens = $this->profiler->find('','',1, 'POST', $start, $end); | |
$profiler = $this->profiler->loadProfile($tokens[0]['token']); | |
// Get the swiftmail collector | |
$collector = $profiler->getCollector('swiftmailer'); | |
// Get all the available messages | |
$messages = $collector->getMessages(); | |
// Check we have only have one message | |
WebTestCase::assertEquals(1, count($messages)); | |
// Get the message | |
$message = reset($messages); | |
// Email is in the X-Swift-To header in test mode as we are not allowing email | |
// delivery in test mode and dev is setting single swiftmailer delivery_address option | |
WebTestCase::assertEquals($emailAddress, reset(array_keys($message->getHeaders()->get('X-Swift-To')->getFieldBodyModel()))); | |
WebTestCase::assertContains('Activate Your Account', $message->getSubject()); | |
WebTestCase::assertRegExp('/<a href="(.*?)".*?>Activate my account<\/a>/', $message->getBody()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this piece of example code, it has been very helpful :)