-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMailServiceTest.php
More file actions
242 lines (190 loc) · 7.19 KB
/
Copy pathMailServiceTest.php
File metadata and controls
242 lines (190 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace Bow\Tests\Mail;
use Bow\Configuration\Loader as ConfigurationLoader;
use Bow\Mail\Contracts\MailAdapterInterface;
use Bow\Mail\Envelop;
use Bow\Mail\Exception\MailException;
use Bow\Mail\Mail;
use Bow\Tests\Config\TestingConfiguration;
use Bow\View\Exception\ViewException;
use Bow\View\View;
use InvalidArgumentException;
class MailServiceTest extends \PHPUnit\Framework\TestCase
{
private ConfigurationLoader $config;
protected function setUp(): void
{
$this->config = TestingConfiguration::getConfig();
Mail::configure($this->config["mail"]);
View::configure($this->config["view"]);
}
public function test_configuration_instance()
{
$mail = Mail::configure($this->config["mail"]);
$this->assertInstanceOf(MailAdapterInterface::class, $mail);
}
public function test_default_configuration_must_be_smtp_driver()
{
$mail = Mail::configure($this->config["mail"]);
$this->assertInstanceOf(\Bow\Mail\Adapters\SmtpAdapter::class, $mail);
}
public function test_configuration_must_be_native_driver()
{
$config = $this->config["mail"];
$config['driver'] = 'mail';
$mail_instance = Mail::configure($config);
$this->assertInstanceOf(\Bow\Mail\Adapters\NativeAdapter::class, $mail_instance);
}
public function test_get_mail_instance()
{
Mail::configure($this->config["mail"]);
$instance = Mail::getInstance();
$this->assertInstanceOf(MailAdapterInterface::class, $instance);
}
public function test_send_mail_with_view_not_found_for_smtp_driver()
{
$this->expectException(ViewException::class);
$this->expectExceptionMessage('The view [mail_view_not_found.twig] does not exists.');
Mail::send('mail_view_not_found', ['name' => "papac"], function (Envelop $envelop) {
$envelop->to('bow@bowphp.com');
$envelop->subject('test email');
});
}
public function test_send_mail_with_view_not_found_for_native_driver()
{
$this->expectException(ViewException::class);
$this->expectExceptionMessage('The view [mail_view_not_found.twig] does not exists.');
Mail::send('mail_view_not_found', ['name' => "papac"], function (Envelop $envelop) {
$envelop->to('bow@tests.com');
$envelop->subject('test email');
});
}
public function test_envelop_set_recipient()
{
$envelop = new Envelop();
$envelop->to('test@example.com');
$recipients = $envelop->getTo();
$this->assertIsArray($recipients);
$this->assertCount(1, $recipients);
}
public function test_envelop_set_multiple_recipients()
{
$envelop = new Envelop();
$envelop->to(['test1@example.com', 'test2@example.com']);
$recipients = $envelop->getTo();
$this->assertCount(2, $recipients);
}
public function test_envelop_set_subject()
{
$envelop = new Envelop();
$envelop->subject('Test Subject');
$this->assertEquals('Test Subject', $envelop->getSubject());
}
public function test_envelop_set_message()
{
$envelop = new Envelop();
$envelop->setMessage('Test message content');
$this->assertEquals('Test message content', $envelop->getMessage());
}
public function test_envelop_set_from()
{
$envelop = new Envelop();
$envelop->from('sender@example.com', 'Sender Name');
$from = $envelop->getFrom();
$this->assertStringContainsString('sender@example.com', $from);
$this->assertStringContainsString('Sender Name', $from);
}
public function test_envelop_set_from_without_name()
{
$envelop = new Envelop();
$envelop->from('sender@example.com');
$this->assertEquals('sender@example.com', $envelop->getFrom());
}
public function test_envelop_set_html_content()
{
$envelop = new Envelop();
$envelop->html('<h1>HTML Content</h1>');
$this->assertEquals('<h1>HTML Content</h1>', $envelop->getMessage());
$this->assertEquals('text/html', $envelop->getType());
}
public function test_envelop_set_text_content()
{
$envelop = new Envelop();
$envelop->text('Plain text content');
$this->assertEquals('Plain text content', $envelop->getMessage());
$this->assertEquals('text/plain', $envelop->getType());
}
public function test_envelop_with_custom_header()
{
$envelop = new Envelop();
$envelop->withHeader('X-Custom-Header', 'CustomValue');
$headers = $envelop->getHeaders();
$this->assertContains('X-Custom-Header: CustomValue', $headers);
}
public function test_envelop_invalid_email_throws_exception()
{
$this->expectException(InvalidArgumentException::class);
$envelop = new Envelop();
$envelop->to('invalid-email');
}
public function test_envelop_get_charset()
{
$envelop = new Envelop();
$this->assertEquals('utf-8', $envelop->getCharset());
}
public function test_envelop_get_type()
{
$envelop = new Envelop();
$this->assertEquals('text/html', $envelop->getType());
}
public function test_envelop_add_file_throws_exception_for_nonexistent_file()
{
$this->expectException(MailException::class);
$this->expectExceptionMessage('file was not found');
$envelop = new Envelop();
$envelop->addFile('/path/to/nonexistent/file.pdf');
}
public function test_envelop_chain_methods()
{
$envelop = new Envelop();
$result = $envelop->to('test@example.com')
->subject('Chained Subject')
->from('sender@example.com');
$this->assertInstanceOf(Envelop::class, $result);
$this->assertEquals('Chained Subject', $envelop->getSubject());
}
public function test_envelop_with_named_email_format()
{
$envelop = new Envelop();
$envelop->to('John Doe <john@example.com>');
$recipients = $envelop->getTo();
$this->assertCount(1, $recipients);
$this->assertEquals('John Doe', $recipients[0][0]);
$this->assertEquals('john@example.com', $recipients[0][1]);
}
public function test_envelop_compile_headers()
{
$envelop = new Envelop();
$envelop->to('test@example.com')
->subject('Test')
->from('sender@example.com');
$headers = $envelop->compileHeaders();
$this->assertIsString($headers);
$this->assertStringContainsString('Mime-Version', $headers);
}
public function test_envelop_set_message_with_type()
{
$envelop = new Envelop();
$envelop->setMessage('Custom message', 'text/plain');
$this->assertEquals('text/plain', $envelop->getType());
$this->assertEquals('Custom message', $envelop->getMessage());
}
public function test_envelop_multiple_calls_to_same_method()
{
$envelop = new Envelop();
$envelop->to('first@example.com');
$envelop->to('second@example.com');
$recipients = $envelop->getTo();
$this->assertCount(2, $recipients);
}
}