Completed
Push — master ( ee71d8...4caee8 )
by Jacob
03:27
created
src/Archangel.php 1 patch
Indentation   +393 added lines, -394 removed lines patch added patch discarded remove patch
@@ -16,399 +16,398 @@
 block discarded – undo
16 16
 class Archangel implements LoggerAwareInterface
17 17
 {
18 18
 
19
-    /** @var string $subject */
20
-    protected $subject;
21
-
22
-    /** @var array $toAddresses */
23
-    protected $toAddresses = array();
24
-
25
-    /** @var array $headers */
26
-    protected $headers = array();
27
-
28
-    /** @var string $plainMessage */
29
-    protected $plainMessage;
30
-
31
-    /** @var string $htmlMessage */
32
-    protected $htmlMessage;
33
-
34
-    /** @var array $attachments */
35
-    protected $attachments = array();
36
-
37
-    /** @var string $boundaryMixed */
38
-    protected $boundaryMixed;
39
-
40
-    /** @var string $boundaryAlternative */
41
-    protected $boundaryAlternative;
42
-
43
-    /** @var LoggerInterface */
44
-    protected $logger;
45
-
46
-    /** @var string LINE_BREAK */
47
-    const LINE_BREAK = "\r\n";
48
-
49
-    /**
50
-     * @param string $mailer
51
-     */
52
-    public function __construct($mailer = null)
53
-    {
54
-        if (is_null($mailer)) {
55
-            $mailer = sprintf('PHP/%s', phpversion());
56
-        }
57
-        $this->headers['X-Mailer'] = $mailer;
58
-
59
-        $this->logger = new NullLogger();
60
-        $this->boundaryMixed = sprintf('PHP-mixed-%s', uniqid());
61
-        $this->boundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
62
-    }
63
-
64
-    /**
65
-     * @param LoggerInterface $logger
66
-     *
67
-     * @return $this;
68
-     */
69
-    public function setLogger(LoggerInterface $logger)
70
-    {
71
-        $this->logger = $logger;
72
-
73
-        return $this;
74
-    }
75
-
76
-    /**
77
-     * Setter method for adding recipients
78
-     *
79
-     * @param string $address email address for the recipient
80
-     * @param string $title   name of the recipient (optional)
81
-
82
-     * @return object instantiated $this
83
-     */
84
-    public function addTo($address, $title = '')
85
-    {
86
-        array_push(
87
-            $this->toAddresses,
88
-            $this->formatEmailAddress($address, $title)
89
-        );
90
-
91
-        return $this;
92
-    }
93
-
94
-    /**
95
-     * Setter method for adding cc recipients
96
-     *
97
-     * @param string $address email address for the cc recipient
98
-     * @param string $title   name of the cc recipient (optional)
99
-     *
100
-     * @return object instantiated $this
101
-     */
102
-    public function addCC($address, $title = '')
103
-    {
104
-        if (!isset($this->headers['CC'])) {
105
-            $this->headers['CC'] = array();
106
-        }
107
-
108
-        array_push(
109
-            $this->headers['CC'],
110
-            $this->formatEmailAddress($address, $title)
111
-        );
112
-
113
-        return $this;
114
-    }
115
-
116
-    /**
117
-     * Setter method for adding bcc recipients
118
-     *
119
-     * @param string $address email address for the bcc recipient
120
-     * @param string $title   name of the bcc recipient (optional)
121
-     *
122
-     * @return object instantiated $this
123
-     */
124
-    public function addBCC($address, $title = '')
125
-    {
126
-        if (!isset($this->headers['BCC'])) {
127
-            $this->headers['BCC'] = array();
128
-        }
129
-
130
-        array_push(
131
-            $this->headers['BCC'],
132
-            $this->formatEmailAddress($address, $title)
133
-        );
134
-
135
-        return $this;
136
-    }
137
-
138
-    /**
139
-     * Setter method for setting the single 'from' field
140
-     *
141
-     * @param string $address email address for the sender
142
-     * @param string $title   name of the sender (optional)
143
-     *
144
-     * @return object instantiated $this
145
-     */
146
-    public function setFrom($address, $title = '')
147
-    {
148
-        $this->headers['From'] = $this->formatEmailAddress($address, $title);
149
-
150
-        return $this;
151
-    }
152
-
153
-    /**
154
-     * Setter method for setting the single 'reply-to' field
155
-     *
156
-     * @param string $address email address for the reply-to
157
-     * @param string $title   name of the reply-to (optional)
158
-     *
159
-     * @return object instantiated $this
160
-     */
161
-    public function setReplyTo($address, $title = '')
162
-    {
163
-        $this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
164
-
165
-        return $this;
166
-    }
167
-
168
-    /**
169
-     * @param string $address
170
-     * @param string $title
171
-     *
172
-     * @return string
173
-     */
174
-    protected function formatEmailAddress($address, $title)
175
-    {
176
-        if (!empty($title)) {
177
-            $address = sprintf('"%s" <%s>', $title, $address);
178
-        }
179
-        return $address;
180
-    }
181
-
182
-    /**
183
-     * Setter method for setting a subject
184
-     *
185
-     * @param string $subject subject for the email
186
-     *
187
-     * @return object instantiated $this
188
-     */
189
-    public function setSubject($subject)
190
-    {
191
-        $this->subject = $subject;
192
-
193
-        return $this;
194
-    }
195
-
196
-    /**
197
-     * Setter method for the plain text message
198
-     *
199
-     * @param string $message the plain-text message
200
-     *
201
-     * @return object instantiated $this
202
-     */
203
-    public function setPlainMessage($message)
204
-    {
205
-        $this->plainMessage = $message;
206
-
207
-        return $this;
208
-    }
209
-
210
-    /**
211
-     * Setter method for the html message
212
-     *
213
-     * @param string $message the html message
214
-     *
215
-     * @return object instantiated $this
216
-     */
217
-    public function setHTMLMessage($message)
218
-    {
219
-        $this->htmlMessage = $message;
220
-
221
-        return $this;
222
-    }
223
-
224
-    /**
225
-     * Setter method for adding attachments
226
-     *
227
-     * @param string $path  the full path of the attachment
228
-     * @param string $type  mime type of the file
229
-     * @param string $title the title of the attachment (optional)
230
-     *
231
-     * @return object instantiated $this
232
-     */
233
-    public function addAttachment($path, $type, $title = '')
234
-    {
235
-        array_push($this->attachments, array(
236
-          'path' => $path,
237
-          'type' => $type,
238
-          'title' => $title,
239
-        ));
240
-
241
-        return $this;
242
-    }
243
-
244
-    /**
245
-     * The executing step, the actual sending of the email
246
-     * First checks to make sure the minimum fields are set (returns false if they are not)
247
-     * Second it attempts to send the mail with php's mail() (returns false if it fails)
248
-     *
249
-     * return boolean whether or not the email was valid & sent
250
-     */
251
-    public function send()
252
-    {
253
-        if (!$this->checkRequiredFields()) {
254
-            return false;
255
-        }
256
-
257
-        $recipients = $this->buildTo();
258
-        $subject = $this->subject;
259
-        $message = $this->buildMessage();
260
-        $headers = $this->buildHeaders();
261
-
262
-        return mail($recipients, $subject, $message, $headers);
263
-    }
264
-
265
-    /**
266
-     * Call to check the minimum required fields
267
-     *
268
-     * @return boolean whether or not the email meets the minimum required fields
269
-     */
270
-    protected function checkRequiredFields()
271
-    {
272
-        if (empty($this->toAddresses)) {
273
-            return false;
274
-        }
275
-        if (empty($this->subject)) {
276
-            return false;
277
-        }
278
-
279
-        if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
280
-            return false;
281
-        }
282
-
283
-        return true;
284
-    }
285
-
286
-    /**
287
-     * Build the recipients from 'to'
288
-     *
289
-     * @return string comma-separated lit of recipients
290
-     */
291
-    protected function buildTo()
292
-    {
293
-        return implode(', ', $this->toAddresses);
294
-    }
295
-
296
-    /**
297
-     * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see
298
-     *
299
-     * @return string email message
300
-     */
301
-    protected function buildMessage()
302
-    {
303
-        $messageString = '';
19
+	/** @var string $subject */
20
+	protected $subject;
21
+
22
+	/** @var array $toAddresses */
23
+	protected $toAddresses = array();
24
+
25
+	/** @var array $headers */
26
+	protected $headers = array();
27
+
28
+	/** @var string $plainMessage */
29
+	protected $plainMessage;
30
+
31
+	/** @var string $htmlMessage */
32
+	protected $htmlMessage;
33
+
34
+	/** @var array $attachments */
35
+	protected $attachments = array();
36
+
37
+	/** @var string $boundaryMixed */
38
+	protected $boundaryMixed;
39
+
40
+	/** @var string $boundaryAlternative */
41
+	protected $boundaryAlternative;
42
+
43
+	/** @var LoggerInterface */
44
+	protected $logger;
45
+
46
+	/** @var string LINE_BREAK */
47
+	const LINE_BREAK = "\r\n";
48
+
49
+	/**
50
+	 * @param string $mailer
51
+	 */
52
+	public function __construct($mailer = null)
53
+	{
54
+		if (is_null($mailer)) {
55
+			$mailer = sprintf('PHP/%s', phpversion());
56
+		}
57
+		$this->headers['X-Mailer'] = $mailer;
58
+
59
+		$this->logger = new NullLogger();
60
+		$this->boundaryMixed = sprintf('PHP-mixed-%s', uniqid());
61
+		$this->boundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
62
+	}
63
+
64
+	/**
65
+	 * @param LoggerInterface $logger
66
+	 *
67
+	 * @return $this;
68
+	 */
69
+	public function setLogger(LoggerInterface $logger)
70
+	{
71
+		$this->logger = $logger;
72
+
73
+		return $this;
74
+	}
75
+
76
+	/**
77
+	 * Setter method for adding recipients
78
+	 *
79
+	 * @param string $address email address for the recipient
80
+	 * @param string $title   name of the recipient (optional)
81
+	 * @return object instantiated $this
82
+	 */
83
+	public function addTo($address, $title = '')
84
+	{
85
+		array_push(
86
+			$this->toAddresses,
87
+			$this->formatEmailAddress($address, $title)
88
+		);
89
+
90
+		return $this;
91
+	}
92
+
93
+	/**
94
+	 * Setter method for adding cc recipients
95
+	 *
96
+	 * @param string $address email address for the cc recipient
97
+	 * @param string $title   name of the cc recipient (optional)
98
+	 *
99
+	 * @return object instantiated $this
100
+	 */
101
+	public function addCC($address, $title = '')
102
+	{
103
+		if (!isset($this->headers['CC'])) {
104
+			$this->headers['CC'] = array();
105
+		}
106
+
107
+		array_push(
108
+			$this->headers['CC'],
109
+			$this->formatEmailAddress($address, $title)
110
+		);
111
+
112
+		return $this;
113
+	}
114
+
115
+	/**
116
+	 * Setter method for adding bcc recipients
117
+	 *
118
+	 * @param string $address email address for the bcc recipient
119
+	 * @param string $title   name of the bcc recipient (optional)
120
+	 *
121
+	 * @return object instantiated $this
122
+	 */
123
+	public function addBCC($address, $title = '')
124
+	{
125
+		if (!isset($this->headers['BCC'])) {
126
+			$this->headers['BCC'] = array();
127
+		}
128
+
129
+		array_push(
130
+			$this->headers['BCC'],
131
+			$this->formatEmailAddress($address, $title)
132
+		);
133
+
134
+		return $this;
135
+	}
136
+
137
+	/**
138
+	 * Setter method for setting the single 'from' field
139
+	 *
140
+	 * @param string $address email address for the sender
141
+	 * @param string $title   name of the sender (optional)
142
+	 *
143
+	 * @return object instantiated $this
144
+	 */
145
+	public function setFrom($address, $title = '')
146
+	{
147
+		$this->headers['From'] = $this->formatEmailAddress($address, $title);
148
+
149
+		return $this;
150
+	}
151
+
152
+	/**
153
+	 * Setter method for setting the single 'reply-to' field
154
+	 *
155
+	 * @param string $address email address for the reply-to
156
+	 * @param string $title   name of the reply-to (optional)
157
+	 *
158
+	 * @return object instantiated $this
159
+	 */
160
+	public function setReplyTo($address, $title = '')
161
+	{
162
+		$this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
163
+
164
+		return $this;
165
+	}
166
+
167
+	/**
168
+	 * @param string $address
169
+	 * @param string $title
170
+	 *
171
+	 * @return string
172
+	 */
173
+	protected function formatEmailAddress($address, $title)
174
+	{
175
+		if (!empty($title)) {
176
+			$address = sprintf('"%s" <%s>', $title, $address);
177
+		}
178
+		return $address;
179
+	}
180
+
181
+	/**
182
+	 * Setter method for setting a subject
183
+	 *
184
+	 * @param string $subject subject for the email
185
+	 *
186
+	 * @return object instantiated $this
187
+	 */
188
+	public function setSubject($subject)
189
+	{
190
+		$this->subject = $subject;
191
+
192
+		return $this;
193
+	}
194
+
195
+	/**
196
+	 * Setter method for the plain text message
197
+	 *
198
+	 * @param string $message the plain-text message
199
+	 *
200
+	 * @return object instantiated $this
201
+	 */
202
+	public function setPlainMessage($message)
203
+	{
204
+		$this->plainMessage = $message;
205
+
206
+		return $this;
207
+	}
208
+
209
+	/**
210
+	 * Setter method for the html message
211
+	 *
212
+	 * @param string $message the html message
213
+	 *
214
+	 * @return object instantiated $this
215
+	 */
216
+	public function setHTMLMessage($message)
217
+	{
218
+		$this->htmlMessage = $message;
219
+
220
+		return $this;
221
+	}
222
+
223
+	/**
224
+	 * Setter method for adding attachments
225
+	 *
226
+	 * @param string $path  the full path of the attachment
227
+	 * @param string $type  mime type of the file
228
+	 * @param string $title the title of the attachment (optional)
229
+	 *
230
+	 * @return object instantiated $this
231
+	 */
232
+	public function addAttachment($path, $type, $title = '')
233
+	{
234
+		array_push($this->attachments, array(
235
+		  'path' => $path,
236
+		  'type' => $type,
237
+		  'title' => $title,
238
+		));
239
+
240
+		return $this;
241
+	}
242
+
243
+	/**
244
+	 * The executing step, the actual sending of the email
245
+	 * First checks to make sure the minimum fields are set (returns false if they are not)
246
+	 * Second it attempts to send the mail with php's mail() (returns false if it fails)
247
+	 *
248
+	 * return boolean whether or not the email was valid & sent
249
+	 */
250
+	public function send()
251
+	{
252
+		if (!$this->checkRequiredFields()) {
253
+			return false;
254
+		}
255
+
256
+		$recipients = $this->buildTo();
257
+		$subject = $this->subject;
258
+		$message = $this->buildMessage();
259
+		$headers = $this->buildHeaders();
260
+
261
+		return mail($recipients, $subject, $message, $headers);
262
+	}
263
+
264
+	/**
265
+	 * Call to check the minimum required fields
266
+	 *
267
+	 * @return boolean whether or not the email meets the minimum required fields
268
+	 */
269
+	protected function checkRequiredFields()
270
+	{
271
+		if (empty($this->toAddresses)) {
272
+			return false;
273
+		}
274
+		if (empty($this->subject)) {
275
+			return false;
276
+		}
277
+
278
+		if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
279
+			return false;
280
+		}
281
+
282
+		return true;
283
+	}
284
+
285
+	/**
286
+	 * Build the recipients from 'to'
287
+	 *
288
+	 * @return string comma-separated lit of recipients
289
+	 */
290
+	protected function buildTo()
291
+	{
292
+		return implode(', ', $this->toAddresses);
293
+	}
294
+
295
+	/**
296
+	 * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see
297
+	 *
298
+	 * @return string email message
299
+	 */
300
+	protected function buildMessage()
301
+	{
302
+		$messageString = '';
304 303
         
305
-        if (!empty($this->attachments)) {
306
-            $messageString .= "--{$this->boundaryMixed}" . self::LINE_BREAK;
307
-        }
308
-        if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
309
-            if (!empty($this->attachments)) {
310
-                $messageString .= "Content-Type: multipart/alternative; boundary={$this->boundaryAlternative}" . self::LINE_BREAK;
311
-                $messageString .= self::LINE_BREAK;
312
-            }
313
-            $messageString .= "--{$this->boundaryAlternative}" . self::LINE_BREAK;
314
-            $messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
315
-            $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
316
-            $messageString .= self::LINE_BREAK;
317
-            $messageString .= $this->plainMessage;
318
-            $messageString .= self::LINE_BREAK;
319
-            $messageString .= "--{$this->boundaryAlternative}" . self::LINE_BREAK;
320
-            $messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
321
-            $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
322
-            $messageString .= self::LINE_BREAK;
323
-            $messageString .= $this->htmlMessage;
324
-            $messageString .= self::LINE_BREAK;
325
-            $messageString .= "--{$this->boundaryAlternative}--" . self::LINE_BREAK;
326
-            $messageString .= self::LINE_BREAK;
327
-        } elseif (!empty($this->plainMessage)) {
328
-            if (!empty($this->attachments)) {
329
-                $messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
330
-                $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
331
-                $messageString .= self::LINE_BREAK;
332
-            }
333
-            $messageString .= $this->plainMessage;
334
-            $messageString .= self::LINE_BREAK;
335
-        } elseif (!empty($this->htmlMessage)) {
336
-            if (!empty($this->attachments)) {
337
-                $messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
338
-                $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
339
-                $messageString .= self::LINE_BREAK;
340
-            }
341
-            $messageString .= $this->htmlMessage;
342
-            $messageString .= self::LINE_BREAK;
343
-        }
344
-        if (!empty($this->attachments)) {
345
-            foreach ($this->attachments as $attachment) {
346
-                $messageString .= "--{$this->boundaryMixed}" . self::LINE_BREAK;
347
-                $messageString .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK;
348
-                $messageString .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK;
349
-                $messageString .= 'Content-Disposition: attachment' . self::LINE_BREAK;
350
-                $messageString .= self::LINE_BREAK;
351
-                $messageString .= $this->buildAttachmentContent($attachment);
352
-                $messageString .= self::LINE_BREAK;
353
-            }
354
-            $messageString .= "--{$this->boundaryMixed}--" . self::LINE_BREAK;
355
-        }
356
-        return $messageString;
357
-    }
358
-
359
-
360
-    /**
361
-     * Builder for the additional headers needed for multipart emails
362
-     *
363
-     * @return string headers needed for multipart
364
-     */
365
-    protected function buildHeaders()
366
-    {
367
-        $headers = array();
368
-        foreach ($this->headers as $key => $value) {
369
-            if ($key == 'CC' || $key == 'BCC') {
370
-                $value = implode(', ', $value);
371
-            }
372
-            array_push($headers, sprintf('%s: %s', $key, $value));
373
-        }
374
-
375
-        if (!empty($this->attachments)) {
376
-            array_push(
377
-                $headers,
378
-                "Content-Type: multipart/mixed; boundary=\"{$this->boundaryMixed}\""
379
-            );
380
-        } elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
381
-            array_push(
382
-                $headers,
383
-                "Content-Type: multipart/alternative; boundary=\"{$this->boundaryAlternative}\""
384
-            );
385
-        } elseif (!empty($this->htmlMessage)) {
386
-            array_push(
387
-                $headers,
388
-                'Content-type: text/html; charset="iso-8859-1"'
389
-            );
390
-        }
391
-
392
-        return implode(self::LINE_BREAK, $headers);
393
-    }
394
-
395
-    /**
396
-     * File reader for attachments
397
-     *
398
-     * @return string binary representation of file, base64'd
399
-     */
400
-    protected function buildAttachmentContent($attachment)
401
-    {
402
-        if (!file_exists($attachment['path'])) {
403
-            return ''; // todo log error
404
-        }
405
-
406
-        $handle = fopen($attachment['path'], 'r');
407
-        $contents = fread($handle, filesize($attachment['path']));
408
-        fclose($handle);
409
-
410
-        $contents = base64_encode($contents);
411
-        $contents = chunk_split($contents);
412
-        return $contents;
413
-    }
304
+		if (!empty($this->attachments)) {
305
+			$messageString .= "--{$this->boundaryMixed}" . self::LINE_BREAK;
306
+		}
307
+		if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
308
+			if (!empty($this->attachments)) {
309
+				$messageString .= "Content-Type: multipart/alternative; boundary={$this->boundaryAlternative}" . self::LINE_BREAK;
310
+				$messageString .= self::LINE_BREAK;
311
+			}
312
+			$messageString .= "--{$this->boundaryAlternative}" . self::LINE_BREAK;
313
+			$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
314
+			$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
315
+			$messageString .= self::LINE_BREAK;
316
+			$messageString .= $this->plainMessage;
317
+			$messageString .= self::LINE_BREAK;
318
+			$messageString .= "--{$this->boundaryAlternative}" . self::LINE_BREAK;
319
+			$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
320
+			$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
321
+			$messageString .= self::LINE_BREAK;
322
+			$messageString .= $this->htmlMessage;
323
+			$messageString .= self::LINE_BREAK;
324
+			$messageString .= "--{$this->boundaryAlternative}--" . self::LINE_BREAK;
325
+			$messageString .= self::LINE_BREAK;
326
+		} elseif (!empty($this->plainMessage)) {
327
+			if (!empty($this->attachments)) {
328
+				$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
329
+				$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
330
+				$messageString .= self::LINE_BREAK;
331
+			}
332
+			$messageString .= $this->plainMessage;
333
+			$messageString .= self::LINE_BREAK;
334
+		} elseif (!empty($this->htmlMessage)) {
335
+			if (!empty($this->attachments)) {
336
+				$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
337
+				$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
338
+				$messageString .= self::LINE_BREAK;
339
+			}
340
+			$messageString .= $this->htmlMessage;
341
+			$messageString .= self::LINE_BREAK;
342
+		}
343
+		if (!empty($this->attachments)) {
344
+			foreach ($this->attachments as $attachment) {
345
+				$messageString .= "--{$this->boundaryMixed}" . self::LINE_BREAK;
346
+				$messageString .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK;
347
+				$messageString .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK;
348
+				$messageString .= 'Content-Disposition: attachment' . self::LINE_BREAK;
349
+				$messageString .= self::LINE_BREAK;
350
+				$messageString .= $this->buildAttachmentContent($attachment);
351
+				$messageString .= self::LINE_BREAK;
352
+			}
353
+			$messageString .= "--{$this->boundaryMixed}--" . self::LINE_BREAK;
354
+		}
355
+		return $messageString;
356
+	}
357
+
358
+
359
+	/**
360
+	 * Builder for the additional headers needed for multipart emails
361
+	 *
362
+	 * @return string headers needed for multipart
363
+	 */
364
+	protected function buildHeaders()
365
+	{
366
+		$headers = array();
367
+		foreach ($this->headers as $key => $value) {
368
+			if ($key == 'CC' || $key == 'BCC') {
369
+				$value = implode(', ', $value);
370
+			}
371
+			array_push($headers, sprintf('%s: %s', $key, $value));
372
+		}
373
+
374
+		if (!empty($this->attachments)) {
375
+			array_push(
376
+				$headers,
377
+				"Content-Type: multipart/mixed; boundary=\"{$this->boundaryMixed}\""
378
+			);
379
+		} elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
380
+			array_push(
381
+				$headers,
382
+				"Content-Type: multipart/alternative; boundary=\"{$this->boundaryAlternative}\""
383
+			);
384
+		} elseif (!empty($this->htmlMessage)) {
385
+			array_push(
386
+				$headers,
387
+				'Content-type: text/html; charset="iso-8859-1"'
388
+			);
389
+		}
390
+
391
+		return implode(self::LINE_BREAK, $headers);
392
+	}
393
+
394
+	/**
395
+	 * File reader for attachments
396
+	 *
397
+	 * @return string binary representation of file, base64'd
398
+	 */
399
+	protected function buildAttachmentContent($attachment)
400
+	{
401
+		if (!file_exists($attachment['path'])) {
402
+			return ''; // todo log error
403
+		}
404
+
405
+		$handle = fopen($attachment['path'], 'r');
406
+		$contents = fread($handle, filesize($attachment['path']));
407
+		fclose($handle);
408
+
409
+		$contents = base64_encode($contents);
410
+		$contents = chunk_split($contents);
411
+		return $contents;
412
+	}
414 413
 }
Please login to merge, or discard this patch.
tests/ArchangelTest.php 1 patch
Indentation   +618 added lines, -618 removed lines patch added patch discarded remove patch
@@ -8,622 +8,622 @@
 block discarded – undo
8 8
 class ArchangelTest extends PHPUnit_Framework_TestCase
9 9
 {
10 10
 
11
-    public function testIsInstanceOfArchangel()
12
-    {
13
-        $archangel = new Archangel();
14
-
15
-        $this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
16
-    }
17
-
18
-    public function testIsLoggerAwareInterface()
19
-    {
20
-        $archangel = new Archangel();
21
-
22
-        $this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel);
23
-    }
24
-
25
-    public function testConstructSetsDefaultMailer()
26
-    {
27
-        $archangel = new Archangel();
28
-        $mailer = sprintf('PHP/%s', phpversion());
29
-        $headers = array('X-Mailer' => $mailer);
30
-
31
-        $this->assertAttributeEquals($headers, 'headers', $archangel);
32
-    }
33
-
34
-    public function testConstructOverridesMailer()
35
-    {
36
-        $archangel = new Archangel('AwesomeMailer');
37
-        $headers = array('X-Mailer' => 'AwesomeMailer');
38
-
39
-        $this->assertAttributeEquals($headers, 'headers', $archangel);
40
-    }
41
-
42
-    public function testConstructSetsNullLogger()
43
-    {
44
-        $archangel = new Archangel();
45
-
46
-        $this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
47
-    }
48
-
49
-    public function testConstructSetsBoundaries()
50
-    {
51
-        $archangel = new Archangel();
52
-        $expectedBoundaryMixed = sprintf('PHP-mixed-%s', uniqid());
53
-        $expectedBoundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
54
-
55
-        $this->assertAttributeEquals($expectedBoundaryMixed, 'boundaryMixed', $archangel);
56
-        $this->assertAttributeEquals($expectedBoundaryAlternative, 'boundaryAlternative', $archangel);
57
-    }
58
-
59
-    public function testSetLogger()
60
-    {
61
-        $logger = $this->getMock('Psr\Log\LoggerInterface');
62
-        $archangel = new Archangel();
63
-        $archangel->setLogger($logger);
64
-
65
-        $this->assertAttributeSame($logger, 'logger', $archangel);
66
-    }
67
-
68
-    public function testAddTo()
69
-    {
70
-        $archangel = new Archangel();
71
-        $archangel->addTo('[email protected]');
72
-
73
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
74
-    }
75
-
76
-    public function testAddToMultiple()
77
-    {
78
-        $archangel = new Archangel();
79
-        $archangel->addTo('[email protected]');
80
-        $archangel->addTo('[email protected]');
81
-
82
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
83
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
84
-    }
85
-
86
-    public function testAddToWithTitle()
87
-    {
88
-        $archangel = new Archangel();
89
-        $archangel->addTo('[email protected]', 'Mr. Test Alot');
90
-
91
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
92
-    }
93
-
94
-    public function testAddCC()
95
-    {
96
-        $archangel = new Archangel();
97
-        $archangel->addCC('[email protected]');
98
-        $headersProperty = $this->getProtectedProperty('headers');
99
-        $headers = $headersProperty->getValue($archangel);
100
-
101
-        $this->assertArraySubset(
102
-            array('CC' => array('[email protected]')),
103
-            $headers
104
-        );
105
-    }
106
-
107
-    public function testAddCCMultiple()
108
-    {
109
-        $archangel = new Archangel();
110
-        $archangel->addCC('[email protected]');
111
-        $archangel->addCC('[email protected]');
112
-        $headersProperty = $this->getProtectedProperty('headers');
113
-        $headers = $headersProperty->getValue($archangel);
114
-
115
-        $this->assertArraySubset(
116
-            array('CC' => array('[email protected]', '[email protected]')),
117
-            $headers
118
-        );
119
-    }
120
-
121
-    public function testAddCCWithTitle()
122
-    {
123
-        $archangel = new Archangel();
124
-        $archangel->addCC('[email protected]', 'Mr. Test Alot');
125
-        $headersProperty = $this->getProtectedProperty('headers');
126
-        $headers = $headersProperty->getValue($archangel);
127
-
128
-        $this->assertArraySubset(
129
-            array('CC' => array('"Mr. Test Alot" <[email protected]>')),
130
-            $headers
131
-        );
132
-    }
133
-
134
-    public function testAddBCC()
135
-    {
136
-        $archangel = new Archangel();
137
-        $archangel->addBCC('[email protected]');
138
-        $headersProperty = $this->getProtectedProperty('headers');
139
-        $headers = $headersProperty->getValue($archangel);
140
-
141
-        $this->assertArraySubset(
142
-            array('BCC' => array('[email protected]')),
143
-            $headers
144
-        );
145
-    }
146
-
147
-    public function testAddBCCMultiple()
148
-    {
149
-        $archangel = new Archangel();
150
-        $archangel->addBCC('[email protected]');
151
-        $archangel->addBCC('[email protected]');
152
-        $headersProperty = $this->getProtectedProperty('headers');
153
-        $headers = $headersProperty->getValue($archangel);
154
-
155
-        $this->assertArraySubset(
156
-            array('BCC' => array('[email protected]', '[email protected]')),
157
-            $headers
158
-        );
159
-    }
160
-
161
-    public function testAddBCCWithTitle()
162
-    {
163
-        $archangel = new Archangel();
164
-        $archangel->addBCC('[email protected]', 'Mr. Test Alot');
165
-        $headersProperty = $this->getProtectedProperty('headers');
166
-        $headers = $headersProperty->getValue($archangel);
167
-
168
-        $this->assertArraySubset(
169
-            array('BCC' => array('"Mr. Test Alot" <[email protected]>')),
170
-            $headers
171
-        );
172
-    }
173
-
174
-    public function testSetFrom()
175
-    {
176
-        $archangel = new Archangel();
177
-        $archangel->setFrom('[email protected]');
178
-        $headersProperty = $this->getProtectedProperty('headers');
179
-        $headers = $headersProperty->getValue($archangel);
180
-
181
-        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
182
-    }
183
-
184
-    public function testSetFromMultiple()
185
-    {
186
-        $archangel = new Archangel();
187
-        $archangel->setFrom('[email protected]');
188
-        $archangel->setFrom('[email protected]');
189
-        $headersProperty = $this->getProtectedProperty('headers');
190
-        $headers = $headersProperty->getValue($archangel);
191
-
192
-        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
193
-        $this->assertNotContains('[email protected]', $headers);
194
-    }
195
-
196
-    public function testSetFromWithTitle()
197
-    {
198
-        $archangel = new Archangel();
199
-        $archangel->setFrom('[email protected]', 'Mr. Test Alot');
200
-        $headersProperty = $this->getProtectedProperty('headers');
201
-        $headers = $headersProperty->getValue($archangel);
202
-
203
-        $this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
204
-    }
205
-
206
-    public function testSetReplyTo()
207
-    {
208
-        $archangel = new Archangel();
209
-        $archangel->setReplyTo('[email protected]');
210
-        $headersProperty = $this->getProtectedProperty('headers');
211
-        $headers = $headersProperty->getValue($archangel);
212
-
213
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
214
-    }
215
-
216
-    public function testSetReplyToMultiple()
217
-    {
218
-        $archangel = new Archangel();
219
-        $archangel->setReplyTo('[email protected]');
220
-        $archangel->setReplyTo('[email protected]');
221
-        $headersProperty = $this->getProtectedProperty('headers');
222
-        $headers = $headersProperty->getValue($archangel);
223
-
224
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
225
-        $this->assertNotContains('[email protected]', $headers);
226
-    }
227
-
228
-    public function testSetReplyToWithTitle()
229
-    {
230
-        $archangel = new Archangel();
231
-        $archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
232
-        $headersProperty = $this->getProtectedProperty('headers');
233
-        $headers = $headersProperty->getValue($archangel);
234
-
235
-        $this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
236
-    }
237
-
238
-    public function testFormatEmailAddress()
239
-    {
240
-        $archangel = new Archangel();
241
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
242
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
243
-
244
-        $this->assertEquals('[email protected]', $formattedEmail);
245
-    }
246
-
247
-    public function testFormatEmailAddressWithTitle()
248
-    {
249
-        $archangel = new Archangel();
250
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
251
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
252
-
253
-        $this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
254
-    }
255
-
256
-    public function testSetSubject()
257
-    {
258
-        $archangel = new Archangel();
259
-        $archangel->setSubject('Test Subject');
260
-
261
-        $this->assertAttributeEquals('Test Subject', 'subject', $archangel);
262
-    }
263
-
264
-    public function testSetPlainMessage()
265
-    {
266
-        $archangel = new Archangel();
267
-        $archangel->setPlainMessage('Plain text message');
268
-
269
-        $this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
270
-    }
271
-
272
-    public function testSetHTMLMessage()
273
-    {
274
-        $archangel = new Archangel();
275
-        $archangel->setHTMLMessage('<p>An HTML message.</p>');
276
-
277
-        $this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
278
-    }
279
-
280
-    /**
281
-     * @dataProvider dataBuildHeaders
282
-     */
283
-    public function testBuildHeaders(
284
-        $expectedHeaders,
285
-        $headers,
286
-        $attachments,
287
-        $plainMessage,
288
-        $htmlMessage
289
-    ) {
290
-        $archangel = new Archangel();
291
-        $headersProperty = $this->getProtectedProperty('headers');
292
-        $headersProperty->setValue($archangel, $headers);
293
-
294
-        if (!empty($attachments)) {
295
-            $attachmentsProperty = $this->getProtectedProperty('attachments');
296
-            $attachmentsProperty->setValue($archangel, $attachments);
297
-        }
298
-
299
-        if (!empty($plainMessage)) {
300
-            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
301
-            $plainMessageProperty->setValue($archangel, $plainMessage);
302
-        }
303
-
304
-        if (!empty($htmlMessage)) {
305
-            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
306
-            $htmlMessageProperty->setValue($archangel, $htmlMessage);
307
-        }
308
-
309
-        $buildHeadersMethod = $this->getProtectedMethod('buildHeaders');
310
-        $builtHeaders = $buildHeadersMethod->invoke($archangel);
311
-
312
-        $this->assertEquals($expectedHeaders, $builtHeaders);
313
-    }
314
-
315
-    public function dataBuildHeaders()
316
-    {
317
-        return array(
318
-            array(
319
-                'expectedHeaders' =>
320
-                    "From: [email protected]\r\n" .
321
-                    "X-Mailer: PHP/6.0.0",
322
-                'headers' => array(
323
-                    'From' => '[email protected]',
324
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
325
-                ),
326
-                'attachments' => null,
327
-                'plainMessage' => true,
328
-                'htmlMessage' => null,
329
-            ),
330
-            array(
331
-                'expectedHeaders' =>
332
-                    "CC: [email protected], [email protected]\r\n" .
333
-                    "From: [email protected]\r\n" .
334
-                    "X-Mailer: PHP/6.0.0",
335
-                'headers' => array(
336
-                    'CC' => array('[email protected]', '[email protected]'),
337
-                    'From' => '[email protected]',
338
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
339
-                ),
340
-                'attachments' => null,
341
-                'plainMessage' => true,
342
-                'htmlMessage' => null,
343
-            ),
344
-            array(
345
-                'expectedHeaders' =>
346
-                    "BCC: [email protected], [email protected]\r\n" .
347
-                    "From: [email protected]\r\n" .
348
-                    "X-Mailer: PHP/6.0.0",
349
-                'headers' => array(
350
-                    'BCC' => array('[email protected]', '[email protected]'),
351
-                    'From' => '[email protected]',
352
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
353
-                ),
354
-                'attachments' => null,
355
-                'plainMessage' => true,
356
-                'htmlMessage' => null,
357
-            ),
358
-            array(
359
-                'expectedHeaders' =>
360
-                    "From: [email protected]\r\n" .
361
-                    "X-Mailer: PHP/6.0.0\r\n" .
362
-                    "Content-Type: multipart/mixed; boundary=\"PHP-mixed-1234567890123\"",
363
-                'headers' => array(
364
-                    'From' => '[email protected]',
365
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
366
-                ),
367
-                'attachments' => true,
368
-                'plainMessage' => true,
369
-                'htmlMessage' => null,
370
-            ),
371
-            array(
372
-                'expectedHeaders' =>
373
-                    "From: [email protected]\r\n" .
374
-                    "X-Mailer: PHP/6.0.0\r\n" .
375
-                    "Content-Type: multipart/alternative; boundary=\"PHP-alternative-1234567890123\"",
376
-                'headers' => array(
377
-                    'From' => '[email protected]',
378
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
379
-                ),
380
-                'attachments' => null,
381
-                'plainMessage' => true,
382
-                'htmlMessage' => true,
383
-            ),
384
-            array(
385
-                'expectedHeaders' =>
386
-                    "From: [email protected]\r\n" .
387
-                    "X-Mailer: PHP/6.0.0\r\n" .
388
-                    "Content-type: text/html; charset=\"iso-8859-1\"",
389
-                'headers' => array(
390
-                    'From' => '[email protected]',
391
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
392
-                ),
393
-                'attachments' => null,
394
-                'plainMessage' => null,
395
-                'htmlMessage' => true,
396
-            ),
397
-        );
398
-    }
399
-
400
-    public function testAddAttachment()
401
-    {
402
-        $archangel = new Archangel();
403
-        $archangel->addAttachment('path', 'type');
404
-
405
-        $this->assertAttributeContains(
406
-            array('path' => 'path', 'type' => 'type', 'title' => ''),
407
-            'attachments',
408
-            $archangel
409
-        );
410
-    }
411
-
412
-    public function testAddAttachmentMultiple()
413
-    {
414
-        $archangel = new Archangel();
415
-        $archangel->addAttachment('pathOne', 'typeOne');
416
-        $archangel->addAttachment('pathTwo', 'typeTwo');
417
-
418
-        $this->assertAttributeContains(
419
-            array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
420
-            'attachments',
421
-            $archangel
422
-        );
423
-        $this->assertAttributeContains(
424
-            array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
425
-            'attachments',
426
-            $archangel
427
-        );
428
-    }
429
-
430
-    public function testAddAttachmentWithTitle()
431
-    {
432
-        $archangel = new Archangel();
433
-        $archangel->addAttachment('path', 'type', 'title');
434
-
435
-        $this->assertAttributeContains(
436
-            array('path' => 'path', 'type' => 'type', 'title' => 'title'),
437
-            'attachments',
438
-            $archangel
439
-        );
440
-    }
441
-
442
-    public function testSend()
443
-    {
444
-        $archangel = new Archangel();
445
-        $archangel->addTo('[email protected]');
446
-        $archangel->setSubject('Test Subject');
447
-        $archangel->setPlainMessage('Plain text message');
448
-        $response = $archangel->send();
449
-
450
-        $expectedResponse = array(
451
-            'to' => '[email protected]',
452
-            'subject' => 'Test Subject',
453
-            'message' => 'Plain text message' . Archangel::LINE_BREAK,
454
-            'headers' => 'X-Mailer: PHP/6.0.0',
455
-        );
456
-
457
-        $this->assertEquals($expectedResponse, $response);
458
-    }
459
-
460
-    /**
461
-     * @dataProvider dataCheckRequiredFields
462
-     */
463
-    public function testCheckRequiredFields(
464
-        $expectedResult,
465
-        $toAddresses,
466
-        $subject,
467
-        $plainMessage,
468
-        $htmlMessage,
469
-        $attachments
470
-    ) {
471
-        $archangel = new Archangel();
472
-
473
-        if (!empty($toAddresses)) {
474
-            $toAddressesProperty = $this->getProtectedProperty('toAddresses');
475
-            $toAddressesProperty->setValue($archangel, $toAddresses);
476
-        }
477
-
478
-        if (!empty($subject)) {
479
-            $subjectProperty = $this->getProtectedProperty('subject');
480
-            $subjectProperty->setValue($archangel, $subject);
481
-        }
482
-
483
-        if (!empty($plainMessage)) {
484
-            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
485
-            $plainMessageProperty->setValue($archangel, $plainMessage);
486
-        }
487
-
488
-        if (!empty($htmlMessage)) {
489
-            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
490
-            $htmlMessageProperty->setValue($archangel, $htmlMessage);
491
-        }
492
-
493
-        if (!empty($attachments)) {
494
-            $attachmentsProperty = $this->getProtectedProperty('attachments');
495
-            $attachmentsProperty->setValue($archangel, $attachments);
496
-        }
497
-
498
-        $checkMethod = $this->getProtectedMethod('checkRequiredFields');
499
-        $isValid = $checkMethod->invoke($archangel);
500
-
501
-        if ($expectedResult == true) {
502
-            $this->assertTrue($isValid);
503
-            return;
504
-        }
505
-        $this->assertNotTrue($isValid);
506
-    }
507
-
508
-    public function dataCheckRequiredFields()
509
-    {
510
-        return array(
511
-            array(
512
-                'expectedResult' => false,
513
-                'toAddresses' => array(),
514
-                'subject' => '',
515
-                'plainMessage' => '',
516
-                'htmlMessage' => '',
517
-                'attachments' => array(),
518
-            ),
519
-            array(
520
-                'expectedResult' => false,
521
-                'toAddresses' => array('[email protected]'),
522
-                'subject' => '',
523
-                'plainMessage' => '',
524
-                'htmlMessage' => '',
525
-                'attachments' => array(),
526
-            ),
527
-            array(
528
-                'expectedResult' => false,
529
-                'toAddresses' => array('[email protected]'),
530
-                'subject' => 'Test Subject',
531
-                'plainMessage' => '',
532
-                'htmlMessage' => '',
533
-                'attachments' => array(),
534
-            ),
535
-            array(
536
-                'expectedResult' => false,
537
-                'toAddresses' => array(),
538
-                'subject' => 'Test Subject',
539
-                'plainMessage' => '',
540
-                'htmlMessage' => '',
541
-                'attachments' => array(),
542
-            ),
543
-            array(
544
-                'expectedResult' => false,
545
-                'toAddresses' => array(),
546
-                'subject' => 'Test Subject',
547
-                'plainMessage' => 'Plain text message',
548
-                'htmlMessage' => '',
549
-                'attachments' => array(),
550
-            ),
551
-            array(
552
-                'expectedResult' => true,
553
-                'toAddresses' => array('[email protected]'),
554
-                'subject' => 'Test Subject',
555
-                'plainMessage' => 'Plain text message',
556
-                'htmlMessage' => '',
557
-                'attachments' => array(),
558
-            ),
559
-            array(
560
-                'expectedResult' => true,
561
-                'toAddresses' => array('[email protected]'),
562
-                'subject' => 'Test Subject',
563
-                'plainMessage' => '',
564
-                'htmlMessage' => '<p>An HTML message.</p>',
565
-                'attachments' => array(),
566
-            ),
567
-            array(
568
-                'expectedResult' => true,
569
-                'toAddresses' => array('[email protected]'),
570
-                'subject' => 'Test Subject',
571
-                'plainMessage' => '',
572
-                'htmlMessage' => '',
573
-                'attachments' => array(
574
-                    array('path' => 'path', 'type' => 'type'),
575
-                ),
576
-            ),
577
-            array(
578
-                'expectedResult' => true,
579
-                'toAddresses' => array('[email protected]'),
580
-                'subject' => 'Test Subject',
581
-                'plainMessage' => 'Plain text message',
582
-                'htmlMessage' => '<p>An HTML message.</p>',
583
-                'attachments' => array(
584
-                    array('path' => 'path', 'type' => 'type'),
585
-                ),
586
-            ),
587
-       );
588
-    }
589
-
590
-    public function testBuildTo()
591
-    {
592
-        $archangel = new Archangel();
593
-        $addressesProperty = $this->getProtectedProperty('toAddresses');
594
-        $addressesProperty->setValue($archangel, array('[email protected]'));
595
-        $buildMethod = $this->getProtectedMethod('buildTo');
596
-        $toAddresses = $buildMethod->invoke($archangel);
597
-
598
-        $this->assertEquals('[email protected]', $toAddresses);
599
-    }
600
-
601
-    public function testBuildToMultiple()
602
-    {
603
-        $archangel = new Archangel();
604
-        $addressesProperty = $this->getProtectedProperty('toAddresses');
605
-        $addressesProperty->setValue($archangel, array('[email protected]', '[email protected]'));
606
-        $buildMethod = $this->getProtectedMethod('buildTo');
607
-        $toAddresses = $buildMethod->invoke($archangel);
608
-
609
-        $this->assertEquals('[email protected], [email protected]', $toAddresses);
610
-    }
611
-
612
-    protected function getProtectedProperty($property)
613
-    {
614
-        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
615
-        $reflectedProperty = $reflectedArchangel->getProperty($property);
616
-        $reflectedProperty->setAccessible(true);
617
-
618
-        return $reflectedProperty;
619
-    }
620
-
621
-    protected function getProtectedMethod($method)
622
-    {
623
-        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
624
-        $reflectedMethod = $reflectedArchangel->getMethod($method);
625
-        $reflectedMethod->setAccessible(true);
626
-
627
-        return $reflectedMethod;
628
-    }
11
+	public function testIsInstanceOfArchangel()
12
+	{
13
+		$archangel = new Archangel();
14
+
15
+		$this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
16
+	}
17
+
18
+	public function testIsLoggerAwareInterface()
19
+	{
20
+		$archangel = new Archangel();
21
+
22
+		$this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel);
23
+	}
24
+
25
+	public function testConstructSetsDefaultMailer()
26
+	{
27
+		$archangel = new Archangel();
28
+		$mailer = sprintf('PHP/%s', phpversion());
29
+		$headers = array('X-Mailer' => $mailer);
30
+
31
+		$this->assertAttributeEquals($headers, 'headers', $archangel);
32
+	}
33
+
34
+	public function testConstructOverridesMailer()
35
+	{
36
+		$archangel = new Archangel('AwesomeMailer');
37
+		$headers = array('X-Mailer' => 'AwesomeMailer');
38
+
39
+		$this->assertAttributeEquals($headers, 'headers', $archangel);
40
+	}
41
+
42
+	public function testConstructSetsNullLogger()
43
+	{
44
+		$archangel = new Archangel();
45
+
46
+		$this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
47
+	}
48
+
49
+	public function testConstructSetsBoundaries()
50
+	{
51
+		$archangel = new Archangel();
52
+		$expectedBoundaryMixed = sprintf('PHP-mixed-%s', uniqid());
53
+		$expectedBoundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
54
+
55
+		$this->assertAttributeEquals($expectedBoundaryMixed, 'boundaryMixed', $archangel);
56
+		$this->assertAttributeEquals($expectedBoundaryAlternative, 'boundaryAlternative', $archangel);
57
+	}
58
+
59
+	public function testSetLogger()
60
+	{
61
+		$logger = $this->getMock('Psr\Log\LoggerInterface');
62
+		$archangel = new Archangel();
63
+		$archangel->setLogger($logger);
64
+
65
+		$this->assertAttributeSame($logger, 'logger', $archangel);
66
+	}
67
+
68
+	public function testAddTo()
69
+	{
70
+		$archangel = new Archangel();
71
+		$archangel->addTo('[email protected]');
72
+
73
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
74
+	}
75
+
76
+	public function testAddToMultiple()
77
+	{
78
+		$archangel = new Archangel();
79
+		$archangel->addTo('[email protected]');
80
+		$archangel->addTo('[email protected]');
81
+
82
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
83
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
84
+	}
85
+
86
+	public function testAddToWithTitle()
87
+	{
88
+		$archangel = new Archangel();
89
+		$archangel->addTo('[email protected]', 'Mr. Test Alot');
90
+
91
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
92
+	}
93
+
94
+	public function testAddCC()
95
+	{
96
+		$archangel = new Archangel();
97
+		$archangel->addCC('[email protected]');
98
+		$headersProperty = $this->getProtectedProperty('headers');
99
+		$headers = $headersProperty->getValue($archangel);
100
+
101
+		$this->assertArraySubset(
102
+			array('CC' => array('[email protected]')),
103
+			$headers
104
+		);
105
+	}
106
+
107
+	public function testAddCCMultiple()
108
+	{
109
+		$archangel = new Archangel();
110
+		$archangel->addCC('[email protected]');
111
+		$archangel->addCC('[email protected]');
112
+		$headersProperty = $this->getProtectedProperty('headers');
113
+		$headers = $headersProperty->getValue($archangel);
114
+
115
+		$this->assertArraySubset(
116
+			array('CC' => array('[email protected]', '[email protected]')),
117
+			$headers
118
+		);
119
+	}
120
+
121
+	public function testAddCCWithTitle()
122
+	{
123
+		$archangel = new Archangel();
124
+		$archangel->addCC('[email protected]', 'Mr. Test Alot');
125
+		$headersProperty = $this->getProtectedProperty('headers');
126
+		$headers = $headersProperty->getValue($archangel);
127
+
128
+		$this->assertArraySubset(
129
+			array('CC' => array('"Mr. Test Alot" <[email protected]>')),
130
+			$headers
131
+		);
132
+	}
133
+
134
+	public function testAddBCC()
135
+	{
136
+		$archangel = new Archangel();
137
+		$archangel->addBCC('[email protected]');
138
+		$headersProperty = $this->getProtectedProperty('headers');
139
+		$headers = $headersProperty->getValue($archangel);
140
+
141
+		$this->assertArraySubset(
142
+			array('BCC' => array('[email protected]')),
143
+			$headers
144
+		);
145
+	}
146
+
147
+	public function testAddBCCMultiple()
148
+	{
149
+		$archangel = new Archangel();
150
+		$archangel->addBCC('[email protected]');
151
+		$archangel->addBCC('[email protected]');
152
+		$headersProperty = $this->getProtectedProperty('headers');
153
+		$headers = $headersProperty->getValue($archangel);
154
+
155
+		$this->assertArraySubset(
156
+			array('BCC' => array('[email protected]', '[email protected]')),
157
+			$headers
158
+		);
159
+	}
160
+
161
+	public function testAddBCCWithTitle()
162
+	{
163
+		$archangel = new Archangel();
164
+		$archangel->addBCC('[email protected]', 'Mr. Test Alot');
165
+		$headersProperty = $this->getProtectedProperty('headers');
166
+		$headers = $headersProperty->getValue($archangel);
167
+
168
+		$this->assertArraySubset(
169
+			array('BCC' => array('"Mr. Test Alot" <[email protected]>')),
170
+			$headers
171
+		);
172
+	}
173
+
174
+	public function testSetFrom()
175
+	{
176
+		$archangel = new Archangel();
177
+		$archangel->setFrom('[email protected]');
178
+		$headersProperty = $this->getProtectedProperty('headers');
179
+		$headers = $headersProperty->getValue($archangel);
180
+
181
+		$this->assertArraySubset(array('From' => '[email protected]'), $headers);
182
+	}
183
+
184
+	public function testSetFromMultiple()
185
+	{
186
+		$archangel = new Archangel();
187
+		$archangel->setFrom('[email protected]');
188
+		$archangel->setFrom('[email protected]');
189
+		$headersProperty = $this->getProtectedProperty('headers');
190
+		$headers = $headersProperty->getValue($archangel);
191
+
192
+		$this->assertArraySubset(array('From' => '[email protected]'), $headers);
193
+		$this->assertNotContains('[email protected]', $headers);
194
+	}
195
+
196
+	public function testSetFromWithTitle()
197
+	{
198
+		$archangel = new Archangel();
199
+		$archangel->setFrom('[email protected]', 'Mr. Test Alot');
200
+		$headersProperty = $this->getProtectedProperty('headers');
201
+		$headers = $headersProperty->getValue($archangel);
202
+
203
+		$this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
204
+	}
205
+
206
+	public function testSetReplyTo()
207
+	{
208
+		$archangel = new Archangel();
209
+		$archangel->setReplyTo('[email protected]');
210
+		$headersProperty = $this->getProtectedProperty('headers');
211
+		$headers = $headersProperty->getValue($archangel);
212
+
213
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
214
+	}
215
+
216
+	public function testSetReplyToMultiple()
217
+	{
218
+		$archangel = new Archangel();
219
+		$archangel->setReplyTo('[email protected]');
220
+		$archangel->setReplyTo('[email protected]');
221
+		$headersProperty = $this->getProtectedProperty('headers');
222
+		$headers = $headersProperty->getValue($archangel);
223
+
224
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
225
+		$this->assertNotContains('[email protected]', $headers);
226
+	}
227
+
228
+	public function testSetReplyToWithTitle()
229
+	{
230
+		$archangel = new Archangel();
231
+		$archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
232
+		$headersProperty = $this->getProtectedProperty('headers');
233
+		$headers = $headersProperty->getValue($archangel);
234
+
235
+		$this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
236
+	}
237
+
238
+	public function testFormatEmailAddress()
239
+	{
240
+		$archangel = new Archangel();
241
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
242
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
243
+
244
+		$this->assertEquals('[email protected]', $formattedEmail);
245
+	}
246
+
247
+	public function testFormatEmailAddressWithTitle()
248
+	{
249
+		$archangel = new Archangel();
250
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
251
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
252
+
253
+		$this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
254
+	}
255
+
256
+	public function testSetSubject()
257
+	{
258
+		$archangel = new Archangel();
259
+		$archangel->setSubject('Test Subject');
260
+
261
+		$this->assertAttributeEquals('Test Subject', 'subject', $archangel);
262
+	}
263
+
264
+	public function testSetPlainMessage()
265
+	{
266
+		$archangel = new Archangel();
267
+		$archangel->setPlainMessage('Plain text message');
268
+
269
+		$this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
270
+	}
271
+
272
+	public function testSetHTMLMessage()
273
+	{
274
+		$archangel = new Archangel();
275
+		$archangel->setHTMLMessage('<p>An HTML message.</p>');
276
+
277
+		$this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
278
+	}
279
+
280
+	/**
281
+	 * @dataProvider dataBuildHeaders
282
+	 */
283
+	public function testBuildHeaders(
284
+		$expectedHeaders,
285
+		$headers,
286
+		$attachments,
287
+		$plainMessage,
288
+		$htmlMessage
289
+	) {
290
+		$archangel = new Archangel();
291
+		$headersProperty = $this->getProtectedProperty('headers');
292
+		$headersProperty->setValue($archangel, $headers);
293
+
294
+		if (!empty($attachments)) {
295
+			$attachmentsProperty = $this->getProtectedProperty('attachments');
296
+			$attachmentsProperty->setValue($archangel, $attachments);
297
+		}
298
+
299
+		if (!empty($plainMessage)) {
300
+			$plainMessageProperty = $this->getProtectedProperty('plainMessage');
301
+			$plainMessageProperty->setValue($archangel, $plainMessage);
302
+		}
303
+
304
+		if (!empty($htmlMessage)) {
305
+			$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
306
+			$htmlMessageProperty->setValue($archangel, $htmlMessage);
307
+		}
308
+
309
+		$buildHeadersMethod = $this->getProtectedMethod('buildHeaders');
310
+		$builtHeaders = $buildHeadersMethod->invoke($archangel);
311
+
312
+		$this->assertEquals($expectedHeaders, $builtHeaders);
313
+	}
314
+
315
+	public function dataBuildHeaders()
316
+	{
317
+		return array(
318
+			array(
319
+				'expectedHeaders' =>
320
+					"From: [email protected]\r\n" .
321
+					"X-Mailer: PHP/6.0.0",
322
+				'headers' => array(
323
+					'From' => '[email protected]',
324
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
325
+				),
326
+				'attachments' => null,
327
+				'plainMessage' => true,
328
+				'htmlMessage' => null,
329
+			),
330
+			array(
331
+				'expectedHeaders' =>
332
+					"CC: [email protected], [email protected]\r\n" .
333
+					"From: [email protected]\r\n" .
334
+					"X-Mailer: PHP/6.0.0",
335
+				'headers' => array(
336
+					'CC' => array('[email protected]', '[email protected]'),
337
+					'From' => '[email protected]',
338
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
339
+				),
340
+				'attachments' => null,
341
+				'plainMessage' => true,
342
+				'htmlMessage' => null,
343
+			),
344
+			array(
345
+				'expectedHeaders' =>
346
+					"BCC: [email protected], [email protected]\r\n" .
347
+					"From: [email protected]\r\n" .
348
+					"X-Mailer: PHP/6.0.0",
349
+				'headers' => array(
350
+					'BCC' => array('[email protected]', '[email protected]'),
351
+					'From' => '[email protected]',
352
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
353
+				),
354
+				'attachments' => null,
355
+				'plainMessage' => true,
356
+				'htmlMessage' => null,
357
+			),
358
+			array(
359
+				'expectedHeaders' =>
360
+					"From: [email protected]\r\n" .
361
+					"X-Mailer: PHP/6.0.0\r\n" .
362
+					"Content-Type: multipart/mixed; boundary=\"PHP-mixed-1234567890123\"",
363
+				'headers' => array(
364
+					'From' => '[email protected]',
365
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
366
+				),
367
+				'attachments' => true,
368
+				'plainMessage' => true,
369
+				'htmlMessage' => null,
370
+			),
371
+			array(
372
+				'expectedHeaders' =>
373
+					"From: [email protected]\r\n" .
374
+					"X-Mailer: PHP/6.0.0\r\n" .
375
+					"Content-Type: multipart/alternative; boundary=\"PHP-alternative-1234567890123\"",
376
+				'headers' => array(
377
+					'From' => '[email protected]',
378
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
379
+				),
380
+				'attachments' => null,
381
+				'plainMessage' => true,
382
+				'htmlMessage' => true,
383
+			),
384
+			array(
385
+				'expectedHeaders' =>
386
+					"From: [email protected]\r\n" .
387
+					"X-Mailer: PHP/6.0.0\r\n" .
388
+					"Content-type: text/html; charset=\"iso-8859-1\"",
389
+				'headers' => array(
390
+					'From' => '[email protected]',
391
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
392
+				),
393
+				'attachments' => null,
394
+				'plainMessage' => null,
395
+				'htmlMessage' => true,
396
+			),
397
+		);
398
+	}
399
+
400
+	public function testAddAttachment()
401
+	{
402
+		$archangel = new Archangel();
403
+		$archangel->addAttachment('path', 'type');
404
+
405
+		$this->assertAttributeContains(
406
+			array('path' => 'path', 'type' => 'type', 'title' => ''),
407
+			'attachments',
408
+			$archangel
409
+		);
410
+	}
411
+
412
+	public function testAddAttachmentMultiple()
413
+	{
414
+		$archangel = new Archangel();
415
+		$archangel->addAttachment('pathOne', 'typeOne');
416
+		$archangel->addAttachment('pathTwo', 'typeTwo');
417
+
418
+		$this->assertAttributeContains(
419
+			array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
420
+			'attachments',
421
+			$archangel
422
+		);
423
+		$this->assertAttributeContains(
424
+			array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
425
+			'attachments',
426
+			$archangel
427
+		);
428
+	}
429
+
430
+	public function testAddAttachmentWithTitle()
431
+	{
432
+		$archangel = new Archangel();
433
+		$archangel->addAttachment('path', 'type', 'title');
434
+
435
+		$this->assertAttributeContains(
436
+			array('path' => 'path', 'type' => 'type', 'title' => 'title'),
437
+			'attachments',
438
+			$archangel
439
+		);
440
+	}
441
+
442
+	public function testSend()
443
+	{
444
+		$archangel = new Archangel();
445
+		$archangel->addTo('[email protected]');
446
+		$archangel->setSubject('Test Subject');
447
+		$archangel->setPlainMessage('Plain text message');
448
+		$response = $archangel->send();
449
+
450
+		$expectedResponse = array(
451
+			'to' => '[email protected]',
452
+			'subject' => 'Test Subject',
453
+			'message' => 'Plain text message' . Archangel::LINE_BREAK,
454
+			'headers' => 'X-Mailer: PHP/6.0.0',
455
+		);
456
+
457
+		$this->assertEquals($expectedResponse, $response);
458
+	}
459
+
460
+	/**
461
+	 * @dataProvider dataCheckRequiredFields
462
+	 */
463
+	public function testCheckRequiredFields(
464
+		$expectedResult,
465
+		$toAddresses,
466
+		$subject,
467
+		$plainMessage,
468
+		$htmlMessage,
469
+		$attachments
470
+	) {
471
+		$archangel = new Archangel();
472
+
473
+		if (!empty($toAddresses)) {
474
+			$toAddressesProperty = $this->getProtectedProperty('toAddresses');
475
+			$toAddressesProperty->setValue($archangel, $toAddresses);
476
+		}
477
+
478
+		if (!empty($subject)) {
479
+			$subjectProperty = $this->getProtectedProperty('subject');
480
+			$subjectProperty->setValue($archangel, $subject);
481
+		}
482
+
483
+		if (!empty($plainMessage)) {
484
+			$plainMessageProperty = $this->getProtectedProperty('plainMessage');
485
+			$plainMessageProperty->setValue($archangel, $plainMessage);
486
+		}
487
+
488
+		if (!empty($htmlMessage)) {
489
+			$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
490
+			$htmlMessageProperty->setValue($archangel, $htmlMessage);
491
+		}
492
+
493
+		if (!empty($attachments)) {
494
+			$attachmentsProperty = $this->getProtectedProperty('attachments');
495
+			$attachmentsProperty->setValue($archangel, $attachments);
496
+		}
497
+
498
+		$checkMethod = $this->getProtectedMethod('checkRequiredFields');
499
+		$isValid = $checkMethod->invoke($archangel);
500
+
501
+		if ($expectedResult == true) {
502
+			$this->assertTrue($isValid);
503
+			return;
504
+		}
505
+		$this->assertNotTrue($isValid);
506
+	}
507
+
508
+	public function dataCheckRequiredFields()
509
+	{
510
+		return array(
511
+			array(
512
+				'expectedResult' => false,
513
+				'toAddresses' => array(),
514
+				'subject' => '',
515
+				'plainMessage' => '',
516
+				'htmlMessage' => '',
517
+				'attachments' => array(),
518
+			),
519
+			array(
520
+				'expectedResult' => false,
521
+				'toAddresses' => array('[email protected]'),
522
+				'subject' => '',
523
+				'plainMessage' => '',
524
+				'htmlMessage' => '',
525
+				'attachments' => array(),
526
+			),
527
+			array(
528
+				'expectedResult' => false,
529
+				'toAddresses' => array('[email protected]'),
530
+				'subject' => 'Test Subject',
531
+				'plainMessage' => '',
532
+				'htmlMessage' => '',
533
+				'attachments' => array(),
534
+			),
535
+			array(
536
+				'expectedResult' => false,
537
+				'toAddresses' => array(),
538
+				'subject' => 'Test Subject',
539
+				'plainMessage' => '',
540
+				'htmlMessage' => '',
541
+				'attachments' => array(),
542
+			),
543
+			array(
544
+				'expectedResult' => false,
545
+				'toAddresses' => array(),
546
+				'subject' => 'Test Subject',
547
+				'plainMessage' => 'Plain text message',
548
+				'htmlMessage' => '',
549
+				'attachments' => array(),
550
+			),
551
+			array(
552
+				'expectedResult' => true,
553
+				'toAddresses' => array('[email protected]'),
554
+				'subject' => 'Test Subject',
555
+				'plainMessage' => 'Plain text message',
556
+				'htmlMessage' => '',
557
+				'attachments' => array(),
558
+			),
559
+			array(
560
+				'expectedResult' => true,
561
+				'toAddresses' => array('[email protected]'),
562
+				'subject' => 'Test Subject',
563
+				'plainMessage' => '',
564
+				'htmlMessage' => '<p>An HTML message.</p>',
565
+				'attachments' => array(),
566
+			),
567
+			array(
568
+				'expectedResult' => true,
569
+				'toAddresses' => array('[email protected]'),
570
+				'subject' => 'Test Subject',
571
+				'plainMessage' => '',
572
+				'htmlMessage' => '',
573
+				'attachments' => array(
574
+					array('path' => 'path', 'type' => 'type'),
575
+				),
576
+			),
577
+			array(
578
+				'expectedResult' => true,
579
+				'toAddresses' => array('[email protected]'),
580
+				'subject' => 'Test Subject',
581
+				'plainMessage' => 'Plain text message',
582
+				'htmlMessage' => '<p>An HTML message.</p>',
583
+				'attachments' => array(
584
+					array('path' => 'path', 'type' => 'type'),
585
+				),
586
+			),
587
+	   );
588
+	}
589
+
590
+	public function testBuildTo()
591
+	{
592
+		$archangel = new Archangel();
593
+		$addressesProperty = $this->getProtectedProperty('toAddresses');
594
+		$addressesProperty->setValue($archangel, array('[email protected]'));
595
+		$buildMethod = $this->getProtectedMethod('buildTo');
596
+		$toAddresses = $buildMethod->invoke($archangel);
597
+
598
+		$this->assertEquals('[email protected]', $toAddresses);
599
+	}
600
+
601
+	public function testBuildToMultiple()
602
+	{
603
+		$archangel = new Archangel();
604
+		$addressesProperty = $this->getProtectedProperty('toAddresses');
605
+		$addressesProperty->setValue($archangel, array('[email protected]', '[email protected]'));
606
+		$buildMethod = $this->getProtectedMethod('buildTo');
607
+		$toAddresses = $buildMethod->invoke($archangel);
608
+
609
+		$this->assertEquals('[email protected], [email protected]', $toAddresses);
610
+	}
611
+
612
+	protected function getProtectedProperty($property)
613
+	{
614
+		$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
615
+		$reflectedProperty = $reflectedArchangel->getProperty($property);
616
+		$reflectedProperty->setAccessible(true);
617
+
618
+		return $reflectedProperty;
619
+	}
620
+
621
+	protected function getProtectedMethod($method)
622
+	{
623
+		$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
624
+		$reflectedMethod = $reflectedArchangel->getMethod($method);
625
+		$reflectedMethod->setAccessible(true);
626
+
627
+		return $reflectedMethod;
628
+	}
629 629
 }
Please login to merge, or discard this patch.