1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Mailer\Tests; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Psr\EventDispatcher\ListenerProviderInterface; |
7
|
|
|
use Yiisoft\EventDispatcher\Provider\AbstractProviderConfigurator; |
8
|
|
|
use Yiisoft\EventDispatcher\Provider\Provider; |
9
|
|
|
use Yiisoft\Mailer\MessageInterface; |
10
|
|
|
use Yiisoft\Mailer\Event\BeforeSend; |
11
|
|
|
|
12
|
|
|
class BaseMailerTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testCompose(): void |
15
|
|
|
{ |
16
|
|
|
$mailer = $this->getMailer(); |
17
|
|
|
$message = $mailer->compose(); |
18
|
|
|
$this->assertInstanceOf(MessageInterface::class, $message); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testComposeWithView(): void |
22
|
|
|
{ |
23
|
|
|
$mailer = $this->getMailer(); |
24
|
|
|
$viewPath = $this->getTestFilePath(); |
25
|
|
|
|
26
|
|
|
$composer = $mailer->getComposer(); |
27
|
|
|
$composer->setViewPath($viewPath); |
28
|
|
|
$composer->setHtmlLayout(''); |
29
|
|
|
$composer->setTextayout(''); |
30
|
|
|
|
31
|
|
|
$htmlViewName = 'test_html_view'; |
32
|
|
|
$htmlViewFileName = $viewPath . DIRECTORY_SEPARATOR . $htmlViewName . '.php'; |
33
|
|
|
$htmlViewFileContent = 'HTML <b>view file</b> content'; |
34
|
|
|
$this->saveFile($htmlViewFileName, $htmlViewFileContent); |
35
|
|
|
|
36
|
|
|
$textViewName = 'test_text_view'; |
37
|
|
|
$textViewFileName = $viewPath . DIRECTORY_SEPARATOR . $textViewName . '.php'; |
38
|
|
|
$textViewFileContent = 'Plain text view file content'; |
39
|
|
|
$this->saveFile($textViewFileName, $textViewFileContent); |
40
|
|
|
|
41
|
|
|
$message = $mailer->compose([ |
42
|
|
|
'html' => $htmlViewName, |
43
|
|
|
'text' => $textViewName, |
44
|
|
|
]); |
45
|
|
|
$this->assertEquals($htmlViewFileContent, $message->getHtmlBody(), 'Unable to render html!'); |
46
|
|
|
$this->assertEquals($textViewFileContent, $message->getTextBody(), 'Unable to render text!'); |
47
|
|
|
|
48
|
|
|
$message = $mailer->compose($htmlViewName); |
49
|
|
|
$this->assertEquals($htmlViewFileContent, $message->getHtmlBody(), 'Unable to render html by direct view!'); |
50
|
|
|
$this->assertEquals(strip_tags($htmlViewFileContent), $message->getTextBody(), 'Unable to render text by direct view!'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider messagesProvider |
55
|
|
|
*/ |
56
|
|
|
public function testSendMultiple(array $messages): void |
57
|
|
|
{ |
58
|
|
|
$mailer = $this->getMailer(); |
59
|
|
|
$this->assertCount(0, $mailer->sendMultiple($messages)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function messagesProvider(): array |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
[[]], |
66
|
|
|
[[$this->createMessage('foo')]], |
67
|
|
|
[[$this->createMessage('bar'), $this->createMessage('baz')]], |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testSendMultipleExceptions(): void |
72
|
|
|
{ |
73
|
|
|
$mailer = $this->getMailer(); |
74
|
|
|
$messages = [$this->createMessage(''), $this->createMessage()]; |
75
|
|
|
/** @var MessageInterface[] $failed */ |
76
|
|
|
$failed = $mailer->sendMultiple($messages); |
77
|
|
|
$this->assertCount(1, $failed); |
78
|
|
|
$this->assertEquals($messages[0], $failed[0]); |
79
|
|
|
$this->assertEquals(new InvalidArgumentException("Message's subject is required"), $failed[0]->getError()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testBeforeSend(): void |
83
|
|
|
{ |
84
|
|
|
$mailer = $this->getMailer(); |
85
|
|
|
$message = $this->createMessage(); |
86
|
|
|
$this->assertTrue($mailer->beforeSend($message)); |
87
|
|
|
|
88
|
|
|
/** @var Provider $provider */ |
89
|
|
|
$provider = $this->get(ListenerProviderInterface::class); |
90
|
|
|
|
91
|
|
|
$configurator = $this->getProviderConfigurator($provider); |
92
|
|
|
$configurator->attach(static function (BeforeSend $event) { |
93
|
|
|
$event->stopPropagation(); |
94
|
|
|
}); |
95
|
|
|
$this->assertFalse($mailer->beforeSend($message)); |
96
|
|
|
$mailer->send($message); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getProviderConfigurator(Provider $provider) |
100
|
|
|
{ |
101
|
|
|
return new class($provider) extends AbstractProviderConfigurator { |
102
|
|
|
private Provider $provider; |
103
|
|
|
|
104
|
|
|
public function __construct(Provider $provider) |
105
|
|
|
{ |
106
|
|
|
$this->provider = $provider; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function attach(callable $listener, string $eventClassName = ''): void |
110
|
|
|
{ |
111
|
|
|
$this->provider->attach($listener, $eventClassName); |
112
|
|
|
} |
113
|
|
|
}; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|