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

BaseMailerTest::testComposeWithView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 30
rs 9.568
1
<?php
2
3
namespace Yiisoft\Mailer\Tests;
4
5
use InvalidArgumentException;
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Psr\Log\LoggerInterface;
8
use Yiisoft\Mailer\Composer;
9
use Yiisoft\Mailer\MessageFactoryInterface;
10
use Yiisoft\Mailer\MessageInterface;
11
use Yiisoft\Mailer\Event\BeforeSend;
12
13
class BaseMailerTest extends TestCase
14
{
15
    public function testCompose(): void
16
    {
17
        $mailer = $this->getMailer();
18
        $message = $mailer->compose();
19
        $this->assertInstanceOf(MessageInterface::class, $message);
20
    }
21
22
    public function testComposeWithView(): void
23
    {
24
        $mailer = $this->getMailer();
25
        $viewPath = $this->getTestFilePath();
26
27
        $composer = $mailer->getComposer();
28
        $composer->setViewPath($viewPath);
29
        $composer->setHtmlLayout('');
30
        $composer->setTextayout('');
31
32
        $htmlViewName = 'test_html_view';
33
        $htmlViewFileName = $viewPath . DIRECTORY_SEPARATOR . $htmlViewName . '.php';
34
        $htmlViewFileContent = 'HTML <b>view file</b> content';
35
        $this->saveFile($htmlViewFileName, $htmlViewFileContent);
36
37
        $textViewName = 'test_text_view';
38
        $textViewFileName = $viewPath . DIRECTORY_SEPARATOR . $textViewName . '.php';
39
        $textViewFileContent = 'Plain text view file content';
40
        $this->saveFile($textViewFileName, $textViewFileContent);
41
42
        $message = $mailer->compose([
43
            'html' => $htmlViewName,
44
            'text' => $textViewName,
45
        ]);
46
        $this->assertEquals($htmlViewFileContent, $message->getHtmlBody(), 'Unable to render html!');
47
        $this->assertEquals($textViewFileContent, $message->getTextBody(), 'Unable to render text!');
48
49
        $message = $mailer->compose($htmlViewName);
50
        $this->assertEquals($htmlViewFileContent, $message->getHtmlBody(), 'Unable to render html by direct view!');
51
        $this->assertEquals(strip_tags($htmlViewFileContent), $message->getTextBody(), 'Unable to render text by direct view!');
52
    }
53
54
    /**
55
     * @dataProvider messagesProvider
56
     */
57
    public function testSendMultiple(array $messages): void
58
    {
59
        $mailer = $this->getMailer();
60
        $this->assertCount(0, $mailer->sendMultiple($messages));
61
    }
62
63
    public function messagesProvider(): array
64
    {
65
        return [
66
            [[]],
67
            [[$this->createMessage('foo')]],
68
            [[$this->createMessage('bar'), $this->createMessage('baz')]],
69
        ];
70
    }
71
72
    public function testSendMultipleExceptions(): void
73
    {
74
        $mailer = $this->getMailer();
75
        $messages = [$this->createMessage(''), $this->createMessage()];
76
        /** @var MessageInterface[] $failed */
77
        $failed = $mailer->sendMultiple($messages);
78
        $this->assertCount(1, $failed);
79
        $this->assertEquals($messages[0], $failed[0]);
80
        $this->assertEquals(new InvalidArgumentException("Message's subject is required"), $failed[0]->getError());
81
    }
82
83
    public function testBeforeSend(): void
84
    {
85
        $message = $this->createMock(MessageInterface::class);
86
        $event = new BeforeSend($message);
87
        $messageFactory = $this->createMock(MessageFactoryInterface::class);
88
        $composer = $this->createMock(Composer::class);
89
        $logger = $this->createMock(LoggerInterface::class);
90
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
91
        $eventDispatcher->method('dispatch')->willReturn($event);
92
        $mailer = new TestMailer($messageFactory, $composer, $eventDispatcher, $logger, '');
93
94
        $this->assertTrue($mailer->beforeSend($message));
95
        $event->stopPropagation();
96
        $this->assertFalse($mailer->beforeSend($message));
97
        $mailer->send($message);
98
    }
99
}
100