Passed
Push — 1 ( c35c92...36eaee )
by Roman
02:04
created

EmogrifierPluginTest::createMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 3
dl 0
loc 14
rs 9.9
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 testCompleteHtml()
15
    {
16
        $html = '<html><head><title>Test</title></head><body>42</body></html>';
17
18
        $plugin = new EmogrifierPlugin();
19
20
        $message = $this->createMessage($html);
21
        $message->shouldReceive('setBody')
22
            ->once()
23
            ->withArgs(function ($code) {
24
                return strpos($code, '<html>') !== false
25
                    && strpos($code, '<title>Test</title>') !== false
26
                    && strpos($code, '<body>42</body>') !== false;
27
            });
28
29
        $evt = $this->createSendEvent($message);
30
31
        $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

31
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $evt);
Loading history...
32
        $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

32
        $plugin->sendPerformed(/** @scrutinizer ignore-type */ $evt);
Loading history...
33
    }
34
35
    public function testBodyOnly()
36
    {
37
        $html = '<style>.test { color: red; }</style><p class="test">Hello World</p>';
38
39
        $plugin = new EmogrifierPlugin();
40
41
        $message = $this->createMessage($html);
42
        $message->shouldReceive('setBody')
43
            ->once()
44
            ->withArgs(function ($code) {
45
                return strpos($code, '<p class="test" style="color: red;">Hello World</p>') !== false;
46
            });
47
48
        $evt = $this->createSendEvent($message);
49
50
        $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

50
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $evt);
Loading history...
51
        $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

51
        $plugin->sendPerformed(/** @scrutinizer ignore-type */ $evt);
Loading history...
52
    }
53
54
    public function testWithMessagePart()
55
    {
56
        $htmlBody = '<style>p { color: red; }</style><p>MessageBody</p>';
57
        $htmlPart = '<p>MessagePart</p>';
58
59
        $part1 = $this->createMessagePart($htmlPart);
60
        $part2 = $this->createMessagePart('Plain text', 'text/plain');
61
62
        $message = $this->createMessage($htmlBody, 'text/html', [$part1, $part2]);
63
64
        $message->shouldReceive('setBody')
65
            ->once()
66
            ->withArgs(function ($code) {
67
                return strpos($code, '<p style="color: red;">MessageBody</p>') !== false;
68
            });
69
70
        $part1->shouldReceive('setBody')
71
            ->once()
72
            ->withArgs(function ($code) {
73
                return strpos($code, '<p>MessagePart</p>') !== false;
74
            });
75
76
        $part2->shouldNotHaveReceived('setBody');
77
78
        $event = $this->createSendEvent($message);
79
80
        $plugin = new EmogrifierPlugin();
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 testWithCss()
86
    {
87
        $css = 'div { color: green; } p { color: red; }';
88
        $htmlBody = '<div>MessageBody</div>';
89
        $htmlPart = '<p>MessagePart</p>';
90
91
        $part = $this->createMessagePart($htmlPart);
92
        $message = $this->createMessage($htmlBody, 'text/html', [$part]);
93
94
        $message->shouldReceive('setBody')
95
            ->once()
96
            ->withArgs(function ($code) {
97
                return strpos($code, '<div style="color: green;">MessageBody</div>') !== false;
98
            });
99
100
        $part->shouldReceive('setBody')
101
            ->once()
102
            ->withArgs(function ($code) {
103
                return strpos($code, '<p style="color: red;">MessagePart</p>') !== false;
104
            });
105
106
        $event = $this->createSendEvent($message);
107
108
        $plugin = new EmogrifierPlugin();
109
        $this->assertNull($plugin->getCss());
110
        $plugin->setCss($css);
111
        $this->assertEquals($css, $plugin->getCss());
112
113
        $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

113
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $event);
Loading history...
114
    }
115
116
    public function testEmptyBodies()
117
    {
118
        $message = $this->createMessage('', 'text/html', [
119
            $part = $this->createMessagePart('')
120
        ]);
121
122
        $part->shouldNotHaveReceived('setBody');
123
        $message->shouldNotHaveReceived('setBody');
124
125
        $event = $this->createSendEvent($message);
126
127
        $plugin = new EmogrifierPlugin();
128
        $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

128
        $plugin->beforeSendPerformed(/** @scrutinizer ignore-type */ $event);
Loading history...
129
    }
130
131
    public function testSendPerformed()
132
    {
133
        $event = $this->createSendEvent($msg = $this->createMessage(''));
134
        $msg->shouldNotReceive('getBody', 'getContentType', 'getChildren');
135
136
        $plugin = new EmogrifierPlugin();
137
        $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

137
        $plugin->sendPerformed(/** @scrutinizer ignore-type */ $event);
Loading history...
138
    }
139
140
    protected function createMessage($body = '', $mimeType = 'text/html', $parts = [])
141
    {
142
        $message = $this->getMockery('Swift_Mime_SimpleMessage')->shouldIgnoreMissing();
143
        $message->shouldReceive('getContentType')
144
            ->zeroOrMoreTimes()
145
            ->andReturn($mimeType);
146
        $message->shouldReceive('getBody')
147
            ->zeroOrMoreTimes()
148
            ->andReturn($body);
149
        $message->shouldReceive('getChildren')
150
            ->zeroOrMoreTimes()
151
            ->andReturn($parts);
152
153
        return $message;
154
    }
155
156
    protected function createMessagePart($body = '', $mimeType = 'text/html')
157
    {
158
        $part = $this->getMockery('Swift_Mime_SimpleMimeEntity')->shouldIgnoreMissing();
159
        $part->shouldReceive('getContentType')
160
            ->zeroOrMoreTimes()
161
            ->andReturn($mimeType);
162
        $part->shouldReceive('getBody')
163
            ->zeroOrMoreTimes()
164
            ->andReturn($body);
165
        return $part;
166
    }
167
168
    protected function createSendEvent($message)
169
    {
170
        $evt = $this->getMockery('Swift_Events_SendEvent')->shouldIgnoreMissing();
171
        $evt->shouldReceive('getMessage')
172
            ->zeroOrMoreTimes()
173
            ->andReturn($message);
174
175
        return $evt;
176
    }
177
178
    protected function getMockery($class)
179
    {
180
        return \Mockery::mock($class);
181
    }
182
}
183