Completed
Push — master ( 5c98d3...0a7e4c )
by Loz
11:47
created

EmailTest::testValidEmailAddress()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 3
eloc 11
c 1
b 1
f 1
nc 4
nop 0
dl 0
loc 18
rs 9.4285
1
<?php
2
/**
3
 * @package framework
4
 * @subpackage tests
5
 */
6
class EmailTest extends SapphireTest {
7
8
	public function testAttachFiles() {
9
		$email = new Email();
10
11
		$email->attachFileFromString('foo bar', 'foo.txt', 'text/plain');
12
		$email->attachFile(__DIR__ . '/fixtures/attachment.txt', null, 'text/plain');
13
14
		$this->assertEquals(
15
			array('contents'=>'foo bar', 'filename'=>'foo.txt', 'mimetype'=>'text/plain'),
16
			$email->attachments[0],
0 ignored issues
show
Documentation introduced by
The property $attachments is declared protected in Email. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
17
			'File is attached correctly from string'
18
		);
19
20
		$this->assertEquals(
21
			array('contents'=>'Hello, I\'m a text document.', 'filename'=>'attachment.txt', 'mimetype'=>'text/plain'),
22
			$email->attachments[1],
0 ignored issues
show
Documentation introduced by
The property $attachments is declared protected in Email. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
23
			'File is attached correctly from file'
24
		);
25
	}
26
27
	public function testCustomHeaders() {
28
		$email = new Email();
29
30
		$email->addCustomHeader('Cc', '[email protected]');
31
		$email->addCustomHeader('Bcc', '[email protected]');
32
33
		$this->assertEmpty(
34
			$email->customHeaders,
0 ignored issues
show
Documentation introduced by
The property $customHeaders is declared protected in Email. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
			'addCustomHeader() doesn\'t add Cc and Bcc headers'
36
		);
37
38
		$email->addCustomHeader('Reply-To', '[email protected]');
39
		$this->assertEquals(
40
			array('Reply-To' => '[email protected]'),
41
			$email->customHeaders,
0 ignored issues
show
Documentation introduced by
The property $customHeaders is declared protected in Email. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
			'addCustomHeader() adds headers'
43
		);
44
45
		$email->addCustomHeader('Reply-To', '[email protected]');
46
		$this->assertEquals(
47
			array('Reply-To' => '[email protected], [email protected]'),
48
			$email->customHeaders,
0 ignored issues
show
Documentation introduced by
The property $customHeaders is declared protected in Email. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
			'addCustomHeader() appends data to existing headers'
50
		);
51
	}
52
53
	public function testValidEmailAddress() {
54
		$validEmails = array('[email protected]', '[email protected]');
55
		$invalidEmails = array('foo.bar@', '@example.com', 'foo@');
56
57
		foreach ($validEmails as $email) {
58
			$this->assertTrue(
59
				Email::is_valid_address($email),
60
				'is_valid_address() returns true for a valid email address'
61
			);
62
		}
63
64
		foreach ($invalidEmails as $email) {
65
			$this->assertFalse(
66
				Email::is_valid_address($email),
67
				'is_valid_address() returns false for an invalid email address'
68
			);
69
		}
70
	}
71
72
	public function testObfuscate() {
73
		$emailAddress = '[email protected]';
74
75
		$direction = Email::obfuscate($emailAddress, 'direction');
76
		$visible = Email::obfuscate($emailAddress, 'visible');
77
		$hex = Email::obfuscate($emailAddress, 'hex');
78
79
		$this->assertEquals(
80
			'<span class="codedirection">moc.elpmaxe@1-tset</span>',
81
			$direction,
82
			'obfuscate() correctly reverses the email direction'
83
		);
84
		$this->assertEquals(
85
			'test [dash] 1 [at] example [dot] com',
86
			$visible,
87
			'obfuscate() correctly obfuscates email characters'
88
		);
89
		$this->assertEquals(
90
			'&#x74;&#x65;&#x73;&#x74;&#x2d;&#x31;&#x40;&#x65;&#x78;&#x61;&#x6d;&#x70;'
91
				. '&#x6c;&#x65;&#x2e;&#x63;&#x6f;&#x6d;',
92
			$hex,
93
			'obfuscate() correctly returns hex representation of email'
94
		);
95
	}
96
97
	public function testSendPlain() {
98
		// Set custom $project - used in email headers
99
		global $project;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
100
		$oldProject = $project;
101
		$project = 'emailtest';
102
103
		Injector::inst()->registerService(new EmailTest_Mailer(), 'Mailer');
104
		$email = new Email(
105
			'[email protected]',
106
			'[email protected]',
107
			'Test send plain',
108
			'Testing Email->sendPlain()',
109
			null,
110
			'[email protected]',
111
			'[email protected]'
112
		);
113
		$email->attachFile(__DIR__ . '/fixtures/attachment.txt', null, 'text/plain');
114
		$email->addCustomHeader('foo', 'bar');
115
		$sent = $email->sendPlain(123);
116
117
		// Restore old project name after sending
118
		$project = $oldProject;
119
120
		$this->assertEquals('[email protected]', $sent['to']);
121
		$this->assertEquals('[email protected]', $sent['from']);
122
		$this->assertEquals('Test send plain', $sent['subject']);
123
		$this->assertEquals('Testing Email->sendPlain()', $sent['content']);
124
		$this->assertEquals(
125
			array(
126
				0 => array(
127
					'contents'=>'Hello, I\'m a text document.',
128
					'filename'=>'attachment.txt',
129
					'mimetype'=>'text/plain'
130
				)
131
			),
132
			$sent['files']
133
		);
134
		$this->assertEquals(
135
			array(
136
				'foo' => 'bar',
137
				'X-SilverStripeMessageID' => 'emailtest.123',
138
				'X-SilverStripeSite' => 'emailtest',
139
				'Cc' => '[email protected]',
140
				'Bcc' => '[email protected]'
141
			),
142
			$sent['customheaders']
143
		);
144
	}
145
146
	public function testSendHTML() {
147
		// Set custom $project - used in email headers
148
		global $project;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
149
		$oldProject = $project;
150
		$project = 'emailtest';
151
152
		Injector::inst()->registerService(new EmailTest_Mailer(), 'Mailer');
153
		$email = new Email(
154
			'[email protected]',
155
			'[email protected]',
156
			'Test send plain',
157
			'Testing Email->send()',
158
			null,
159
			'[email protected]',
160
			'[email protected]'
161
		);
162
		$email->attachFile(__DIR__ . '/fixtures/attachment.txt', null, 'text/plain');
163
		$email->addCustomHeader('foo', 'bar');
164
		$sent = $email->send(123);
165
166
		// Restore old project name after sending
167
		$project = $oldProject;
168
169
		$this->assertEquals('[email protected]', $sent['to']);
170
		$this->assertEquals('[email protected]', $sent['from']);
171
		$this->assertEquals('Test send plain', $sent['subject']);
172
		$this->assertContains('Testing Email-&gt;send()', $sent['content']);
173
		$this->assertNull($sent['plaincontent']);
174
		$this->assertEquals(
175
			array(
176
				0 => array(
177
					'contents'=>'Hello, I\'m a text document.',
178
					'filename'=>'attachment.txt',
179
					'mimetype'=>'text/plain'
180
				)
181
			),
182
			$sent['files']
183
		);
184
		$this->assertEquals(
185
			array(
186
				'foo' => 'bar',
187
				'X-SilverStripeMessageID' => 'emailtest.123',
188
				'X-SilverStripeSite' => 'emailtest',
189
				'Cc' => '[email protected]',
190
				'Bcc' => '[email protected]'
191
			),
192
			$sent['customheaders']
193
		);
194
	}
195
196
}
197
198
class EmailTest_Mailer extends Mailer {
199
200
	public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false,
201
			$plainContent = false) {
202
		return array(
203
			'to' => $to,
204
			'from' => $from,
205
			'subject' => $subject,
206
			'content' => $htmlContent,
207
			'files' => $attachedFiles,
208
			'customheaders' => $customheaders,
209
			'plaincontent' => $plainContent
210
		);
211
	}
212
213
	public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customheaders = false) {
214
		return array(
215
			'to' => $to,
216
			'from' => $from,
217
			'subject' => $subject,
218
			'content' => $plainContent,
219
			'files' => $attachedFiles,
220
			'customheaders' => $customheaders
221
		);
222
	}
223
224
}
225