Passed
Branch 1 (deb1ae)
by Roman
02:39
created

EmogrifierPluginTest::createMessagePart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Bummzack\SwiftMailer\EmogrifyPlugin\Tests;
5
6
use Bummzack\SwiftMailer\EmogrifyPlugin\EmogrifierPlugin;
7
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
8
use PHPUnit\Framework\TestCase;
9
10
class EmogrifierPluginTest extends TestCase
11
{
12
    use MockeryPHPUnitIntegration;
13
14
    public function testBodyOnly()
15
    {
16
        $html = '<style>.test { color: red; }</style><p class="test">Hello World</p>';
17
18
        $plugin = new EmogrifierPlugin();
19
20
        $message = $this->createMessage($html);
21
        $message->shouldReceive('setBody')
22
            ->once()
23
            ->with('<p class="test" style="color: red;">Hello World</p>');
24
25
        $evt = $this->createSendEvent($message);
26
27
        $plugin->beforeSendPerformed($evt);
0 ignored issues
show
Bug introduced by
$evt of type Mockery\Mock is incompatible with the type Swift_Events_SendEvent expected by parameter $evt of Bummzack\SwiftMailer\Emo...::beforeSendPerformed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $evt);
Loading history...
28
        $plugin->sendPerformed($evt);
0 ignored issues
show
Bug introduced by
$evt of type Mockery\Mock is incompatible with the type Swift_Events_SendEvent expected by parameter $evt of Bummzack\SwiftMailer\Emo...Plugin::sendPerformed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
        $plugin->sendPerformed(/** @scrutinizer ignore-type */ $evt);
Loading history...
29
    }
30
31
    public function testWithMessagePart()
32
    {
33
        $htmlBody = '<style>p { color: red; }</style><p>MessageBody</p>';
34
        $htmlPart = '<p>MessagePart</p>';
35
36
        $part1 = $this->createMessagePart($htmlPart);
37
        $part2 = $this->createMessagePart('Plain text', 'text/plain');
38
39
        $message = $this->createMessage($htmlBody, 'text/html', [$part1, $part2]);
40
41
        $message->shouldReceive('setBody')
42
            ->once()
43
            ->with('<p style="color: red;">MessageBody</p>');
44
45
        $part1->shouldReceive('setBody')
46
            ->once()
47
            ->with('<p>MessagePart</p>');
48
49
        $part2->shouldNotHaveReceived('setBody');
50
51
        $event = $this->createSendEvent($message);
52
53
        $plugin = new EmogrifierPlugin();
54
55
        $plugin->beforeSendPerformed($event);
0 ignored issues
show
Bug introduced by
$event of type Mockery\Mock is incompatible with the type Swift_Events_SendEvent expected by parameter $evt of Bummzack\SwiftMailer\Emo...::beforeSendPerformed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $event);
Loading history...
56
    }
57
58
    public function testWithCss()
59
    {
60
        $css = 'div { color: green; } p { color: red; }';
61
        $htmlBody = '<div>MessageBody</div>';
62
        $htmlPart = '<p>MessagePart</p>';
63
64
        $part = $this->createMessagePart($htmlPart);
65
        $message = $this->createMessage($htmlBody, 'text/html', [$part]);
66
67
        $message->shouldReceive('setBody')
68
            ->once()
69
            ->with('<div style="color: green;">MessageBody</div>');
70
71
        $part->shouldReceive('setBody')
72
            ->once()
73
            ->with('<p style="color: red;">MessagePart</p>');
74
75
        $event = $this->createSendEvent($message);
76
77
        $plugin = new EmogrifierPlugin();
78
        $this->assertNull($plugin->getCss());
79
        $plugin->setCss($css);
80
        $this->assertEquals($css, $plugin->getCss());
81
82
        $plugin->beforeSendPerformed($event);
0 ignored issues
show
Bug introduced by
$event of type Mockery\Mock is incompatible with the type Swift_Events_SendEvent expected by parameter $evt of Bummzack\SwiftMailer\Emo...::beforeSendPerformed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $event);
Loading history...
83
    }
84
85
    public function testEmptyBodies()
86
    {
87
        $message = $this->createMessage('', 'text/html', [
88
            $part = $this->createMessagePart('')
89
        ]);
90
91
        $part->shouldNotHaveReceived('setBody');
92
        $message->shouldNotHaveReceived('setBody');
93
94
        $event = $this->createSendEvent($message);
95
96
        $plugin = new EmogrifierPlugin();
97
        $plugin->beforeSendPerformed($event);
0 ignored issues
show
Bug introduced by
$event of type Mockery\Mock is incompatible with the type Swift_Events_SendEvent expected by parameter $evt of Bummzack\SwiftMailer\Emo...::beforeSendPerformed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $event);
Loading history...
98
    }
99
100
    public function testSendPerformed()
101
    {
102
        $event = $this->createSendEvent($msg = $this->createMessage(''));
103
        $msg->shouldNotReceive('getBody', 'getContentType', 'getChildren');
104
105
        $plugin = new EmogrifierPlugin();
106
        $plugin->sendPerformed($event);
0 ignored issues
show
Bug introduced by
$event of type Mockery\Mock is incompatible with the type Swift_Events_SendEvent expected by parameter $evt of Bummzack\SwiftMailer\Emo...Plugin::sendPerformed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
        $plugin->sendPerformed(/** @scrutinizer ignore-type */ $event);
Loading history...
107
    }
108
109
    protected function createMessage($body = '', $mimeType = 'text/html', $parts = [])
110
    {
111
        $message = $this->getMockery('Swift_Mime_SimpleMessage')->shouldIgnoreMissing();
112
        $message->shouldReceive('getContentType')
113
            ->zeroOrMoreTimes()
114
            ->andReturn($mimeType);
115
        $message->shouldReceive('getBody')
116
            ->zeroOrMoreTimes()
117
            ->andReturn($body);
118
        $message->shouldReceive('getChildren')
119
            ->zeroOrMoreTimes()
120
            ->andReturn($parts);
121
122
        return $message;
123
    }
124
125
    protected function createMessagePart($body = '', $mimeType = 'text/html')
126
    {
127
        $part = $this->getMockery('Swift_Mime_SimpleMimeEntity')->shouldIgnoreMissing();
128
        $part->shouldReceive('getContentType')
129
            ->zeroOrMoreTimes()
130
            ->andReturn($mimeType);
131
        $part->shouldReceive('getBody')
132
            ->zeroOrMoreTimes()
133
            ->andReturn($body);
134
        return $part;
135
    }
136
137
    protected function createSendEvent($message)
138
    {
139
        $evt = $this->getMockery('Swift_Events_SendEvent')->shouldIgnoreMissing();
140
        $evt->shouldReceive('getMessage')
141
            ->zeroOrMoreTimes()
142
            ->andReturn($message);
143
144
        return $evt;
145
    }
146
147
    protected function getMockery($class)
148
    {
149
        return \Mockery::mock($class);
150
    }
151
}
152