Passed
Push — master ( 56e798...72475d )
by Thomas
02:26
created

EmailUtilsTest::testStringify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 21
rs 9.7998
1
<?php
2
3
namespace LeKoala\SparkPost\Test;
4
5
use SilverStripe\Dev\SapphireTest;
6
use LeKoala\SparkPost\EmailUtils;
7
use Symfony\Component\Mime\Address;
8
9
/**
10
 * Test for EmailUtils
11
 *
12
 * @group SparkPost
13
 */
14
class EmailUtilsTest extends SapphireTest
15
{
16
    public function testDisplayName(): void
17
    {
18
        $arr = [
19
            // Standard emails
20
            "[email protected]" => "me",
21
            "[email protected]" => "mobius",
22
            "[email protected]" => "test_with-chars.in.it",
23
            // Rfc emails
24
            "Me <[email protected]>" => "Me",
25
            "Möbius <[email protected]>" => "Möbius",
26
            "John Smith <[email protected]>" => "John Smith",
27
28
        ];
29
30
        foreach ($arr as $k => $v) {
31
            $displayName = EmailUtils::get_displayname_from_rfc_email($k);
32
            $this->assertEquals($v, $displayName);
33
        }
34
    }
35
36
    public function testGetEmail(): void
37
    {
38
        $arr = [
39
            // Standard emails
40
            "[email protected]" => "[email protected]",
41
            "[email protected]" => "[email protected]",
42
            "[email protected]" => "[email protected]",
43
            // Rfc emails
44
            "Me <[email protected]>" => "[email protected]",
45
            "Möbius <[email protected]>" => "[email protected]",
46
            "John Smith <[email protected]>" => "[email protected]",
47
48
        ];
49
50
        foreach ($arr as $k => $v) {
51
            $email = EmailUtils::get_email_from_rfc_email($k);
52
            $this->assertEquals($v, $email);
53
        }
54
    }
55
56
    public function testInlineStyles(): void
57
    {
58
        if (!class_exists(\Pelago\Emogrifier\CssInliner::class)) {
59
            $this->markTestIncomplete("Install pelago/emogrifier to run this test");
60
        }
61
62
        $html = <<<HTML
63
<!DOCTYPE html>
64
<html>
65
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">.red {color:red;}</style></head>
66
<body><span class="red">red</span></body>
67
</html>
68
HTML;
69
        $result = <<<HTML
70
<!DOCTYPE html>
71
<html>
72
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
73
<body><span class="red" style="color: red;">red</span></body>
74
</html>
75
76
HTML;
77
78
        $process = EmailUtils::inline_styles($html);
79
        $this->assertEquals($result, $process);
80
    }
81
82
    public function testConvertHtmlToText(): void
83
    {
84
        $textResult = "Some\r\nText Link (http://test.com) *End*";
85
86
        $someHtml = '   Some<br/>Text <a href="http://test.com">Link</a> <strong>End</strong>    ';
87
        $process = EmailUtils::convert_html_to_text($someHtml);
88
        $this->assertEquals($textResult, $process);
89
90
        $someHtml = "   Some<br/>Text <a class='test' href='http://test.com'>Link</a> <strong>End</strong>    ";
91
        $process = EmailUtils::convert_html_to_text($someHtml);
92
        $this->assertEquals($textResult, $process);
93
    }
94
95
    public function testStringify(): void
96
    {
97
        $testArr = ['[email protected]' => 'Testman'];
98
        $testArr2 = ['[email protected]', 'Testman'];
99
        $testAddr = new Address('[email protected]', 'Testman');
100
101
        $expected = 'Testman <[email protected]>';
102
103
        $this->assertEquals($expected, EmailUtils::stringify($testAddr));
104
        $this->assertEquals($expected, EmailUtils::stringify($testArr));
105
        $this->assertEquals($expected, EmailUtils::stringify($testArr2));
106
107
        $testArr = ['[email protected]'];
108
        $testArr2 = ['[email protected]'];
109
        $testAddr = new Address('[email protected]');
110
111
        $expected = '[email protected]';
112
113
        $this->assertEquals($expected, EmailUtils::stringify($testAddr));
114
        $this->assertEquals($expected, EmailUtils::stringify($testArr));
115
        $this->assertEquals($expected, EmailUtils::stringify($testArr2));
116
    }
117
}
118