Completed
Push — master ( e6ae53...3ea501 )
by Sam
10:01 queued 45s
created

EmailTest::testIsEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Control\Tests\Email;
4
5
use PHPUnit_Framework_MockObject_MockObject;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Control\Email\SwiftMailer;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\ORM\FieldType\DBDatetime;
10
use SilverStripe\Security\Member;
11
use Swift_Attachment;
12
use Swift_Mailer;
13
use Swift_Message;
14
use Swift_NullTransport;
15
use Swift_RfcComplianceException;
16
17
class EmailTest extends SapphireTest
18
{
19
20
    public function testAddAttachment()
21
    {
22
        $email = new Email();
23
24
        $email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain');
25
26
        $children = $email->getSwiftMessage()->getChildren();
27
        $this->assertCount(1, $children);
28
29
        /** @var Swift_Attachment $child */
30
        $child = reset($children);
31
32
        $this->assertInstanceOf(Swift_Attachment::class, $child);
33
        $this->assertEquals('text/plain', $child->getContentType());
34
        $this->assertEquals('attachment.txt', $child->getFilename());
35
    }
36
37
    public function testAddAttachmentFromData()
38
    {
39
        $email = new Email();
40
41
        $email->addAttachmentFromData('foo bar', 'foo.txt', 'text/plain');
42
        $children = $email->getSwiftMessage()->getChildren();
43
44
        $this->assertCount(1, $children);
45
46
        /** @var Swift_Attachment $child */
47
        $child = reset($children);
48
49
        $this->assertInstanceOf(Swift_Attachment::class, $child);
50
        $this->assertEquals('foo bar', $child->getBody());
51
        $this->assertEquals('text/plain', $child->getContentType());
52
        $this->assertEquals('foo.txt', $child->getFilename());
53
    }
54
55
    public function testValidEmailAddress()
56
    {
57
        $validEmails = array('[email protected]', '[email protected]');
58
        $invalidEmails = array('foo.bar@', '@example.com', 'foo@');
59
60
        foreach ($validEmails as $email) {
61
            $this->assertTrue(Email::is_valid_address($email));
62
        }
63
64
        foreach ($invalidEmails as $email) {
65
            $this->assertFalse(Email::is_valid_address($email));
66
        }
67
    }
68
69
    public function testObfuscate()
70
    {
71
        $emailAddress = '[email protected]';
72
73
        $direction = Email::obfuscate($emailAddress, 'direction');
74
        $visible = Email::obfuscate($emailAddress, 'visible');
75
        $hex = Email::obfuscate($emailAddress, 'hex');
76
77
        $this->assertEquals('<span class="codedirection">moc.elpmaxe@1-tset</span>', $direction);
78
        $this->assertEquals('test [dash] 1 [at] example [dot] com', $visible);
79
        $this->assertEquals(
80
            '&#x74;&#x65;&#x73;&#x74;&#x2d;&#x31;&#x40;&#x65;&#x78;&#x61;&#x6d;&#x70;&#x6c;&#x65;&#x2e;&#x63;&#x6f;&#x6d;',
81
            $hex
82
        );
83
    }
84
85
    public function testSendPlain()
86
    {
87
        /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
88
        $email = $this->getMockBuilder(Email::class)
89
            ->enableProxyingToOriginalMethods()
90
            ->disableOriginalConstructor()
91
            ->setConstructorArgs(array(
92
                '[email protected]',
93
                '[email protected]',
94
                'Test send plain',
95
                'Testing Email->sendPlain()',
96
                '[email protected]',
97
                '[email protected]',
98
            ))
99
            ->getMock();
100
101
        // email should not call render if a body is supplied
102
        $email->expects($this->never())->method('render');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in SilverStripe\Control\Email\Email.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
103
104
        $email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain');
0 ignored issues
show
Bug introduced by
The method addAttachment does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
105
        $successful = $email->sendPlain();
0 ignored issues
show
Bug introduced by
The method sendPlain does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
107
        $this->assertTrue($successful);
108
        $this->assertEmpty($email->getFailedRecipients());
0 ignored issues
show
Bug introduced by
The method getFailedRecipients does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
109
110
        $sentMail = $this->mailer->findEmail('[email protected]');
111
112
        $this->assertTrue(is_array($sentMail));
113
114
        $this->assertEquals('[email protected]', $sentMail['To']);
115
        $this->assertEquals('[email protected]', $sentMail['From']);
116
        $this->assertEquals('Test send plain', $sentMail['Subject']);
117
        $this->assertEquals('Testing Email->sendPlain()', $sentMail['Content']);
118
119
        $this->assertCount(1, $sentMail['AttachedFiles']);
120
        $child = reset($sentMail['AttachedFiles']);
121
        $this->assertEquals('text/plain', $child['mimetype']);
122
        $this->assertEquals('attachment.txt', $child['filename']);
123
        $this->assertEquals('Hello, I\'m a text document.', $child['contents']);
124
    }
125
126
    public function testSend()
127
    {
128
        /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
129
        $email = $this->getMockBuilder(Email::class)
130
            ->enableProxyingToOriginalMethods()
131
            ->disableOriginalConstructor()
132
            ->setConstructorArgs(array(
133
                '[email protected]',
134
                '[email protected]',
135
                'Test send HTML',
136
                'Testing Email->send()',
137
                '[email protected]',
138
                '[email protected]',
139
            ))
140
            ->getMock();
141
142
        // email should not call render if a body is supplied
143
        $email->expects($this->never())->method('render');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in SilverStripe\Control\Email\Email.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
144
145
        $email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain');
0 ignored issues
show
Bug introduced by
The method addAttachment does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
146
        $successful = $email->send();
0 ignored issues
show
Bug introduced by
The method send does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
147
148
        $this->assertTrue($successful);
149
        $this->assertEmpty($email->getFailedRecipients());
0 ignored issues
show
Bug introduced by
The method getFailedRecipients does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
150
151
        $sentMail = $this->mailer->findEmail('[email protected]');
152
153
        $this->assertTrue(is_array($sentMail));
154
155
        $this->assertEquals('[email protected]', $sentMail['To']);
156
        $this->assertEquals('[email protected]', $sentMail['From']);
157
        $this->assertEquals('Test send HTML', $sentMail['Subject']);
158
        $this->assertEquals('Testing Email->send()', $sentMail['Content']);
159
160
        $this->assertCount(1, $sentMail['AttachedFiles']);
161
        $child = reset($sentMail['AttachedFiles']);
162
        $this->assertEquals('text/plain', $child['mimetype']);
163
        $this->assertEquals('attachment.txt', $child['filename']);
164
        $this->assertEquals('Hello, I\'m a text document.', $child['contents']);
165
    }
166
167
    public function testRenderedSend()
168
    {
169
        /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
170
        $email = $this->getMockBuilder(Email::class)
171
            ->enableProxyingToOriginalMethods()
172
            ->disableOriginalConstructor()
173
            ->setConstructorArgs(array(
174
              '[email protected]',
175
              '[email protected]',
176
            ))
177
            ->getMock();
178
        $email->setData(array(
0 ignored issues
show
Bug introduced by
The method setData does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
179
            'EmailContent' => 'test',
180
        ));
181
        $this->assertFalse($email->hasPlainPart());
0 ignored issues
show
Bug introduced by
The method hasPlainPart does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
182
        $this->assertEmpty($email->getBody());
0 ignored issues
show
Bug introduced by
The method getBody does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
183
        // these seem to fail for some reason :/
184
        //$email->expects($this->once())->method('render');
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
185
        //$email->expects($this->once())->method('generatePlainPartFromBody');
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
        $email->send();
0 ignored issues
show
Bug introduced by
The method send does only exist in SilverStripe\Control\Email\Email, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
187
        $this->assertTrue($email->hasPlainPart());
188
        $this->assertNotEmpty($email->getBody());
189
    }
190
191
    public function testConsturctor()
192
    {
193
        $email = new Email(
194
            '[email protected]',
195
            '[email protected]',
196
            'subject',
197
            'body',
198
            '[email protected]',
199
            '[email protected]',
200
            '[email protected]'
201
        );
202
203
        $this->assertCount(1, $email->getFrom());
204
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
205
        $this->assertCount(1, $email->getTo());
206
        $this->assertContains('[email protected]', array_keys($email->getTo()));
207
        $this->assertEquals('subject', $email->getSubject());
208
        $this->assertEquals('body', $email->getBody());
209
        $this->assertCount(1, $email->getCC());
210
        $this->assertContains('[email protected]', array_keys($email->getCC()));
211
        $this->assertCount(1, $email->getBCC());
212
        $this->assertContains('[email protected]', array_keys($email->getBCC()));
213
        $this->assertEquals('[email protected]', $email->getReturnPath());
214
    }
215
216
    public function testGetSwiftMessage()
217
    {
218
        $email = new Email(
219
            '[email protected]',
220
            '[email protected]',
221
            'subject',
222
            'body',
223
            '[email protected]',
224
            '[email protected]',
225
            '[email protected]'
226
        );
227
        $swiftMessage = $email->getSwiftMessage();
228
229
        $this->assertInstanceOf(Swift_Message::class, $swiftMessage);
230
231
        $this->assertCount(1, $swiftMessage->getFrom());
232
        $this->assertContains('[email protected]', array_keys($swiftMessage->getFrom()));
233
        $this->assertCount(1, $swiftMessage->getTo());
234
        $this->assertContains('[email protected]', array_keys($swiftMessage->getTo()));
235
        $this->assertEquals('subject', $swiftMessage->getSubject());
236
        $this->assertEquals('body', $swiftMessage->getBody());
237
        $this->assertCount(1, $swiftMessage->getCC());
238
        $this->assertContains('[email protected]', array_keys($swiftMessage->getCc()));
239
        $this->assertCount(1, $swiftMessage->getBCC());
240
        $this->assertContains('[email protected]', array_keys($swiftMessage->getBcc()));
241
        $this->assertEquals('[email protected]', $swiftMessage->getReturnPath());
242
    }
243
244
    public function testSetSwiftMessage()
245
    {
246
        Email::config()->update('admin_email', '[email protected]');
247
        DBDatetime::set_mock_now('2017-01-01 07:00:00');
248
        $email = new Email();
249
        $swiftMessage = new Swift_Message();
250
        $email->setSwiftMessage($swiftMessage);
251
        $this->assertCount(1, $email->getFrom());
252
        $this->assertContains('[email protected]', array_keys($swiftMessage->getFrom()));
253
        $this->assertEquals(strtotime('2017-01-01 07:00:00'), $swiftMessage->getDate());
254
        $this->assertEquals($swiftMessage, $email->getSwiftMessage());
255
256
        // check from field is retained
257
        $swiftMessage = new Swift_Message();
258
        $swiftMessage->setFrom('[email protected]');
259
        $email->setSwiftMessage($swiftMessage);
260
        $this->assertCount(1, $email->getFrom());
261
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
262
    }
263
264
    public function testAdminEmailApplied()
265
    {
266
        Email::config()->update('admin_email', '[email protected]');
267
        $email = new Email();
268
269
        $this->assertCount(1, $email->getFrom());
270
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
271
    }
272
273
    public function testGetFrom()
274
    {
275
        $email = new Email('[email protected]');
276
        $this->assertCount(1, $email->getFrom());
277
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
278
    }
279
280
    public function testSetFrom()
281
    {
282
        $email = new Email('[email protected]');
283
        $this->assertCount(1, $email->getFrom());
284
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
285
        $email->setFrom('[email protected]');
286
        $this->assertCount(1, $email->getFrom());
287
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
288
    }
289
290
    public function testAddFrom()
291
    {
292
        $email = new Email('[email protected]');
293
        $this->assertCount(1, $email->getFrom());
294
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
295
        $email->addFrom('[email protected]');
296
        $this->assertCount(2, $email->getFrom());
297
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
298
        $this->assertContains('[email protected]', array_keys($email->getFrom()));
299
    }
300
301
    public function testSetGetSender()
302
    {
303
        $email = new Email();
304
        $this->assertEmpty($email->getSender());
305
        $email->setSender('[email protected]', 'Silver Stripe');
306
        $this->assertEquals(array('[email protected]' => 'Silver Stripe'), $email->getSender());
307
    }
308
309
    public function testSetGetReturnPath()
310
    {
311
        $email = new Email();
312
        $this->assertEmpty($email->getReturnPath());
313
        $email->setReturnPath('[email protected]');
314
        $this->assertEquals('[email protected]', $email->getReturnPath());
315
    }
316
317
    public function testSetGetTo()
318
    {
319
        $email = new Email('[email protected]', '[email protected]');
320
        $this->assertCount(1, $email->getTo());
321
        $this->assertContains('[email protected]', array_keys($email->getTo()));
322
        $email->setTo('[email protected]', 'Silver Stripe');
323
        $this->assertEquals(array('[email protected]' => 'Silver Stripe'), $email->getTo());
324
    }
325
326
    public function testAddTo()
327
    {
328
        $email = new Email('[email protected]', '[email protected]');
329
        $this->assertCount(1, $email->getTo());
330
        $this->assertContains('[email protected]', array_keys($email->getTo()));
331
        $email->addTo('[email protected]');
332
        $this->assertCount(2, $email->getTo());
333
        $this->assertContains('[email protected]', array_keys($email->getTo()));
334
        $this->assertContains('[email protected]', array_keys($email->getTo()));
335
    }
336
337
    public function testSetGetCC()
338
    {
339
        $email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]');
340
        $this->assertCount(1, $email->getCC());
341
        $this->assertContains('[email protected]', array_keys($email->getCC()));
342
        $email->setCC('[email protected]', 'Silver Stripe');
343
        $this->assertEquals(array('[email protected]' => 'Silver Stripe'), $email->getCC());
344
    }
345
346
    public function testAddCC()
347
    {
348
        $email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]');
349
        $this->assertCount(1, $email->getCC());
350
        $this->assertContains('[email protected]', array_keys($email->getCC()));
351
        $email->addCC('[email protected]', 'Silver Stripe');
352
        $this->assertCount(2, $email->getCC());
353
        $this->assertContains('[email protected]', array_keys($email->getCC()));
354
        $this->assertContains('[email protected]', array_keys($email->getCC()));
355
    }
356
357
    public function testSetGetBCC()
358
    {
359
        $email = new Email(
360
            '[email protected]',
361
            '[email protected]',
362
            'subject',
363
            'body',
364
            '[email protected]',
365
            '[email protected]'
366
        );
367
        $this->assertCount(1, $email->getBCC());
368
        $this->assertContains('[email protected]', array_keys($email->getBCC()));
369
        $email->setBCC('[email protected]', 'Silver Stripe');
370
        $this->assertEquals(array('[email protected]' => 'Silver Stripe'), $email->getBCC());
371
    }
372
373
    public function testAddBCC()
374
    {
375
        $email = new Email(
376
            '[email protected]',
377
            '[email protected]',
378
            'subject',
379
            'body',
380
            '[email protected]',
381
            '[email protected]'
382
        );
383
        $this->assertCount(1, $email->getBCC());
384
        $this->assertContains('[email protected]', array_keys($email->getBCC()));
385
        $email->addBCC('[email protected]', 'Silver Stripe');
386
        $this->assertCount(2, $email->getBCC());
387
        $this->assertContains('[email protected]', array_keys($email->getBCC()));
388
        $this->assertContains('[email protected]', array_keys($email->getBCC()));
389
    }
390
391
    public function testReplyTo()
392
    {
393
        $email = new Email();
394
        $this->assertEmpty($email->getReplyTo());
395
        $email->setReplyTo('[email protected]', 'Silver Stripe');
396
        $this->assertEquals(array('[email protected]' => 'Silver Stripe'), $email->getReplyTo());
397
        $email->addReplyTo('[email protected]');
398
        $this->assertCount(2, $email->getReplyTo());
399
        $this->assertContains('[email protected]', array_keys($email->getReplyTo()));
400
        $this->assertContains('[email protected]', array_keys($email->getReplyTo()));
401
    }
402
403
    public function testSubject()
404
    {
405
        $email = new Email('[email protected]', '[email protected]', 'subject');
406
        $this->assertEquals('subject', $email->getSubject());
407
        $email->setSubject('new subject');
408
        $this->assertEquals('new subject', $email->getSubject());
409
    }
410
411
    public function testPriority()
412
    {
413
        $email = new Email();
414
        $this->assertEquals(3, $email->getPriority());
415
        $email->setPriority(5);
416
        $this->assertEquals(5, $email->getPriority());
417
    }
418
419
    public function testData()
420
    {
421
        $email = new Email();
422
        $this->assertEmpty($email->getData());
423
        $email->setData(array(
424
            'Title' => 'My Title',
425
        ));
426
        $this->assertCount(1, $email->getData());
427
        $this->assertEquals(array('Title' => 'My Title'), $email->getData());
428
429
        $email->addData('Content', 'My content');
430
        $this->assertCount(2, $email->getData());
431
        $this->assertEquals(array(
432
            'Title' => 'My Title',
433
            'Content' => 'My content',
434
        ), $email->getData());
435
        $email->removeData('Title');
436
        $this->assertEquals(array('Content' => 'My content'), $email->getData());
437
    }
438
439
    public function testDataWithViewableData()
440
    {
441
        $member = new Member();
442
        $member->FirstName = 'First Name';
443
        $email = new Email();
444
        $this->assertEmpty($email->getData());
445
        $email->setData($member);
446
        $this->assertEquals($member, $email->getData());
447
        $email->addData('Test', 'Test value');
448
        $this->assertEquals('Test value', $email->getData()->Test);
449
        $email->removeData('Test');
450
        $this->assertNull($email->getData()->Test);
451
    }
452
453
    public function testBody()
454
    {
455
        $email = new Email();
456
        $this->assertEmpty($email->getBody());
457
        $email->setBody('<h1>Title</h1>');
458
        $this->assertEquals('<h1>Title</h1>', $email->getBody());
459
    }
460
461
    public function testHTMLTemplate()
462
    {
463
        $email = new Email();
464
        $this->assertEquals(Email::class, $email->getHTMLTemplate());
465
        $email->setHTMLTemplate('MyTemplate');
466
        $this->assertEquals('MyTemplate', $email->getHTMLTemplate());
467
    }
468
469
    public function testPlainTemplate()
470
    {
471
        $email = new Email();
472
        $this->assertEmpty($email->getPlainTemplate());
473
        $email->setPlainTemplate('MyTemplate');
474
        $this->assertEquals('MyTemplate', $email->getPlainTemplate());
475
    }
476
477
    public function testGetFailedRecipients()
478
    {
479
        $mailer = new SwiftMailer();
480
        /** @var Swift_NullTransport|PHPUnit_Framework_MockObject_MockObject $transport */
481
        $transport = $this->getMockBuilder(Swift_NullTransport::class)->getMock();
482
        $transport->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Swift_NullTransport.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
483
                  ->method('send')
484
                  ->willThrowException(new Swift_RfcComplianceException('Bad email'));
485
        $mailer->setSwiftMailer(new Swift_Mailer($transport));
486
        $email = new Email();
487
        $email->setTo('[email protected]');
488
        $email->setFrom('[email protected]');
489
        $mailer->send($email);
490
        $this->assertCount(1, $email->getFailedRecipients());
491
    }
492
493
    public function testIsEmail()
494
    {
495
        $this->assertTrue((new Email)->IsEmail());
496
    }
497
498
    public function testRender()
499
    {
500
        $email = new Email();
501
        $email->setData(array(
502
            'EmailContent' => 'my content',
503
        ));
504
        $email->render();
505
        $this->assertContains('my content', $email->getBody());
506
        $children = $email->getSwiftMessage()->getChildren();
507
        $this->assertCount(1, $children);
508
        $plainPart = reset($children);
509
        $this->assertEquals('my content', $plainPart->getBody());
510
511
        // ensure repeat renders don't add multiple plain parts
512
        $email->render();
513
        $this->assertCount(1, $email->getSwiftMessage()->getChildren());
514
    }
515
516
    public function testRenderPlainOnly()
517
    {
518
        $email = new Email();
519
        $email->setData(array(
520
            'EmailContent' => 'test content',
521
        ));
522
        $email->render(true);
523
        $this->assertEquals('text/plain', $email->getSwiftMessage()->getContentType());
524
        $this->assertEmpty($email->getSwiftMessage()->getChildren());
525
    }
526
527
    public function testHasPlainPart()
528
    {
529
        $email = new Email();
530
        $email->setData(array(
531
            'EmailContent' => 'test',
532
        ));
533
        //emails are assumed to be HTML by default
534
        $this->assertFalse($email->hasPlainPart());
535
        //make sure plain attachments aren't picked up as a plain part
536
        $email->addAttachmentFromData('data', 'attachent.txt', 'text/plain');
537
        $this->assertFalse($email->hasPlainPart());
538
        $email->getSwiftMessage()->addPart('plain', 'text/plain');
539
        $this->assertTrue($email->hasPlainPart());
540
    }
541
542
    public function testGeneratePlainPartFromBody()
543
    {
544
        $email = new Email();
545
        $email->setBody('<h1>Test</h1>');
546
        $this->assertEmpty($email->getSwiftMessage()->getChildren());
547
        $email->generatePlainPartFromBody();
548
        $children = $email->getSwiftMessage()->getChildren();
549
        $this->assertCount(1, $children);
550
        $plainPart = reset($children);
551
        $this->assertContains('Test', $plainPart->getBody());
552
        $this->assertNotContains('<h1>Test</h1>', $plainPart->getBody());
553
    }
554
555
    public function testMultipleEmailSends()
556
    {
557
        $email = new Email();
558
        $email->setData(array(
559
            'EmailContent' => 'Test',
560
        ));
561
        $this->assertEmpty($email->getBody());
562
        $this->assertEmpty($email->getSwiftMessage()->getChildren());
563
        $email->send();
564
        $this->assertContains('Test', $email->getBody());
565
        $this->assertCount(1, $email->getSwiftMessage()->getChildren());
566
        $children = $email->getSwiftMessage()->getChildren();
567
        /** @var \Swift_MimePart $plainPart */
568
        $plainPart = reset($children);
569
        $this->assertContains('Test', $plainPart->getBody());
570
571
572
        //send again
573
        $email->send();
574
        $this->assertContains('Test', $email->getBody());
575
        $this->assertCount(1, $email->getSwiftMessage()->getChildren());
576
        $children = $email->getSwiftMessage()->getChildren();
577
        /** @var \Swift_MimePart $plainPart */
578
        $plainPart = reset($children);
579
        $this->assertContains('Test', $plainPart->getBody());
580
    }
581
}
582