Passed
Push — master ( 2823db...e46f60 )
by Alexander
10:02
created

FileMailerTest::testSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.8333
1
<?php
2
3
namespace Yiisoft\Mailer\Tests;
4
5
class FileMailerTest extends TestCase
6
{
7
    public function testSend(): void
8
    {
9
        $mailer = $this->getMailer();
10
11
        $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mails';
12
        $mailer->setPath($path);
13
        $this->assertSame($path, $mailer->getPath());
14
15
        $message = $mailer->compose()
16
            ->setTo('[email protected]')
17
            ->setFrom('[email protected]')
18
            ->setSubject('test subject')
19
            ->setTextBody('text body' . microtime(true));
20
        $mailer->send($message);
21
        $file = $path . DIRECTORY_SEPARATOR . $mailer->lastFilename;
22
        $this->assertTrue(is_file($file));
23
        $this->assertEquals($message->toString(), file_get_contents($file));
24
    }
25
26
    public function testFilenameCallback(): void
27
    {
28
        $mailer = $this->getMailer();
29
30
        $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mails';
31
        $mailer->setPath($path);
32
        $this->assertSame($path, $mailer->getPath());
33
34
        $filename = date('Ymd') . DIRECTORY_SEPARATOR . md5(uniqid()) . '.txt';
35
        $mailer->setFilenameCallback(function () use ($filename) {
36
            return $filename;
37
        });
38
        $message = $mailer->compose()
39
            ->setTo([
40
                '[email protected]',
41
                '[email protected]',
42
            ])
43
            ->setFrom('[email protected]')
44
            ->setSubject('test subject')
45
            ->setTextBody('text body' . microtime(true));
46
        $mailer->send($message);
47
        $file = $path . DIRECTORY_SEPARATOR . $filename;
48
        $this->assertTrue(is_file($file));
49
        $this->assertEquals($message->toString(), file_get_contents($file));
50
    }
51
}
52