1
votes

I have functional test for my endpoint(api action) postLeadAction and after flush new entity I send email for congratulations. I send email with help SwiftMailer with transport sendGrid. And how to before send email check sebject, fromName, fromEmail, toEmail. Now I run test with --env=test and in config swiftmailer for test environment add spool parameters for put email file for directory and not send for the email

How to mock swiftMailer and before send email check parameters ?

this my config_test.yml

swiftmailer:
default_mailer: default
mailers:
    default:
        transport: %mailer_transport%
        host: '%mailer_host%'
        port: 587
        encryption: ~
        username: '%mailer_user%'
        password: '%mailer_password%'
        spool:
            type: file
            path: '%kernel.root_dir%/spool'

This MailerWrapper class, email send with SwiftMailer function 'send'

class MailerWrapper
{
protected $mailer;
/**
 * @var \Swift_Message
 */

  //some parameters

public function __construct(\Swift_Mailer $mailer)
{
    $this->mailer = $mailer;
}

public function newMessage()
{
    //some parameters

    return $this;
}

public function send()
{
   //some logic with message
    return $this->mailer->send($this->message);
}

I try like cookbook

// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();

$mailCollector = $this->client->getProfile()->getCollector('swiftmailer.mailer.default');

// Check that an email was sent
$this->assertEquals(1, $mailCollector->getMessageCount());

$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];

but have error

PHP Fatal error:  Call to a member function getCollector() on a non-object

UPDATE

in config I don't enable profile

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
        cookie_httponly: true
        cookie_secure: true
    profiler:
        collect: false

but I have error because I enable profile after http request, when I enable before - everything ok

$client->enableProfiler();

$this->request(
    'post',
    $this->generateUrl('post_lead', [], UrlGeneratorInterface::RELATIVE_PATH),
    [],
    [
     // some parameters
    ]
);

$mailCollector = $client->getProfile()->getCollector('swiftmailer');
1
no, I enable profiler in test. But after request, when before - everything ok. I update question - shuba.ivan

1 Answers

2
votes

I tested with Symfony 2.8 and I had to enable the profiler in the configuration:

# app/config_test.yml

framework:
    profiler:
        enabled: true
        collect: false

Your tests should work after having defined enabled: true.

In order to avoid the PHP Fatal error in my tests, I add a small check before testing the emails:

// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();

// Check that the profiler is available.
if ($profile = $this->client->getProfile()) {
    $mailCollector = $profile->getCollector('swiftmailer');

    // Check that an e-mail was sent
    $this->assertEquals(1, $mailCollector->getMessageCount());

    // …
}
else {
    $this->markTestIncomplete(
        'Profiler is disabled.'
    );
}

With this check, the tests will be marked as incomplete by PHPUnit instead of returning an error and breaking the tests suite.