Passed
Branch master (e8fd46)
by Alexey
02:50
created

MailerTest::defaultFromToCanBeRedefined()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
use PHPUnit\Framework\TestCase;
4
5
class MailerTest extends TestCase
6
{
7
    protected $config = [
8
        'mailer' => [
9
            'transport' => 'mail',
10
            'spool' => [
11
                'type' => 'file',
12
                'path' => 'spool',
13
            ],
14
        ],
15
    ];
16
17
    protected $mailer;
18
19
    public function __construct()
20
    {
21
        $config = new \Venta\Config\Config($this->config);
22
        $eventDispatcher = new \Venta\Event\EventDispatcher();
23
        $this->mailer = new Venta\Mail\Mailer($config, $eventDispatcher);
24
    }
25
26
    /**
27
     * @test
28
     * @expectedException \Venta\Mail\Exception\UnknownTransportException
29
     */
30
    public function attemptToUseNonconfigureTransportThrowsException()
31
    {
32
        $this->mailer->withTransport('gmail');
33
    }
34
35
    /**
36
     * @test
37
     * @expectedException \Venta\Mail\Exception\TransportException
38
     */
39
    public function attemptToUseUnconfiguredSpoolThrowException()
40
    {
41
        $array = [
42
            'mailer' => [
43
                'transport' => 'mail',
44
            ],
45
        ];
46
        $config = new \Venta\Config\Config($array);
47
        $eventDispatcher = new \Venta\Event\EventDispatcher();
48
        $mailer = new Venta\Mail\Mailer($config, $eventDispatcher);
49
        $mailer->spoolWithTransport();
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function configuredTransportsCanBeObtained()
56
    {
57
        $array = [
58
            'mailer' => [
59
                'mail' => [
60
                    'transport' => 'mail',
61
                ],
62
                'smtp' => [
63
                    'transport' => 'smtp',
64
                    'host' => 'localhost',
65
                ],
66
            ],
67
        ];
68
69
        $config = new \Venta\Config\Config($array);
70
        $eventDispatcher = new \Venta\Event\EventDispatcher();
71
        $mailer = new \Venta\Mail\Mailer($config, $eventDispatcher);
72
73
        $this->assertInstanceOf(\Swift_Transport_MailTransport::class, $mailer->withTransport('mail')->getTransport());
74
        $this->assertInstanceOf(\Swift_Transport_SmtpAgent::class, $mailer->withTransport('smtp')->getTransport());
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function defaultFromAndToCanBeSet()
81
    {
82
        $array = [
83
            'mailer' => [
84
                'transport' => 'mail',
85
                'from' => '[email protected]',
86
                'to' => '[email protected]',
87
            ],
88
        ];
89
        $config = new \Venta\Config\Config($array);
90
        $eventDispatcher = new \Venta\Event\EventDispatcher();
91
        $mailer = new Venta\Mail\Mailer($config, $eventDispatcher);
92
        $this->assertEquals(['[email protected]' => null], $mailer->getMessageBuilder()->getFrom());
93
        $this->assertEquals(['[email protected]' => null], $mailer->getMessageBuilder()->getTo());
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function defaultFromToCanBeArrays()
100
    {
101
        $array = [
102
            'mailer' => [
103
                'transport' => 'mail',
104
                'from' => [
105
                    '[email protected]' => 'team',
106
                    '[email protected]',
107
                ],
108
                'to' => [
109
                    '[email protected]' => 'other team',
110
                    '[email protected]',
111
                ],
112
            ],
113
        ];
114
        $config = new \Venta\Config\Config($array);
115
        $eventDispatcher = new \Venta\Event\EventDispatcher();
116
        $mailer = new Venta\Mail\Mailer($config, $eventDispatcher);
117
118
        $this->assertEquals([
119
            '[email protected]' => 'team',
120
            '[email protected]' => null,
121
        ], $mailer->getMessageBuilder()->getFrom());
122
        $this->assertEquals([
123
            '[email protected]' => 'other team',
124
            '[email protected]' => null,
125
        ], $mailer->getMessageBuilder()->getTo());
126
    }
127
128
    /**
129
     * @test
130
     */
131
    public function defaultFromToCanBeRedefined()
132
    {
133
        $array = [
134
            'mailer' => [
135
                'transport' => 'mail',
136
                'from' => [
137
                    '[email protected]' => 'team',
138
                    '[email protected]',
139
                ],
140
                'to' => [
141
                    '[email protected]' => 'other team',
142
                    '[email protected]',
143
                ],
144
            ],
145
        ];
146
        $config = new \Venta\Config\Config($array);
147
        $eventDispatcher = new \Venta\Event\EventDispatcher();
148
        $mailer = new Venta\Mail\Mailer($config, $eventDispatcher);
149
        $message = $mailer->getMessageBuilder()
150
                          ->setTo('[email protected]')
151
                          ->setFrom('[email protected]');
152
153
        $this->assertEquals(['[email protected]' => null], $message->getTo());
154
        $this->assertEquals(['[email protected]' => null], $message->getFrom());
155
    }
156
157
    /**
158
     * @test
159
     * @expectedException \Venta\Mail\Exception\TransportException
160
     */
161
    public function fileSpoolPathIsRequired()
162
    {
163
        $array = [
164
            'mailer' => [
165
                'mail' => [
166
                    'transport' => 'mail',
167
                ],
168
                'spool' => [
169
                    'type' => 'file',
170
                ],
171
            ],
172
        ];
173
174
        $config = new \Venta\Config\Config($array);
175
        $eventDispatcher = new \Venta\Event\EventDispatcher();
176
        $mailer = new \Venta\Mail\Mailer($config, $eventDispatcher);
177
        $this->assertInstanceOf(Swift_Transport_SpoolTransport::class, $mailer->spoolWithTransport()->getTransport());
178
    }
179
180
    /**
181
     * @test
182
     */
183
    public function gmailIsAutoConfigured()
184
    {
185
        $array = [
186
            'mailer' => [
187
                'transport' => 'gmail',
188
                'username' => 'user',
189
                'password' => 'password',
190
            ],
191
        ];
192
        $config = new \Venta\Config\Config($array);
193
        $eventDispatcher = new \Venta\Event\EventDispatcher();
194
        $mailer = new \Venta\Mail\Mailer($config, $eventDispatcher);
195
196
        $this->assertInstanceOf(\Swift_Transport_SmtpAgent::class, $mailer->withTransport()->getTransport());
197
    }
198
199
    /**
200
     * @test
201
     * @expectedException \Venta\Mail\Exception\TransportException
202
     */
203
    public function gmailRequiresserAndPassword()
204
    {
205
        $array = [
206
            'mailer' => [
207
                'mail' => [
208
                    'transport' => 'gmail',
209
                ],
210
            ],
211
        ];
212
213
        $config = new \Venta\Config\Config($array);
214
        $eventDispatcher = new \Venta\Event\EventDispatcher();
215
        new \Venta\Mail\Mailer($config, $eventDispatcher);
216
    }
217
218
    /**
219
     * @test
220
     */
221
    public function mailerCanBeContructed()
222
    {
223
        $config = new \Venta\Config\Config($this->config);
224
        $eventDispatcher = new \Venta\Event\EventDispatcher();
225
226
        $this->assertInstanceOf(Venta\Mail\Mailer::class, new Venta\Mail\Mailer($config, $eventDispatcher));
227
    }
228
229
    /**
230
     * @test
231
     */
232
    public function mailerIsEnabledByDefault()
233
    {
234
        $array = [
235
            'mailer' => [
236
                'transport' => 'mail',
237
            ],
238
        ];
239
        $config = new \Venta\Config\Config($array);
240
        $eventDispatcher = new \Venta\Event\EventDispatcher();
241
        $mailer = new Venta\Mail\Mailer($config, $eventDispatcher);
242
243
        $this->assertFalse($mailer->isDisabled());
244
    }
245
246
    /**
247
     * @test
248
     * @expectedException \Venta\Mail\Exception\TransportException
249
     */
250
    public function mailerNeedsAtLeastOneTransport()
251
    {
252
        $array = [
253
            'mailer' => [
254
                'disable_delivery' => 'false',
255
            ],
256
        ];
257
258
        $config = new \Venta\Config\Config($array);
259
        $eventDispatcher = new \Venta\Event\EventDispatcher();
260
        new Venta\Mail\Mailer($config, $eventDispatcher);
261
    }
262
263
    /**
264
     * @test
265
     */
266
    public function messageBuilderCanBeRetrieved()
267
    {
268
        $message = $this->mailer->getMessageBuilder();
269
270
        $this->assertInstanceOf(\Swift_Message::class, $message);
271
    }
272
273
    /**
274
     * @test
275
     */
276
    public function nullTransportUsedWhenDisabled()
277
    {
278
        $array = [
279
            'mailer' => [
280
                'disable_delivery' => true,
281
            ],
282
        ];
283
        $config = new \Venta\Config\Config($array);
284
        $eventDispatcher = new \Venta\Event\EventDispatcher();
285
        $mailer = new Venta\Mail\Mailer($config, $eventDispatcher);
286
287
        $this->assertTrue($mailer->isDisabled());
288
        $this->assertInstanceOf(\Swift_Transport_NullTransport::class, $mailer->withTransport()->getTransport());
289
    }
290
291
    /**
292
     * @test
293
     * @expectedException \Venta\Mail\Exception\TransportException
294
     */
295
    public function smtpRequiresHost()
296
    {
297
        $array = [
298
            'mailer' => [
299
                'mail' => [
300
                    'transport' => 'smtp',
301
                    'port' => '3333',
302
                ],
303
            ],
304
        ];
305
306
        $config = new \Venta\Config\Config($array);
307
        $eventDispatcher = new \Venta\Event\EventDispatcher();
308
        new \Venta\Mail\Mailer($config, $eventDispatcher);
309
    }
310
311
    /**
312
     * @test
313
     */
314
    public function spoolCanBeExplicitlyDisabled()
315
    {
316
        $array = [
317
            'mailer' => [
318
                'mail' => [
319
                    'transport' => 'mail',
320
                ],
321
                'spool' => false,
322
            ],
323
        ];
324
325
        $config = new \Venta\Config\Config($array);
326
        $eventDispatcher = new \Venta\Event\EventDispatcher();
327
        new \Venta\Mail\Mailer($config, $eventDispatcher);
328
    }
329
330
    /**
331
     * @ignore
332
     */
333
    public function spoolCanUseMemoryStorage()
334
    {
335
        $array = [
336
            'mailer' => [
337
                'mail' => [
338
                    'transport' => 'mail',
339
                ],
340
                'spool' => [
341
                    'type' => 'memory',
342
                ],
343
            ],
344
        ];
345
346
        $config = new \Venta\Config\Config($array);
347
        $eventDispatcher = new \Venta\Event\EventDispatcher();
348
        $mailer = new \Venta\Mail\Mailer($config, $eventDispatcher);
349
        $this->assertInstanceOf(Swift_Transport_SpoolTransport::class, $mailer->spoolWithTransport()->getTransport());
350
    }
351
352
    /**
353
     * @ignore
354
     */
355
    public function spoolRealTransportIsSet()
356
    {
357
        $this->assertInstanceOf(Swift_Transport_SpoolTransport::class,
358
            $this->mailer->spoolWithTransport()->getTransport());
359
    }
360
361
    /**
362
     * @test
363
     */
364
    public function spoolTransportCanBeDefinedInConfig()
365
    {
366
        $this->assertInstanceOf(\Swift_Transport_SpoolTransport::class, $this->mailer->getSpoolTransport());
367
    }
368
369
    /**
370
     * @test
371
     * @expectedException \Venta\Mail\Exception\TransportException
372
     */
373
    public function undefiendTransportThrowsException()
374
    {
375
        $array = [
376
            'mailer' => [
377
                'smtp' => [
378
                    'host' => 'localhost',
379
                ],
380
            ],
381
        ];
382
383
        $config = new \Venta\Config\Config($array);
384
        $eventDispatcher = new \Venta\Event\EventDispatcher();
385
        new \Venta\Mail\Mailer($config, $eventDispatcher);
386
    }
387
388
    /**
389
     * @test
390
     * @expectedException \Exception
391
     */
392
    public function undefinedMailerConfigThrowsException()
393
    {
394
        $array = [
395
            'db_host' => 'localhost',
396
        ];
397
        $config = new \Venta\Config\Config($array);
398
        $eventDispatcher = new \Venta\Event\EventDispatcher();
399
        new \Venta\Mail\Mailer($config, $eventDispatcher);
400
    }
401
402
    /**
403
     * @test
404
     * @expectedException \Venta\Mail\Exception\UnknownTransportException
405
     */
406
    public function unknownSpoolTypeProducesException()
407
    {
408
        $array = [
409
            'mailer' => [
410
                'mail' => [
411
                    'transport' => 'mail',
412
                ],
413
                'spool' => [
414
                    'type' => 'unknown',
415
                ],
416
            ],
417
        ];
418
419
        $config = new \Venta\Config\Config($array);
420
        $eventDispatcher = new \Venta\Event\EventDispatcher();
421
        $mailer = new \Venta\Mail\Mailer($config, $eventDispatcher);
422
        $this->assertInstanceOf(Swift_Transport_SpoolTransport::class, $mailer->spoolWithTransport()->getTransport());
423
    }
424
425
    /**
426
     * @test
427
     * @expectedException \Venta\Mail\Exception\UnknownTransportException
428
     */
429
    public function unknownTransprotTypeThrowsException()
430
    {
431
        $array = [
432
            'mailer' => [
433
                'mail' => [
434
                    'transport' => 'unknown',
435
                ],
436
            ],
437
        ];
438
439
        $config = new \Venta\Config\Config($array);
440
        $eventDispatcher = new \Venta\Event\EventDispatcher();
441
        new \Venta\Mail\Mailer($config, $eventDispatcher);
442
    }
443
}