2017-01-13 02:48:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Control\Tests\Email;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Email\Email;
|
|
|
|
use SilverStripe\Control\Email\SwiftMailer;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use Swift_Mailer;
|
|
|
|
use Swift_MailTransport;
|
|
|
|
use Swift_Message;
|
|
|
|
use Swift_NullTransport;
|
|
|
|
use Swift_Plugins_AntiFloodPlugin;
|
|
|
|
|
|
|
|
class SwiftMailerTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function testSwiftMailer()
|
|
|
|
{
|
|
|
|
$mailer = new SwiftMailer();
|
|
|
|
$mailer->setSwiftMailer($swift = new Swift_Mailer(new Swift_NullTransport()));
|
|
|
|
|
|
|
|
$this->assertEquals($swift, $mailer->getSwiftMailer());
|
|
|
|
|
|
|
|
SwiftMailer::config()->remove('swift_plugins');
|
2022-11-15 06:20:54 +01:00
|
|
|
SwiftMailer::config()->merge('swift_plugins', [Swift_Plugins_AntiFloodPlugin::class]);
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
/** @var Swift_MailTransport $transport */
|
|
|
|
$transport = $this->getMockBuilder(Swift_MailTransport::class)->getMock();
|
|
|
|
$transport
|
|
|
|
->expects($this->once())
|
|
|
|
->method('registerPlugin')
|
2018-09-26 01:21:37 +02:00
|
|
|
->with(
|
|
|
|
$this->isInstanceOf(Swift_Plugins_AntiFloodPlugin::class)
|
|
|
|
);
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
/** @var Swift_Mailer $swift */
|
|
|
|
$swift = $this->getMockBuilder(Swift_Mailer::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$swift
|
|
|
|
->expects($this->once())
|
|
|
|
->method('registerPlugin')
|
|
|
|
->willReturnCallback(function ($plugin) use ($transport) {
|
|
|
|
$transport->registerPlugin($plugin);
|
|
|
|
});
|
|
|
|
|
|
|
|
$mailer->setSwiftMailer($swift);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSend()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$email->setTo('to@example.com');
|
|
|
|
$email->setFrom('from@example.com');
|
|
|
|
$email->setSubject('Subject');
|
|
|
|
|
2017-03-24 03:50:39 +01:00
|
|
|
$mailer = $this->getMockBuilder(SwiftMailer::class)
|
2020-04-20 19:58:09 +02:00
|
|
|
->setMethods(['sendSwift'])
|
2017-03-24 03:50:39 +01:00
|
|
|
->getMock();
|
2018-09-26 01:21:37 +02:00
|
|
|
$mailer->expects($this->once())->method('sendSwift')->with(
|
|
|
|
$this->isInstanceOf(Swift_Message::class)
|
|
|
|
);
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$mailer->send($email);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendSwift()
|
|
|
|
{
|
|
|
|
$mailer = new SwiftMailer();
|
|
|
|
$sendSwiftMethod = new \ReflectionMethod($mailer, 'sendSwift');
|
|
|
|
$sendSwiftMethod->setAccessible(true);
|
|
|
|
$transport = $this->getMockBuilder(Swift_NullTransport::class)->getMock();
|
|
|
|
$transport->expects($this->once())
|
|
|
|
->method('send');
|
|
|
|
$mailer->setSwiftMailer(new Swift_Mailer($transport));
|
|
|
|
$swiftMessage = new Swift_Message('Test', 'Body');
|
|
|
|
$swiftMessage->setTo('to@example.com');
|
|
|
|
$swiftMessage->setFrom('from@example.com');
|
|
|
|
$sendSwiftMethod->invoke($mailer, $swiftMessage);
|
|
|
|
}
|
|
|
|
}
|