|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests\Email; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Email\Email; |
|
6
|
|
|
use SilverStripe\Control\Email\SwiftMailer; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
use Swift_Mailer; |
|
9
|
|
|
use Swift_MailTransport; |
|
10
|
|
|
use Swift_Message; |
|
11
|
|
|
use Swift_NullTransport; |
|
12
|
|
|
use Swift_Plugins_AntiFloodPlugin; |
|
13
|
|
|
|
|
14
|
|
|
class SwiftMailerTest extends SapphireTest |
|
15
|
|
|
{ |
|
16
|
|
|
public function testSwiftMailer() |
|
17
|
|
|
{ |
|
18
|
|
|
$mailer = new SwiftMailer(); |
|
19
|
|
|
$mailer->setSwiftMailer($swift = new Swift_Mailer(new Swift_NullTransport())); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertEquals($swift, $mailer->getSwiftMailer()); |
|
22
|
|
|
|
|
23
|
|
|
SwiftMailer::config()->remove('swift_plugins'); |
|
24
|
|
|
SwiftMailer::config()->update('swift_plugins', array(Swift_Plugins_AntiFloodPlugin::class)); |
|
25
|
|
|
|
|
26
|
|
|
/** @var Swift_MailTransport $transport */ |
|
27
|
|
|
$transport = $this->getMockBuilder(Swift_MailTransport::class)->getMock(); |
|
28
|
|
|
$transport |
|
29
|
|
|
->expects($this->once()) |
|
30
|
|
|
->method('registerPlugin') |
|
31
|
|
|
->willReturnCallback(function ($plugin) { |
|
32
|
|
|
$this->assertInstanceOf(Swift_Plugins_AntiFloodPlugin::class, $plugin); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
/** @var Swift_Mailer $swift */ |
|
36
|
|
|
$swift = $this->getMockBuilder(Swift_Mailer::class)->disableOriginalConstructor()->getMock(); |
|
37
|
|
|
$swift |
|
38
|
|
|
->expects($this->once()) |
|
39
|
|
|
->method('registerPlugin') |
|
40
|
|
|
->willReturnCallback(function ($plugin) use ($transport) { |
|
41
|
|
|
$transport->registerPlugin($plugin); |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
$mailer->setSwiftMailer($swift); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testSend() |
|
48
|
|
|
{ |
|
49
|
|
|
$email = new Email(); |
|
50
|
|
|
$email->setTo('[email protected]'); |
|
51
|
|
|
$email->setFrom('[email protected]'); |
|
52
|
|
|
$email->setSubject('Subject'); |
|
53
|
|
|
|
|
54
|
|
|
$mailer = $this->getMock(SwiftMailer::class, array('sendSwift')); |
|
55
|
|
|
$mailer->expects($this->once())->method('sendSwift')->willReturnCallback(function ($message) { |
|
56
|
|
|
$this->assertInstanceOf(Swift_Message::class, $message); |
|
57
|
|
|
}); |
|
58
|
|
|
|
|
59
|
|
|
$mailer->send($email); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testSendSwift() |
|
63
|
|
|
{ |
|
64
|
|
|
$mailer = new SwiftMailer(); |
|
65
|
|
|
$sendSwiftMethod = new \ReflectionMethod($mailer, 'sendSwift'); |
|
66
|
|
|
$sendSwiftMethod->setAccessible(true); |
|
67
|
|
|
$transport = $this->getMockBuilder(Swift_NullTransport::class)->getMock(); |
|
68
|
|
|
$transport->expects($this->once()) |
|
69
|
|
|
->method('send'); |
|
70
|
|
|
$mailer->setSwiftMailer(new Swift_Mailer($transport)); |
|
71
|
|
|
$swiftMessage = new Swift_Message('Test', 'Body'); |
|
72
|
|
|
$swiftMessage->setTo('[email protected]'); |
|
73
|
|
|
$swiftMessage->setFrom('[email protected]'); |
|
74
|
|
|
$sendSwiftMethod->invoke($mailer, $swiftMessage); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|