Testing Symfony2 emails with Behat 3

I followed Behat 2.5 docs to check emails. After a few tricks that match Behat 3, I ended up with the following code (I removed the unnecessary parts):

public function getSymfonyProfile() { $driver = $this->mink->getSession()->getDriver(); if (!$driver instanceof KernelDriver) { // Throw exception } $profile = $driver->getClient()->getProfile(); if (false === $profile) { // Throw exception } return $profile; } /** * @Then I should get an email with subject :subject on :email */ public function iShouldGetAnEmail($subject, $email) { $profile = $this->getSymfonyProfile(); $collector = $profile->getCollector('swiftmailer'); foreach ($collector->getMessages() as $message) { // Assert email } // Throw an error if something went wrong } 

When I run this test, it throws the following error :

 exception 'LogicException' with message 'Missing default data in Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector' in vendor/symfony/swiftmailer-bundle/Symfony/Bundle/SwiftmailerBundle/DataCollector/MessageDataCollector.php:93 Stack trace: #0 vendor/symfony/swiftmailer-bundle/Symfony/Bundle/SwiftmailerBundle/DataCollector/MessageDataCollector.php(122): Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector->getMailerData('default') #1 features/bootstrap/FeatureContext.php(107): Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector->getMessages() 

My profiler is configured as follows:

 # app/config/config_test.yml framework: test: ~ profiler: enabled: true collect: true 

It seems that the Profile is loaded correctly and the MessageDataCollector from Swiftmailer exists, but it does not do its job as expected. Any clue to solve this problem?

+5
source share
2 answers

You may have fixed the problem because I no longer have it (I use Behat v3.0.15, the BrowserKit 1.3. * Driver and Symfony v2.6.6).

I managed to reproduce your error, but only when I forgot to enable the collection of profiler data:

 profiler: collect: false 

Once this problem was resolved (the configuration that you provided to solve the problem for me), I managed to check the email in my Behat tests. Two solutions for this:

Solution # 1: intercept redirection worldwide

If this does not break all your other tests, you can do this by setting up your web profiler as follows:

 web_profiler: intercept_redirects: true 

Decision No. 2: Preventing Temporary Client Access to Redirects

For its part, intercepting redirects around the world in the configuration violated most of my other functional tests. Therefore, I use this method.

As the prevention of redirection allows you to mainly check data in data collectors, I decided to use the @collect tag for each script that requires redirecting interception. Then I used @BeforeScenario and @AfterScenario to enable this behavior only for these scenarios:

 /** * Follow client redirection once * * @Then /^(?:|I )follow the redirection$/ */ public function followRedirect() { $this->getDriver()->getClient()->followRedirect(); } /** * Restore the automatic following of redirections * * @param BeforeScenarioScope $scope * * @BeforeScenario @collect */ public static function disableFollowRedirects(BeforeScenarioScope $scope) { $context = $scope->getEnvironment()->getContext(get_class()); $context->getDriver()->getClient()->followRedirects(false); } /** * Restore the automatic following of redirections * * @param AfterScenarioScope $scope * * @AfterScenario @collect */ public static function restoreFollowRedirects(AfterScenarioScope $scope) { $context = $scope->getEnvironment()->getContext(get_class()); $context->getDriver()->getClient()->followRedirects(true); } 
+4
source

This is not the answer you are looking for, but I am sure that it will satisfy your needs (perhaps more).
If I can suggest, try using Mailcatcher with this package: https://packagist.org/packages/alexandresalome/mailcatcher
You can easily check whether emails are sent, what is their topic, follow the link in the body and so on ...


+3
source

Source: https://habr.com/ru/post/1207400/


All Articles