1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Knp\RadBundle\Mailer; |
4
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
6
|
|
|
use Prophecy\Argument; |
7
|
|
|
|
8
|
|
|
class MessageFactorySpec extends ObjectBehavior |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param Swift_Mailer $mailer |
12
|
|
|
* @param Twig_Environment $twig |
13
|
|
|
* @param Twig_ExistsLoaderInterface $loader |
14
|
|
|
* @param Knp\RadBundle\AppBundle\BundleGuesser $bundleGuesser |
15
|
|
|
* @param Symfony\Component\HttpKernel\Bundle\BundleInterface $bundle |
16
|
|
|
*/ |
17
|
|
|
function let($mailer, $twig, $loader, $bundleGuesser, $bundle) |
18
|
|
|
{ |
19
|
|
|
$twig->getLoader()->willReturn($loader); |
20
|
|
|
|
21
|
|
|
$this->beConstructedWith($mailer, $twig, $bundleGuesser); |
22
|
|
|
|
23
|
|
|
$bundleGuesser->getBundleForClass(Argument::any())->willReturn($bundle); |
24
|
|
|
$bundle->getName()->willReturn('App'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Swift_Mime_Message $message |
29
|
|
|
* @param Twig_Template $template |
30
|
|
|
* @param stdClass $subject |
31
|
|
|
* @param stdClass $body |
32
|
|
|
*/ |
33
|
|
|
function it_should_create_a_txt_message_if_only_txt_template_available( |
|
|
|
|
34
|
|
|
$mailer, $twig, $loader, $message, $template, $subject, $body |
|
|
|
|
35
|
|
|
) |
36
|
|
|
{ |
|
|
|
|
37
|
|
|
$loader->exists('App:Mails:contact_message.txt.twig')->willReturn(true); |
38
|
|
|
$twig->loadTemplate('App:Mails:contact_message.txt.twig')->willReturn($template); |
39
|
|
|
$loader->exists('App:Mails:contact_message.html.twig')->willReturn(false); |
40
|
|
|
|
41
|
|
|
$template->renderBlock('subject', array('foo' => 'bar'))->willReturn($subject); |
42
|
|
|
$template->renderBlock('body', array('foo' => 'bar'))->willReturn($body); |
43
|
|
|
|
44
|
|
|
$mailer->createMessage()->willReturn($message); |
45
|
|
|
$message->setSubject($subject)->shouldBeCalled(); |
46
|
|
|
$message->setBody($body, 'text/plain')->shouldBeCalled(); |
47
|
|
|
|
48
|
|
|
$this->createMessage('App\Controller\Message', 'contact_message', array('foo' => 'bar'))->shouldReturn($message); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Swift_Mime_Message $message |
53
|
|
|
* @param Twig_Template $template |
54
|
|
|
* @param stdClass $subject |
55
|
|
|
* @param stdClass $body |
56
|
|
|
*/ |
57
|
|
|
function it_should_create_an_html_message_if_only_html_template_available( |
|
|
|
|
58
|
|
|
$mailer, $twig, $loader, $message, $template, $subject, $body |
|
|
|
|
59
|
|
|
) |
60
|
|
|
{ |
|
|
|
|
61
|
|
|
$loader->exists('App:Mails:contact_message.txt.twig')->willReturn(false); |
62
|
|
|
$loader->exists('App:Mails:contact_message.html.twig')->willReturn(true); |
63
|
|
|
$twig->loadTemplate('App:Mails:contact_message.html.twig')->willReturn($template); |
64
|
|
|
|
65
|
|
|
$template->renderBlock('subject', array('foo' => 'bar'))->willReturn($subject); |
66
|
|
|
$template->renderBlock('body', array('foo' => 'bar'))->willReturn($body); |
67
|
|
|
|
68
|
|
|
$mailer->createMessage()->willReturn($message); |
69
|
|
|
$message->setSubject($subject)->shouldBeCalled(); |
70
|
|
|
$message->setBody($body, 'text/html')->shouldBeCalled(); |
71
|
|
|
|
72
|
|
|
$this->createMessage('App\Controller\Test', 'contact_message', array('foo' => 'bar'))->shouldReturn($message); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Swift_Message $message |
77
|
|
|
* @param Twig_Template $template1 |
78
|
|
|
* @param Twig_Template $template2 |
79
|
|
|
* @param stdClass $subject1 |
80
|
|
|
* @param stdClass $body1 |
81
|
|
|
* @param stdClass $subject2 |
82
|
|
|
* @param stdClass $body2 |
83
|
|
|
*/ |
84
|
|
|
function it_should_create_a_multipart_message_if_both_html_and_txt_templates_available( |
|
|
|
|
85
|
|
|
$mailer, $twig, $loader, $message, $template1, $template2, $subject1, $body1, $subject2, $body2 |
|
|
|
|
86
|
|
|
) |
87
|
|
|
{ |
|
|
|
|
88
|
|
|
$loader->exists('App:Mails:contact_message.txt.twig')->willReturn(true); |
89
|
|
|
$twig->loadTemplate('App:Mails:contact_message.txt.twig')->willReturn($template1); |
90
|
|
|
$loader->exists('App:Mails:contact_message.html.twig')->willReturn(true); |
91
|
|
|
$twig->loadTemplate('App:Mails:contact_message.html.twig')->willReturn($template2); |
92
|
|
|
|
93
|
|
|
$template1->renderBlock('subject', array('foo' => 'bar'))->willReturn($subject1); |
94
|
|
|
$template1->renderBlock('body', array('foo' => 'bar'))->willReturn($body1); |
95
|
|
|
|
96
|
|
|
$template2->renderBlock('subject', array('foo' => 'bar'))->willReturn($subject2); |
97
|
|
|
$template2->renderBlock('body', array('foo' => 'bar'))->willReturn($body2); |
98
|
|
|
|
99
|
|
|
$mailer->createMessage()->willReturn($message); |
100
|
|
|
$message->setSubject($subject1)->shouldBeCalled(); |
101
|
|
|
$message->setBody($body1, 'text/plain')->shouldBeCalled(); |
102
|
|
|
$message->addPart($body2, 'text/html')->shouldBeCalled(); |
103
|
|
|
|
104
|
|
|
$this->createMessage('App\Controller\Message', 'contact_message', array('foo' => 'bar'))->shouldReturn($message); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Swift_Mime_Message $message |
109
|
|
|
*/ |
110
|
|
|
function it_should_throw_exception_if_no_template_available( |
|
|
|
|
111
|
|
|
$mailer, $twig, $loader, $message |
|
|
|
|
112
|
|
|
) |
113
|
|
|
{ |
|
|
|
|
114
|
|
|
$loader->exists('App:Mails:contact_message.txt.twig')->willReturn(false); |
115
|
|
|
$loader->exists('App:Mails:contact_message.html.twig')->willReturn(false); |
116
|
|
|
|
117
|
|
|
$this->shouldThrow('Twig_Error_Loader')->duringCreateMessage('App\Controller\Test', 'contact_message', array()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Swift_Mime_Message $message |
122
|
|
|
* @param Twig_Template $template1 |
123
|
|
|
* @param Twig_Template $template2 |
124
|
|
|
* @param stdClass $subject1 |
|
|
|
|
125
|
|
|
* @param stdClass $body1 |
|
|
|
|
126
|
|
|
* @param stdClass $subject2 |
|
|
|
|
127
|
|
|
* @param stdClass $body2 |
|
|
|
|
128
|
|
|
*/ |
129
|
|
|
function it_should_throw_exception_if_both_templates_provide_no_subject_or_body( |
|
|
|
|
130
|
|
|
$mailer, $twig, $loader, $message, $template1, $template2 |
|
|
|
|
131
|
|
|
) |
132
|
|
|
{ |
|
|
|
|
133
|
|
|
$loader->exists('App:Mails:contact_message.txt.twig')->willReturn(true); |
134
|
|
|
$twig->loadTemplate('App:Mails:contact_message.txt.twig')->willReturn($template1); |
135
|
|
|
$loader->exists('App:Mails:contact_message.html.twig')->willReturn(true); |
136
|
|
|
$twig->loadTemplate('App:Mails:contact_message.html.twig')->willReturn($template2); |
137
|
|
|
|
138
|
|
|
$template1->renderBlock('subject', array('foo' => 'bar'))->willReturn(null); |
139
|
|
|
$template1->renderBlock('body', array('foo' => 'bar'))->willReturn(null); |
140
|
|
|
|
141
|
|
|
$template2->renderBlock('subject', array('foo' => 'bar'))->willReturn(null); |
142
|
|
|
$template2->renderBlock('body', array('foo' => 'bar'))->willReturn(null); |
143
|
|
|
|
144
|
|
|
$this->shouldThrow('Twig_Error_Loader')->duringCreateMessage('App\Controller\Test', 'contact_message', array( |
145
|
|
|
'foo' => 'bar' |
146
|
|
|
)); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|