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');