1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Tests\Consumer; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\NotificationBundle\Consumer\SwiftMailerConsumer; |
18
|
|
|
use Sonata\NotificationBundle\Model\Message; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Tests the SwiftMailerConsumer. |
22
|
|
|
*/ |
23
|
|
|
class SwiftMailerConsumerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var SwiftMailerConsumer |
27
|
|
|
*/ |
28
|
|
|
private $consumer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Swift_Mailer |
32
|
|
|
*/ |
33
|
|
|
private $mailer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initializes some dependencies used by tests. |
37
|
|
|
*/ |
38
|
|
|
protected function setUp(): void |
39
|
|
|
{ |
40
|
|
|
$this->mailer = $this->createMock('Swift_Mailer'); |
41
|
|
|
$this->consumer = new SwiftMailerConsumer($this->mailer); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tests the sendEmail method. |
46
|
|
|
*/ |
47
|
|
|
public function testSendEmail(): void |
48
|
|
|
{ |
49
|
|
|
$message = new Message(); |
50
|
|
|
$message->setBody([ |
51
|
|
|
'subject' => 'subject', |
52
|
|
|
'from' => [ |
53
|
|
|
'email' => '[email protected]', |
54
|
|
|
'name' => 'nameFrom', |
55
|
|
|
], |
56
|
|
|
'to' => [ |
57
|
|
|
'[email protected]', |
58
|
|
|
'[email protected]' => 'nameTo2', |
59
|
|
|
], |
60
|
|
|
'replyTo' => [ |
61
|
|
|
'[email protected]', |
62
|
|
|
'[email protected]' => 'nameReplyTo2', |
63
|
|
|
], |
64
|
|
|
'returnPath' => [ |
65
|
|
|
'email' => '[email protected]', |
66
|
|
|
], |
67
|
|
|
'cc' => [ |
68
|
|
|
'[email protected]', |
69
|
|
|
'[email protected]' => 'nameCc2', |
70
|
|
|
], |
71
|
|
|
'bcc' => [ |
72
|
|
|
'[email protected]', |
73
|
|
|
'[email protected]' => 'nameBcc2', |
74
|
|
|
], |
75
|
|
|
'message' => [ |
76
|
|
|
'text' => 'message text', |
77
|
|
|
'html' => 'message html', |
78
|
|
|
], |
79
|
|
|
'attachment' => [ |
80
|
|
|
'file' => 'path to file', |
81
|
|
|
'name' => 'file name', |
82
|
|
|
], |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$mail = $this->createMock('Swift_Message'); |
86
|
|
|
$mail->expects($this->once())->method('setSubject')->with($this->equalTo('subject'))->willReturnSelf(); |
87
|
|
|
$mail->expects($this->once())->method('setFrom')->with($this->equalTo(['[email protected]' => 'nameFrom']))->willReturnSelf(); |
88
|
|
|
$mail->expects($this->once())->method('setTo')->with($this->equalTo(['[email protected]', '[email protected]' => 'nameTo2']))->willReturnSelf(); |
89
|
|
|
$mail->expects($this->once())->method('setReplyTo')->with($this->equalTo(['[email protected]', '[email protected]' => 'nameReplyTo2']))->willReturnSelf(); |
90
|
|
|
$mail->expects($this->once())->method('setReturnPath')->with($this->equalTo(['email' => '[email protected]']))->willReturnSelf(); |
91
|
|
|
$mail->expects($this->once()) |
92
|
|
|
->method('setCc') |
93
|
|
|
->with($this->equalTo(['[email protected]', '[email protected]' => 'nameCc2'])) |
94
|
|
|
->willReturnSelf(); |
95
|
|
|
$mail->expects($this->once()) |
96
|
|
|
->method('setBcc') |
97
|
|
|
->with($this->equalTo(['[email protected]', '[email protected]' => 'nameBcc2'])) |
98
|
|
|
->willReturnSelf(); |
99
|
|
|
$mail->expects($this->exactly(2)) |
100
|
|
|
->method('addPart') |
101
|
|
|
->withConsecutive( |
102
|
|
|
[$this->equalTo('message text'), $this->equalTo('text/plain')], |
103
|
|
|
[$this->equalTo('message html'), $this->equalTo('text/html')] |
104
|
|
|
) |
105
|
|
|
->willReturnSelf(); |
106
|
|
|
$mail->expects($this->once()) |
107
|
|
|
->method('attach') |
108
|
|
|
->willReturnSelf(); |
109
|
|
|
|
110
|
|
|
$this->mailer->expects($this->once())->method('createMessage')->willReturn($mail); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$method = new \ReflectionMethod($this->consumer, 'sendEmail'); |
113
|
|
|
$method->setAccessible(true); |
114
|
|
|
|
115
|
|
|
$method->invoke($this->consumer, $message); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.