1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkCoreBundle\Tests\Mail; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkCoreBundle\Mail\MessageFactory; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
7
|
|
|
|
8
|
|
|
class MessageFactoryTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var MessageFactory |
12
|
|
|
*/ |
13
|
|
|
protected $messageFactory; |
14
|
|
|
|
15
|
|
|
protected function setUp() |
16
|
|
|
{ |
17
|
|
|
$temp = tempnam(sys_get_temp_dir(), 'message_factory_test'); |
18
|
|
|
$this->messageFactory = new MessageFactory( |
19
|
|
|
$this->getTemplatingMock(), |
20
|
|
|
'mails/default_email.html.twig', |
21
|
|
|
$temp |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inherit |
27
|
|
|
*/ |
28
|
|
|
protected function tearDown() |
29
|
|
|
{ |
30
|
|
|
$this->messageFactory = null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
35
|
|
|
*/ |
36
|
|
|
protected function getTemplatingMock() |
37
|
|
|
{ |
38
|
|
|
$templating = $this->getMockBuilder(EngineInterface::class)->getMock(); |
39
|
|
|
$templating |
40
|
|
|
->method('render') |
41
|
|
|
->willReturn('<html><head></head><body><p>And I, le content</p></body></html>'); |
42
|
|
|
|
43
|
|
|
return $templating; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testTheDefaultsShouldBeEmptyWhenNotSet() |
47
|
|
|
{ |
48
|
|
|
$message = $this->messageFactory->createDefaultMessage(); |
49
|
|
|
|
50
|
|
|
$this->assertInstanceOf('\Swift_Message', $message); |
51
|
|
|
|
52
|
|
|
$this->assertEmpty($message->getFrom()); |
53
|
|
|
$this->assertNull($message->getReplyTo()); |
54
|
|
|
$this->assertEmpty($message->getTo()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testTheDefaultsShouldBeCorrectWhenSet() |
58
|
|
|
{ |
59
|
|
|
$this->messageFactory->setDefaultSender('[email protected]', 'John From'); |
60
|
|
|
$this->messageFactory->setDefaultReplyTo('[email protected]', 'John Reply To'); |
61
|
|
|
$this->messageFactory->setDefaultTo('[email protected]', 'John To'); |
62
|
|
|
|
63
|
|
|
$message = $this->messageFactory->createDefaultMessage(); |
64
|
|
|
|
65
|
|
|
$this->assertInstanceOf('\Swift_Message', $message); |
66
|
|
|
|
67
|
|
|
$this->assertEquals(['[email protected]' => 'John From'], $message->getFrom()); |
68
|
|
|
$this->assertEquals(['[email protected]' => 'John Reply To'], $message->getReplyTo()); |
69
|
|
|
$this->assertEquals(['[email protected]' => 'John To'], $message->getTo()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testCreationOfPlainTextMessage() |
73
|
|
|
{ |
74
|
|
|
$subject = 'I am le subject'; |
75
|
|
|
$content = 'And I, le content'; |
76
|
|
|
|
77
|
|
|
$message = $this->messageFactory->createPlainTextMessage( |
78
|
|
|
$subject, |
79
|
|
|
$content |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->assertInstanceOf('\Swift_Message', $message); |
83
|
|
|
$this->assertEquals($subject, $message->getSubject()); |
84
|
|
|
$this->assertEquals($content, $message->getBody()); |
85
|
|
|
$this->assertEmpty($message->getChildren()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testCreationOfHtmlMessageWithAutomaticPlainText() |
89
|
|
|
{ |
90
|
|
|
$subject = 'I am le subject'; |
91
|
|
|
$content = '<p>And I, le content</p>'; |
92
|
|
|
$expectedResult = <<<EOF |
93
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
94
|
|
|
<html> |
95
|
|
|
<head></head> |
96
|
|
|
<body><p>And I, le content</p></body> |
97
|
|
|
</html> |
98
|
|
|
EOF; |
99
|
|
|
|
100
|
|
|
$message = $this->messageFactory->createHtmlMessage( |
101
|
|
|
$subject, |
102
|
|
|
$content |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf('\Swift_Message', $message); |
106
|
|
|
$this->assertEquals($subject, $message->getSubject()); |
107
|
|
|
$this->assertEquals($expectedResult, $message->getBody()); |
108
|
|
|
|
109
|
|
|
$children = $message->getChildren(); |
110
|
|
|
$this->assertCount(1, $children); |
111
|
|
|
$this->assertEquals('text/plain', $children[0]->getContentType()); |
112
|
|
|
$this->assertEquals( |
113
|
|
|
$this->messageFactory->convertToPlainText($content), |
114
|
|
|
$children[0]->getBody() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testTagsShouldBeStripped() |
119
|
|
|
{ |
120
|
|
|
$this->assertEquals( |
121
|
|
|
'a paragraph', |
122
|
|
|
$this->messageFactory->convertToPlainText( |
123
|
|
|
'<p>a paragraph</p>' |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
$this->assertEquals( |
127
|
|
|
'a paragraph', |
128
|
|
|
$this->messageFactory->convertToPlainText( |
129
|
|
|
'<p> |
130
|
|
|
a paragraph |
131
|
|
|
</p>' |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|