Completed
Push — master ( becc57...d128ac )
by Jacob
10:02
created
src/Archangel.php 2 patches
Doc Comments   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
      * @param string $address email address for the recipient
84 84
      * @param string $title   name of the recipient (optional)
85 85
 
86
-     * @return object instantiated $this
86
+     * @return Archangel instantiated $this
87 87
      */
88 88
     public function addTo($address, $title = '')
89 89
     {
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
      * @param string $address email address for the cc recipient
102 102
      * @param string $title   name of the cc recipient (optional)
103 103
      *
104
-     * @return object instantiated $this
104
+     * @return Archangel instantiated $this
105 105
      */
106 106
     public function addCC($address, $title = '')
107 107
     {
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
      * @param string $address email address for the bcc recipient
120 120
      * @param string $title   name of the bcc recipient (optional)
121 121
      *
122
-     * @return object instantiated $this
122
+     * @return Archangel instantiated $this
123 123
      */
124 124
     public function addBCC($address, $title = '')
125 125
     {
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
      * @param string $address email address for the sender
138 138
      * @param string $title   name of the sender (optional)
139 139
      *
140
-     * @return object instantiated $this
140
+     * @return Archangel instantiated $this
141 141
      */
142 142
     public function setFrom($address, $title = '')
143 143
     {
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
      * @param string $address email address for the reply-to
156 156
      * @param string $title   name of the reply-to (optional)
157 157
      *
158
-     * @return object instantiated $this
158
+     * @return Archangel instantiated $this
159 159
      */
160 160
     public function setReplyTo($address, $title = '')
161 161
     {
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
      *
173 173
      * @param string $subject subject for the email
174 174
      *
175
-     * @return object instantiated $this
175
+     * @return Archangel instantiated $this
176 176
      */
177 177
     public function setSubject($subject)
178 178
     {
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
      *
187 187
      * @param string $message the plain-text message
188 188
      *
189
-     * @return object instantiated $this
189
+     * @return Archangel instantiated $this
190 190
      */
191 191
     public function setPlainMessage($message)
192 192
     {
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
      *
201 201
      * @param string $message the html message
202 202
      *
203
-     * @return object instantiated $this
203
+     * @return Archangel instantiated $this
204 204
      */
205 205
     public function setHTMLMessage($message)
206 206
     {
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
      * @param string $type  mime type of the file
217 217
      * @param string $title the title of the attachment (optional)
218 218
      *
219
-     * @return object instantiated $this
219
+     * @return Archangel instantiated $this
220 220
      */
221 221
     public function addAttachment($path, $type, $title = '')
222 222
     {
Please login to merge, or discard this patch.
Indentation   +411 added lines, -412 removed lines patch added patch discarded remove patch
@@ -16,418 +16,417 @@
 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 $ccAddresses */
26
-    protected $ccAddresses = array();
27
-
28
-    /** @var array $bccAddresses */
29
-    protected $bccAddresses = array();
30
-
31
-    /** @var array $headers */
32
-    protected $headers = array();
33
-
34
-    /** @var string $plainMessage */
35
-    protected $plainMessage;
36
-
37
-    /** @var string $htmlMessage */
38
-    protected $htmlMessage;
39
-
40
-    /** @var array $attachments */
41
-    protected $attachments = array();
42
-
43
-    /** @var string $boundary */
44
-    protected $boundary;
45
-
46
-    /** @var string $alternativeBoundary */
47
-    protected $alternativeBoundary;
48
-
49
-    /** @var LoggerInterface */
50
-    protected $logger;
51
-
52
-    /** @var string LINE_BREAK */
53
-    const LINE_BREAK = "\r\n";
54
-
55
-    /**
56
-     * @param string $mailer
57
-     */
58
-    public function __construct($mailer = null)
59
-    {
60
-        if (is_null($mailer)) {
61
-            $mailer = sprintf('PHP/%s', phpversion());
62
-        }
63
-        $this->headers['X-Mailer'] = $mailer;
64
-
65
-        $this->logger = new NullLogger();
66
-    }
67
-
68
-    /**
69
-     * @param LoggerInterface $logger
70
-     *
71
-     * @return $this;
72
-     */
73
-    public function setLogger(LoggerInterface $logger)
74
-    {
75
-        $this->logger = $logger;
76
-
77
-        return $this;
78
-    }
79
-
80
-    /**
81
-     * Setter method for adding recipients
82
-     *
83
-     * @param string $address email address for the recipient
84
-     * @param string $title   name of the recipient (optional)
85
-
86
-     * @return object instantiated $this
87
-     */
88
-    public function addTo($address, $title = '')
89
-    {
90
-        array_push(
91
-            $this->toAddresses,
92
-            $this->formatEmailAddress($address, $title)
93
-        );
94
-
95
-        return $this;
96
-    }
97
-
98
-    /**
99
-     * Setter method for adding cc recipients
100
-     *
101
-     * @param string $address email address for the cc recipient
102
-     * @param string $title   name of the cc recipient (optional)
103
-     *
104
-     * @return object instantiated $this
105
-     */
106
-    public function addCC($address, $title = '')
107
-    {
108
-        array_push(
109
-            $this->ccAddresses,
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
-        array_push(
127
-            $this->bccAddresses,
128
-            $this->formatEmailAddress($address, $title)
129
-        );
130
-
131
-        return $this;
132
-    }
133
-
134
-    /**
135
-     * Setter method for setting the single 'from' field
136
-     *
137
-     * @param string $address email address for the sender
138
-     * @param string $title   name of the sender (optional)
139
-     *
140
-     * @return object instantiated $this
141
-     */
142
-    public function setFrom($address, $title = '')
143
-    {
144
-        $this->headers['From'] = $this->formatEmailAddress($address, $title);
145
-
146
-        return $this;
147
-    }
148
-
149
-    /**
150
-     * Setter method for setting the single 'reply-to' field
151
-     *
152
-     * @param string $address email address for the reply-to
153
-     * @param string $title   name of the reply-to (optional)
154
-     *
155
-     * @return object instantiated $this
156
-     */
157
-    public function setReplyTo($address, $title = '')
158
-    {
159
-        $this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
160
-
161
-        return $this;
162
-    }
163
-
164
-    /**
165
-     * @param string $address
166
-     * @param string $title
167
-     *
168
-     * @return string
169
-     */
170
-    protected function formatEmailAddress($address, $title)
171
-    {
172
-        if (!empty($title)) {
173
-            $address = sprintf('"%s" <%s>', $title, $address);
174
-        }
175
-        return $address;
176
-    }
177
-
178
-    /**
179
-     * Setter method for setting a subject
180
-     *
181
-     * @param string $subject subject for the email
182
-     *
183
-     * @return object instantiated $this
184
-     */
185
-    public function setSubject($subject)
186
-    {
187
-        $this->subject = $subject;
188
-
189
-        return $this;
190
-    }
191
-
192
-    /**
193
-     * Setter method for the plain text message
194
-     *
195
-     * @param string $message the plain-text message
196
-     *
197
-     * @return object instantiated $this
198
-     */
199
-    public function setPlainMessage($message)
200
-    {
201
-        $this->plainMessage = $message;
202
-
203
-        return $this;
204
-    }
205
-
206
-    /**
207
-     * Setter method for the html message
208
-     *
209
-     * @param string $message the html message
210
-     *
211
-     * @return object instantiated $this
212
-     */
213
-    public function setHTMLMessage($message)
214
-    {
215
-        $this->htmlMessage = $message;
216
-
217
-        return $this;
218
-    }
219
-
220
-    /**
221
-     * Setter method for adding attachments
222
-     *
223
-     * @param string $path  the full path of the attachment
224
-     * @param string $type  mime type of the file
225
-     * @param string $title the title of the attachment (optional)
226
-     *
227
-     * @return object instantiated $this
228
-     */
229
-    public function addAttachment($path, $type, $title = '')
230
-    {
231
-        array_push($this->attachments, array(
232
-          'path' => $path,
233
-          'type' => $type,
234
-          'title' => $title,
235
-        ));
236
-
237
-        return $this;
238
-    }
239
-
240
-    /**
241
-     * The executing step, the actual sending of the email
242
-     * First checks to make sure the minimum fields are set (returns false if they are not)
243
-     * Second it attempts to send the mail with php's mail() (returns false if it fails)
244
-     *
245
-     * return boolean whether or not the email was valid & sent
246
-     */
247
-    public function send()
248
-    {
249
-        if (!$this->checkRequiredFields()) {
250
-            return false;
251
-        }
252
-
253
-        $recipients = $this->buildTo();
254
-        $subject = $this->subject;
255
-        $message = $this->buildMessage();
256
-        $headers = $this->buildHeaders();
257
-
258
-        return mail($recipients, $subject, $message, $headers);
259
-    }
260
-
261
-    /**
262
-     * Call to check the minimum required fields
263
-     *
264
-     * @return boolean whether or not the email meets the minimum required fields
265
-     */
266
-    protected function checkRequiredFields()
267
-    {
268
-        if (empty($this->toAddresses)) {
269
-            return false;
270
-        }
271
-        if (empty($this->subject)) {
272
-            return false;
273
-        }
274
-
275
-        if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
276
-            return false;
277
-        }
278
-
279
-        return true;
280
-    }
281
-
282
-    /**
283
-     * Build the recipients from 'to'
284
-     *
285
-     * @return string comma-separated lit of recipients
286
-     */
287
-    protected function buildTo()
288
-    {
289
-        return implode(', ', $this->toAddresses);
290
-    }
291
-
292
-    /**
293
-     * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see
294
-     *
295
-     * @return string email message
296
-     */
297
-    protected function buildMessage()
298
-    {
299
-        $messageString = '';
19
+	/** @var string $subject */
20
+	protected $subject;
21
+
22
+	/** @var array $toAddresses */
23
+	protected $toAddresses = array();
24
+
25
+	/** @var array $ccAddresses */
26
+	protected $ccAddresses = array();
27
+
28
+	/** @var array $bccAddresses */
29
+	protected $bccAddresses = array();
30
+
31
+	/** @var array $headers */
32
+	protected $headers = array();
33
+
34
+	/** @var string $plainMessage */
35
+	protected $plainMessage;
36
+
37
+	/** @var string $htmlMessage */
38
+	protected $htmlMessage;
39
+
40
+	/** @var array $attachments */
41
+	protected $attachments = array();
42
+
43
+	/** @var string $boundary */
44
+	protected $boundary;
45
+
46
+	/** @var string $alternativeBoundary */
47
+	protected $alternativeBoundary;
48
+
49
+	/** @var LoggerInterface */
50
+	protected $logger;
51
+
52
+	/** @var string LINE_BREAK */
53
+	const LINE_BREAK = "\r\n";
54
+
55
+	/**
56
+	 * @param string $mailer
57
+	 */
58
+	public function __construct($mailer = null)
59
+	{
60
+		if (is_null($mailer)) {
61
+			$mailer = sprintf('PHP/%s', phpversion());
62
+		}
63
+		$this->headers['X-Mailer'] = $mailer;
64
+
65
+		$this->logger = new NullLogger();
66
+	}
67
+
68
+	/**
69
+	 * @param LoggerInterface $logger
70
+	 *
71
+	 * @return $this;
72
+	 */
73
+	public function setLogger(LoggerInterface $logger)
74
+	{
75
+		$this->logger = $logger;
76
+
77
+		return $this;
78
+	}
79
+
80
+	/**
81
+	 * Setter method for adding recipients
82
+	 *
83
+	 * @param string $address email address for the recipient
84
+	 * @param string $title   name of the recipient (optional)
85
+	 * @return object instantiated $this
86
+	 */
87
+	public function addTo($address, $title = '')
88
+	{
89
+		array_push(
90
+			$this->toAddresses,
91
+			$this->formatEmailAddress($address, $title)
92
+		);
93
+
94
+		return $this;
95
+	}
96
+
97
+	/**
98
+	 * Setter method for adding cc recipients
99
+	 *
100
+	 * @param string $address email address for the cc recipient
101
+	 * @param string $title   name of the cc recipient (optional)
102
+	 *
103
+	 * @return object instantiated $this
104
+	 */
105
+	public function addCC($address, $title = '')
106
+	{
107
+		array_push(
108
+			$this->ccAddresses,
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
+		array_push(
126
+			$this->bccAddresses,
127
+			$this->formatEmailAddress($address, $title)
128
+		);
129
+
130
+		return $this;
131
+	}
132
+
133
+	/**
134
+	 * Setter method for setting the single 'from' field
135
+	 *
136
+	 * @param string $address email address for the sender
137
+	 * @param string $title   name of the sender (optional)
138
+	 *
139
+	 * @return object instantiated $this
140
+	 */
141
+	public function setFrom($address, $title = '')
142
+	{
143
+		$this->headers['From'] = $this->formatEmailAddress($address, $title);
144
+
145
+		return $this;
146
+	}
147
+
148
+	/**
149
+	 * Setter method for setting the single 'reply-to' field
150
+	 *
151
+	 * @param string $address email address for the reply-to
152
+	 * @param string $title   name of the reply-to (optional)
153
+	 *
154
+	 * @return object instantiated $this
155
+	 */
156
+	public function setReplyTo($address, $title = '')
157
+	{
158
+		$this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
159
+
160
+		return $this;
161
+	}
162
+
163
+	/**
164
+	 * @param string $address
165
+	 * @param string $title
166
+	 *
167
+	 * @return string
168
+	 */
169
+	protected function formatEmailAddress($address, $title)
170
+	{
171
+		if (!empty($title)) {
172
+			$address = sprintf('"%s" <%s>', $title, $address);
173
+		}
174
+		return $address;
175
+	}
176
+
177
+	/**
178
+	 * Setter method for setting a subject
179
+	 *
180
+	 * @param string $subject subject for the email
181
+	 *
182
+	 * @return object instantiated $this
183
+	 */
184
+	public function setSubject($subject)
185
+	{
186
+		$this->subject = $subject;
187
+
188
+		return $this;
189
+	}
190
+
191
+	/**
192
+	 * Setter method for the plain text message
193
+	 *
194
+	 * @param string $message the plain-text message
195
+	 *
196
+	 * @return object instantiated $this
197
+	 */
198
+	public function setPlainMessage($message)
199
+	{
200
+		$this->plainMessage = $message;
201
+
202
+		return $this;
203
+	}
204
+
205
+	/**
206
+	 * Setter method for the html message
207
+	 *
208
+	 * @param string $message the html message
209
+	 *
210
+	 * @return object instantiated $this
211
+	 */
212
+	public function setHTMLMessage($message)
213
+	{
214
+		$this->htmlMessage = $message;
215
+
216
+		return $this;
217
+	}
218
+
219
+	/**
220
+	 * Setter method for adding attachments
221
+	 *
222
+	 * @param string $path  the full path of the attachment
223
+	 * @param string $type  mime type of the file
224
+	 * @param string $title the title of the attachment (optional)
225
+	 *
226
+	 * @return object instantiated $this
227
+	 */
228
+	public function addAttachment($path, $type, $title = '')
229
+	{
230
+		array_push($this->attachments, array(
231
+		  'path' => $path,
232
+		  'type' => $type,
233
+		  'title' => $title,
234
+		));
235
+
236
+		return $this;
237
+	}
238
+
239
+	/**
240
+	 * The executing step, the actual sending of the email
241
+	 * First checks to make sure the minimum fields are set (returns false if they are not)
242
+	 * Second it attempts to send the mail with php's mail() (returns false if it fails)
243
+	 *
244
+	 * return boolean whether or not the email was valid & sent
245
+	 */
246
+	public function send()
247
+	{
248
+		if (!$this->checkRequiredFields()) {
249
+			return false;
250
+		}
251
+
252
+		$recipients = $this->buildTo();
253
+		$subject = $this->subject;
254
+		$message = $this->buildMessage();
255
+		$headers = $this->buildHeaders();
256
+
257
+		return mail($recipients, $subject, $message, $headers);
258
+	}
259
+
260
+	/**
261
+	 * Call to check the minimum required fields
262
+	 *
263
+	 * @return boolean whether or not the email meets the minimum required fields
264
+	 */
265
+	protected function checkRequiredFields()
266
+	{
267
+		if (empty($this->toAddresses)) {
268
+			return false;
269
+		}
270
+		if (empty($this->subject)) {
271
+			return false;
272
+		}
273
+
274
+		if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
275
+			return false;
276
+		}
277
+
278
+		return true;
279
+	}
280
+
281
+	/**
282
+	 * Build the recipients from 'to'
283
+	 *
284
+	 * @return string comma-separated lit of recipients
285
+	 */
286
+	protected function buildTo()
287
+	{
288
+		return implode(', ', $this->toAddresses);
289
+	}
290
+
291
+	/**
292
+	 * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see
293
+	 *
294
+	 * @return string email message
295
+	 */
296
+	protected function buildMessage()
297
+	{
298
+		$messageString = '';
300 299
         
301
-        if (!empty($this->attachments)) {
302
-            $messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
303
-        }
304
-        if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
305
-            if (!empty($this->attachments)) {
306
-                $messageString .= "Content-Type: multipart/alternative; boundary={$this->getAlternativeBoundary()}" . self::LINE_BREAK;
307
-                $messageString .= self::LINE_BREAK;
308
-            }
309
-            $messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
310
-            $messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
311
-            $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
312
-            $messageString .= self::LINE_BREAK;
313
-            $messageString .= $this->plainMessage;
314
-            $messageString .= self::LINE_BREAK;
315
-            $messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
316
-            $messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
317
-            $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
318
-            $messageString .= self::LINE_BREAK;
319
-            $messageString .= $this->htmlMessage;
320
-            $messageString .= self::LINE_BREAK;
321
-            $messageString .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK;
322
-            $messageString .= self::LINE_BREAK;
323
-        } elseif (!empty($this->plainMessage)) {
324
-            if (!empty($this->attachments)) {
325
-                $messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
326
-                $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
327
-                $messageString .= self::LINE_BREAK;
328
-            }
329
-            $messageString .= $this->plainMessage;
330
-            $messageString .= self::LINE_BREAK;
331
-        } elseif (!empty($this->htmlMessage)) {
332
-            if (!empty($this->attachments)) {
333
-                $messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
334
-                $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
335
-                $messageString .= self::LINE_BREAK;
336
-            }
337
-            $messageString .= $this->htmlMessage;
338
-            $messageString .= self::LINE_BREAK;
339
-        }
340
-        if (!empty($this->attachments)) {
341
-            foreach ($this->attachments as $attachment) {
342
-                $messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
343
-                $messageString .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK;
344
-                $messageString .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK;
345
-                $messageString .= 'Content-Disposition: attachment' . self::LINE_BREAK;
346
-                $messageString .= self::LINE_BREAK;
347
-                $messageString .= $this->buildAttachmentContent($attachment);
348
-                $messageString .= self::LINE_BREAK;
349
-            }
350
-            $messageString .= "--{$this->getBoundary()}--" . self::LINE_BREAK;
351
-        }
352
-        return $messageString;
353
-    }
354
-
355
-
356
-    /**
357
-     * Builder for the additional headers needed for multipart emails
358
-     *
359
-     * @return string headers needed for multipart
360
-     */
361
-    protected function buildHeaders()
362
-    {
363
-        $headerString = '';
364
-        foreach ($this->headers as $key => $value) {
365
-            $headerString .= sprintf('%s: %s', $key, $value) . self::LINE_BREAK;
366
-        }
367
-
368
-        if (!empty($this->ccAddresses)) {
369
-            $headerString .= 'CC: ' . implode(', ', $this->ccAddresses) . self::LINE_BREAK;
370
-        }
371
-        if (!empty($this->bccAddresses)) {
372
-            $headerString .= 'BCC: ' . implode(', ', $this->bccAddresses) . self::LINE_BREAK;
373
-        }
300
+		if (!empty($this->attachments)) {
301
+			$messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
302
+		}
303
+		if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
304
+			if (!empty($this->attachments)) {
305
+				$messageString .= "Content-Type: multipart/alternative; boundary={$this->getAlternativeBoundary()}" . self::LINE_BREAK;
306
+				$messageString .= self::LINE_BREAK;
307
+			}
308
+			$messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
309
+			$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
310
+			$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
311
+			$messageString .= self::LINE_BREAK;
312
+			$messageString .= $this->plainMessage;
313
+			$messageString .= self::LINE_BREAK;
314
+			$messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
315
+			$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
316
+			$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
317
+			$messageString .= self::LINE_BREAK;
318
+			$messageString .= $this->htmlMessage;
319
+			$messageString .= self::LINE_BREAK;
320
+			$messageString .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK;
321
+			$messageString .= self::LINE_BREAK;
322
+		} elseif (!empty($this->plainMessage)) {
323
+			if (!empty($this->attachments)) {
324
+				$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
325
+				$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
326
+				$messageString .= self::LINE_BREAK;
327
+			}
328
+			$messageString .= $this->plainMessage;
329
+			$messageString .= self::LINE_BREAK;
330
+		} elseif (!empty($this->htmlMessage)) {
331
+			if (!empty($this->attachments)) {
332
+				$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
333
+				$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
334
+				$messageString .= self::LINE_BREAK;
335
+			}
336
+			$messageString .= $this->htmlMessage;
337
+			$messageString .= self::LINE_BREAK;
338
+		}
339
+		if (!empty($this->attachments)) {
340
+			foreach ($this->attachments as $attachment) {
341
+				$messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
342
+				$messageString .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK;
343
+				$messageString .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK;
344
+				$messageString .= 'Content-Disposition: attachment' . self::LINE_BREAK;
345
+				$messageString .= self::LINE_BREAK;
346
+				$messageString .= $this->buildAttachmentContent($attachment);
347
+				$messageString .= self::LINE_BREAK;
348
+			}
349
+			$messageString .= "--{$this->getBoundary()}--" . self::LINE_BREAK;
350
+		}
351
+		return $messageString;
352
+	}
353
+
354
+
355
+	/**
356
+	 * Builder for the additional headers needed for multipart emails
357
+	 *
358
+	 * @return string headers needed for multipart
359
+	 */
360
+	protected function buildHeaders()
361
+	{
362
+		$headerString = '';
363
+		foreach ($this->headers as $key => $value) {
364
+			$headerString .= sprintf('%s: %s', $key, $value) . self::LINE_BREAK;
365
+		}
366
+
367
+		if (!empty($this->ccAddresses)) {
368
+			$headerString .= 'CC: ' . implode(', ', $this->ccAddresses) . self::LINE_BREAK;
369
+		}
370
+		if (!empty($this->bccAddresses)) {
371
+			$headerString .= 'BCC: ' . implode(', ', $this->bccAddresses) . self::LINE_BREAK;
372
+		}
374 373
         
375
-        if (!empty($this->attachments)) {
376
-            $headerString .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\"";
377
-        } elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
378
-            $headerString .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\"";
379
-        } elseif (!empty($this->htmlMessage)) {
380
-            $headerString .= 'Content-type: text/html; charset="iso-8859-1"';
381
-        }
382
-
383
-        return $headerString;
384
-    }
385
-
386
-    /**
387
-     * File reader for attachments
388
-     *
389
-     * @return string binary representation of file, base64'd
390
-     */
391
-    protected function buildAttachmentContent($attachment)
392
-    {
393
-        if (!file_exists($attachment['path'])) {
394
-            return ''; // todo log error
395
-        }
396
-
397
-        $handle = fopen($attachment['path'], 'r');
398
-        $contents = fread($handle, filesize($attachment['path']));
399
-        fclose($handle);
400
-
401
-        $contents = base64_encode($contents);
402
-        $contents = chunk_split($contents);
403
-        return $contents;
404
-    }
405
-
406
-    /**
407
-     * Holder for the boundry logic
408
-     * Not called/created unless it's needed
409
-     *
410
-     * @return  string  boundary
411
-     */
412
-    protected function getBoundary()
413
-    {
414
-        if (!isset($this->boundary)) {
415
-            $this->boundary = sprintf('PHP-mixed-%s', uniqid());
416
-        }
417
-        return $this->boundary;
418
-    }
419
-
420
-    /**
421
-     * Holder to create the alternative boundry logic
422
-     * Not called/created unless it's needed
423
-     *
424
-     * @return string alternative boundary
425
-     */
426
-    protected function getAlternativeBoundary()
427
-    {
428
-        if (!isset($this->alternativeBoundary)) {
429
-            $this->alternativeBoundary = sprintf('PHP-alternative-%s', uniqid());
430
-        }
431
-        return $this->alternativeBoundary;
432
-    }
374
+		if (!empty($this->attachments)) {
375
+			$headerString .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\"";
376
+		} elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
377
+			$headerString .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\"";
378
+		} elseif (!empty($this->htmlMessage)) {
379
+			$headerString .= 'Content-type: text/html; charset="iso-8859-1"';
380
+		}
381
+
382
+		return $headerString;
383
+	}
384
+
385
+	/**
386
+	 * File reader for attachments
387
+	 *
388
+	 * @return string binary representation of file, base64'd
389
+	 */
390
+	protected function buildAttachmentContent($attachment)
391
+	{
392
+		if (!file_exists($attachment['path'])) {
393
+			return ''; // todo log error
394
+		}
395
+
396
+		$handle = fopen($attachment['path'], 'r');
397
+		$contents = fread($handle, filesize($attachment['path']));
398
+		fclose($handle);
399
+
400
+		$contents = base64_encode($contents);
401
+		$contents = chunk_split($contents);
402
+		return $contents;
403
+	}
404
+
405
+	/**
406
+	 * Holder for the boundry logic
407
+	 * Not called/created unless it's needed
408
+	 *
409
+	 * @return  string  boundary
410
+	 */
411
+	protected function getBoundary()
412
+	{
413
+		if (!isset($this->boundary)) {
414
+			$this->boundary = sprintf('PHP-mixed-%s', uniqid());
415
+		}
416
+		return $this->boundary;
417
+	}
418
+
419
+	/**
420
+	 * Holder to create the alternative boundry logic
421
+	 * Not called/created unless it's needed
422
+	 *
423
+	 * @return string alternative boundary
424
+	 */
425
+	protected function getAlternativeBoundary()
426
+	{
427
+		if (!isset($this->alternativeBoundary)) {
428
+			$this->alternativeBoundary = sprintf('PHP-alternative-%s', uniqid());
429
+		}
430
+		return $this->alternativeBoundary;
431
+	}
433 432
 }
Please login to merge, or discard this patch.
tests/function-overrides.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -3,5 +3,5 @@
 block discarded – undo
3 3
 namespace Jacobemerick\Archangel;
4 4
 
5 5
 function uniqid() {
6
-    return '1234567890123';
6
+	return '1234567890123';
7 7
 }
Please login to merge, or discard this patch.
tests/ArchangelTest.php 1 patch
Indentation   +439 added lines, -439 removed lines patch added patch discarded remove patch
@@ -8,444 +8,444 @@
 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
-    }
11
+	public function testIsInstanceOfArchangel()
12
+	{
13
+		$archangel = new Archangel();
14
+
15
+		$this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
16
+	}
17 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 testSetLogger()
50
-    {
51
-        $logger = $this->getMock('Psr\Log\LoggerInterface');
52
-        $archangel = new Archangel();
53
-        $archangel->setLogger($logger);
54
-
55
-        $this->assertAttributeSame($logger, 'logger', $archangel);
56
-    }
57
-
58
-    public function testAddTo()
59
-    {
60
-        $archangel = new Archangel();
61
-        $archangel->addTo('[email protected]');
62
-
63
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
64
-    }
65
-
66
-    public function testAddToMultiple()
67
-    {
68
-        $archangel = new Archangel();
69
-        $archangel->addTo('[email protected]');
70
-        $archangel->addTo('[email protected]');
71
-
72
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
73
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
74
-    }
75
-
76
-    public function testAddToWithTitle()
77
-    {
78
-        $archangel = new Archangel();
79
-        $archangel->addTo('[email protected]', 'Mr. Test Alot');
80
-
81
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
82
-    }
83
-
84
-    public function testAddCc()
85
-    {
86
-        $archangel = new Archangel();
87
-        $archangel->addCc('[email protected]');
88
-
89
-        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
90
-    }
91
-
92
-    public function testAddCcMultiple()
93
-    {
94
-        $archangel = new Archangel();
95
-        $archangel->addCc('[email protected]');
96
-        $archangel->addCc('[email protected]');
97
-
98
-        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
99
-        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
100
-    }
101
-
102
-    public function testAddCcWithTitle()
103
-    {
104
-        $archangel = new Archangel();
105
-        $archangel->addCc('[email protected]', 'Mr. Test Alot');
106
-
107
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'ccAddresses', $archangel);
108
-    }
109
-
110
-    public function testAddBcc()
111
-    {
112
-        $archangel = new Archangel();
113
-        $archangel->addBcc('[email protected]');
114
-
115
-        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
116
-    }
117
-
118
-    public function testAddBccMultiple()
119
-    {
120
-        $archangel = new Archangel();
121
-        $archangel->addBcc('[email protected]');
122
-        $archangel->addBcc('[email protected]');
123
-
124
-        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
125
-        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
126
-    }
127
-
128
-    public function testAddBccWithTitle()
129
-    {
130
-        $archangel = new Archangel();
131
-        $archangel->addBcc('[email protected]', 'Mr. Test Alot');
132
-
133
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'bccAddresses', $archangel);
134
-    }
135
-
136
-    public function testSetFrom()
137
-    {
138
-        $archangel = new Archangel();
139
-        $archangel->setFrom('[email protected]');
140
-        $headersProperty = $this->getProtectedProperty('headers');
141
-        $headers = $headersProperty->getValue($archangel);
142
-
143
-        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
144
-    }
145
-
146
-    public function testSetFromMultiple()
147
-    {
148
-        $archangel = new Archangel();
149
-        $archangel->setFrom('[email protected]');
150
-        $archangel->setFrom('[email protected]');
151
-        $headersProperty = $this->getProtectedProperty('headers');
152
-        $headers = $headersProperty->getValue($archangel);
153
-
154
-        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
155
-        $this->assertNotContains('[email protected]', $headers);
156
-    }
157
-
158
-    public function testSetFromWithTitle()
159
-    {
160
-        $archangel = new Archangel();
161
-        $archangel->setFrom('[email protected]', 'Mr. Test Alot');
162
-        $headersProperty = $this->getProtectedProperty('headers');
163
-        $headers = $headersProperty->getValue($archangel);
164
-
165
-        $this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
166
-    }
167
-
168
-    public function testSetReplyTo()
169
-    {
170
-        $archangel = new Archangel();
171
-        $archangel->setReplyTo('[email protected]');
172
-        $headersProperty = $this->getProtectedProperty('headers');
173
-        $headers = $headersProperty->getValue($archangel);
174
-
175
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
176
-    }
177
-
178
-    public function testSetReplyToMultiple()
179
-    {
180
-        $archangel = new Archangel();
181
-        $archangel->setReplyTo('[email protected]');
182
-        $archangel->setReplyTo('[email protected]');
183
-        $headersProperty = $this->getProtectedProperty('headers');
184
-        $headers = $headersProperty->getValue($archangel);
185
-
186
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
187
-        $this->assertNotContains('[email protected]', $headers);
188
-    }
189
-
190
-    public function testSetReplyToWithTitle()
191
-    {
192
-        $archangel = new Archangel();
193
-        $archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
194
-        $headersProperty = $this->getProtectedProperty('headers');
195
-        $headers = $headersProperty->getValue($archangel);
196
-
197
-        $this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
198
-    }
199
-
200
-    public function testFormatEmailAddress()
201
-    {
202
-        $archangel = new Archangel();
203
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
204
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
205
-
206
-        $this->assertEquals('[email protected]', $formattedEmail);
207
-    }
208
-
209
-    public function testFormatEmailAddressWithTitle()
210
-    {
211
-        $archangel = new Archangel();
212
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
213
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
214
-
215
-        $this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
216
-    }
217
-
218
-    public function testSetSubject()
219
-    {
220
-        $archangel = new Archangel();
221
-        $archangel->setSubject('Test Subject');
222
-
223
-        $this->assertAttributeEquals('Test Subject', 'subject', $archangel);
224
-    }
225
-
226
-    public function testSetPlainMessage()
227
-    {
228
-        $archangel = new Archangel();
229
-        $archangel->setPlainMessage('Plain text message');
230
-
231
-        $this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
232
-    }
233
-
234
-    public function testSetHTMLMessage()
235
-    {
236
-        $archangel = new Archangel();
237
-        $archangel->setHTMLMessage('<p>An HTML message.</p>');
238
-
239
-        $this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
240
-    }
241
-
242
-    public function testAddAttachment()
243
-    {
244
-        $archangel = new Archangel();
245
-        $archangel->addAttachment('path', 'type');
246
-
247
-        $this->assertAttributeContains(
248
-            array('path' => 'path', 'type' => 'type', 'title' => ''),
249
-            'attachments',
250
-            $archangel
251
-        );
252
-    }
253
-
254
-    public function testAddAttachmentMultiple()
255
-    {
256
-        $archangel = new Archangel();
257
-        $archangel->addAttachment('pathOne', 'typeOne');
258
-        $archangel->addAttachment('pathTwo', 'typeTwo');
259
-
260
-        $this->assertAttributeContains(
261
-            array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
262
-            'attachments',
263
-            $archangel
264
-        );
265
-        $this->assertAttributeContains(
266
-            array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
267
-            'attachments',
268
-            $archangel
269
-        );
270
-    }
271
-
272
-    public function testAddAttachmentWithTitle()
273
-    {
274
-        $archangel = new Archangel();
275
-        $archangel->addAttachment('path', 'type', 'title');
276
-
277
-        $this->assertAttributeContains(
278
-            array('path' => 'path', 'type' => 'type', 'title' => 'title'),
279
-            'attachments',
280
-            $archangel
281
-        );
282
-    }
283
-
284
-    /**
285
-     * @dataProvider dataCheckRequiredFields
286
-     */
287
-    public function testCheckRequiredFields(
288
-        $expectedResult,
289
-        $toAddresses,
290
-        $subject,
291
-        $plainMessage,
292
-        $htmlMessage,
293
-        $attachments
294
-    ) {
295
-        $archangel = new Archangel();
296
-
297
-        if (!empty($toAddresses)) {
298
-            $toAddressesProperty = $this->getProtectedProperty('toAddresses');
299
-            $toAddressesProperty->setValue($archangel, $toAddresses);
300
-        }
301
-
302
-        if (!empty($subject)) {
303
-            $subjectProperty = $this->getProtectedProperty('subject');
304
-            $subjectProperty->setValue($archangel, $subject);
305
-        }
306
-
307
-        if (!empty($plainMessage)) {
308
-            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
309
-            $plainMessageProperty->setValue($archangel, $plainMessage);
310
-        }
311
-
312
-        if (!empty($htmlMessage)) {
313
-            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
314
-            $htmlMessageProperty->setValue($archangel, $htmlMessage);
315
-        }
316
-
317
-        if (!empty($attachments)) {
318
-            $attachmentsProperty = $this->getProtectedProperty('attachments');
319
-            $attachmentsProperty->setValue($archangel, $attachments);
320
-        }
321
-
322
-        $checkMethod = $this->getProtectedMethod('checkRequiredFields');
323
-        $isValid = $checkMethod->invoke($archangel);
324
-
325
-        if ($expectedResult == true) {
326
-            $this->assertTrue($isValid);
327
-            return;
328
-        }
329
-        $this->assertNotTrue($isValid);
330
-    }
331
-
332
-    public function dataCheckRequiredFields()
333
-    {
334
-        return array(
335
-            array(
336
-                'expectedResult' => false,
337
-                'toAddresses' => array(),
338
-                'subject' => '',
339
-                'plainMessage' => '',
340
-                'htmlMessage' => '',
341
-                'attachments' => array(),
342
-            ),
343
-            array(
344
-                'expectedResult' => false,
345
-                'toAddresses' => array('[email protected]'),
346
-                'subject' => '',
347
-                'plainMessage' => '',
348
-                'htmlMessage' => '',
349
-                'attachments' => array(),
350
-            ),
351
-            array(
352
-                'expectedResult' => false,
353
-                'toAddresses' => array('[email protected]'),
354
-                'subject' => 'Test Subject',
355
-                'plainMessage' => '',
356
-                'htmlMessage' => '',
357
-                'attachments' => array(),
358
-            ),
359
-            array(
360
-                'expectedResult' => false,
361
-                'toAddresses' => array(),
362
-                'subject' => 'Test Subject',
363
-                'plainMessage' => '',
364
-                'htmlMessage' => '',
365
-                'attachments' => array(),
366
-            ),
367
-            array(
368
-                'expectedResult' => false,
369
-                'toAddresses' => array(),
370
-                'subject' => 'Test Subject',
371
-                'plainMessage' => 'Plain text message',
372
-                'htmlMessage' => '',
373
-                'attachments' => array(),
374
-            ),
375
-            array(
376
-                'expectedResult' => true,
377
-                'toAddresses' => array('[email protected]'),
378
-                'subject' => 'Test Subject',
379
-                'plainMessage' => 'Plain text message',
380
-                'htmlMessage' => '',
381
-                'attachments' => array(),
382
-            ),
383
-            array(
384
-                'expectedResult' => true,
385
-                'toAddresses' => array('[email protected]'),
386
-                'subject' => 'Test Subject',
387
-                'plainMessage' => '',
388
-                'htmlMessage' => '<p>An HTML message.</p>',
389
-                'attachments' => array(),
390
-            ),
391
-            array(
392
-                'expectedResult' => true,
393
-                'toAddresses' => array('[email protected]'),
394
-                'subject' => 'Test Subject',
395
-                'plainMessage' => '',
396
-                'htmlMessage' => '',
397
-                'attachments' => array(
398
-                    array('path' => 'path', 'type' => 'type'),
399
-                ),
400
-            ),
401
-            array(
402
-                'expectedResult' => true,
403
-                'toAddresses' => array('[email protected]'),
404
-                'subject' => 'Test Subject',
405
-                'plainMessage' => 'Plain text message',
406
-                'htmlMessage' => '<p>An HTML message.</p>',
407
-                'attachments' => array(
408
-                    array('path' => 'path', 'type' => 'type'),
409
-                ),
410
-            ),
411
-       );
412
-    }
413
-
414
-    public function testGetBoundary()
415
-    {
416
-        $archangel = new Archangel();
417
-        $boundaryMethod = $this->getProtectedMethod('getBoundary');
418
-        $boundary = $boundaryMethod->invoke($archangel);
419
-        $expectedBoundary = sprintf('PHP-mixed-%s', uniqid());
420
-
421
-        $this->assertEquals($expectedBoundary, $boundary);
422
-    }
423
-
424
-    public function testGetAlternativeBoundary()
425
-    {
426
-        $archangel = new Archangel();
427
-        $boundaryMethod = $this->getProtectedMethod('getAlternativeBoundary');
428
-        $boundary = $boundaryMethod->invoke($archangel);
429
-        $expectedBoundary = sprintf('PHP-alternative-%s', uniqid());
430
-
431
-        $this->assertEquals($expectedBoundary, $boundary);
432
-    }
433
-
434
-    protected function getProtectedProperty($property)
435
-    {
436
-        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
437
-        $reflectedProperty = $reflectedArchangel->getProperty($property);
438
-        $reflectedProperty->setAccessible(true);
439
-
440
-        return $reflectedProperty;
441
-    }
442
-
443
-    protected function getProtectedMethod($method)
444
-    {
445
-        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
446
-        $reflectedMethod = $reflectedArchangel->getMethod($method);
447
-        $reflectedMethod->setAccessible(true);
448
-
449
-        return $reflectedMethod;
450
-    }
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 testSetLogger()
50
+	{
51
+		$logger = $this->getMock('Psr\Log\LoggerInterface');
52
+		$archangel = new Archangel();
53
+		$archangel->setLogger($logger);
54
+
55
+		$this->assertAttributeSame($logger, 'logger', $archangel);
56
+	}
57
+
58
+	public function testAddTo()
59
+	{
60
+		$archangel = new Archangel();
61
+		$archangel->addTo('[email protected]');
62
+
63
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
64
+	}
65
+
66
+	public function testAddToMultiple()
67
+	{
68
+		$archangel = new Archangel();
69
+		$archangel->addTo('[email protected]');
70
+		$archangel->addTo('[email protected]');
71
+
72
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
73
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
74
+	}
75
+
76
+	public function testAddToWithTitle()
77
+	{
78
+		$archangel = new Archangel();
79
+		$archangel->addTo('[email protected]', 'Mr. Test Alot');
80
+
81
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
82
+	}
83
+
84
+	public function testAddCc()
85
+	{
86
+		$archangel = new Archangel();
87
+		$archangel->addCc('[email protected]');
88
+
89
+		$this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
90
+	}
91
+
92
+	public function testAddCcMultiple()
93
+	{
94
+		$archangel = new Archangel();
95
+		$archangel->addCc('[email protected]');
96
+		$archangel->addCc('[email protected]');
97
+
98
+		$this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
99
+		$this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
100
+	}
101
+
102
+	public function testAddCcWithTitle()
103
+	{
104
+		$archangel = new Archangel();
105
+		$archangel->addCc('[email protected]', 'Mr. Test Alot');
106
+
107
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'ccAddresses', $archangel);
108
+	}
109
+
110
+	public function testAddBcc()
111
+	{
112
+		$archangel = new Archangel();
113
+		$archangel->addBcc('[email protected]');
114
+
115
+		$this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
116
+	}
117
+
118
+	public function testAddBccMultiple()
119
+	{
120
+		$archangel = new Archangel();
121
+		$archangel->addBcc('[email protected]');
122
+		$archangel->addBcc('[email protected]');
123
+
124
+		$this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
125
+		$this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
126
+	}
127
+
128
+	public function testAddBccWithTitle()
129
+	{
130
+		$archangel = new Archangel();
131
+		$archangel->addBcc('[email protected]', 'Mr. Test Alot');
132
+
133
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'bccAddresses', $archangel);
134
+	}
135
+
136
+	public function testSetFrom()
137
+	{
138
+		$archangel = new Archangel();
139
+		$archangel->setFrom('[email protected]');
140
+		$headersProperty = $this->getProtectedProperty('headers');
141
+		$headers = $headersProperty->getValue($archangel);
142
+
143
+		$this->assertArraySubset(array('From' => '[email protected]'), $headers);
144
+	}
145
+
146
+	public function testSetFromMultiple()
147
+	{
148
+		$archangel = new Archangel();
149
+		$archangel->setFrom('[email protected]');
150
+		$archangel->setFrom('[email protected]');
151
+		$headersProperty = $this->getProtectedProperty('headers');
152
+		$headers = $headersProperty->getValue($archangel);
153
+
154
+		$this->assertArraySubset(array('From' => '[email protected]'), $headers);
155
+		$this->assertNotContains('[email protected]', $headers);
156
+	}
157
+
158
+	public function testSetFromWithTitle()
159
+	{
160
+		$archangel = new Archangel();
161
+		$archangel->setFrom('[email protected]', 'Mr. Test Alot');
162
+		$headersProperty = $this->getProtectedProperty('headers');
163
+		$headers = $headersProperty->getValue($archangel);
164
+
165
+		$this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
166
+	}
167
+
168
+	public function testSetReplyTo()
169
+	{
170
+		$archangel = new Archangel();
171
+		$archangel->setReplyTo('[email protected]');
172
+		$headersProperty = $this->getProtectedProperty('headers');
173
+		$headers = $headersProperty->getValue($archangel);
174
+
175
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
176
+	}
177
+
178
+	public function testSetReplyToMultiple()
179
+	{
180
+		$archangel = new Archangel();
181
+		$archangel->setReplyTo('[email protected]');
182
+		$archangel->setReplyTo('[email protected]');
183
+		$headersProperty = $this->getProtectedProperty('headers');
184
+		$headers = $headersProperty->getValue($archangel);
185
+
186
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
187
+		$this->assertNotContains('[email protected]', $headers);
188
+	}
189
+
190
+	public function testSetReplyToWithTitle()
191
+	{
192
+		$archangel = new Archangel();
193
+		$archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
194
+		$headersProperty = $this->getProtectedProperty('headers');
195
+		$headers = $headersProperty->getValue($archangel);
196
+
197
+		$this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
198
+	}
199
+
200
+	public function testFormatEmailAddress()
201
+	{
202
+		$archangel = new Archangel();
203
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
204
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
205
+
206
+		$this->assertEquals('[email protected]', $formattedEmail);
207
+	}
208
+
209
+	public function testFormatEmailAddressWithTitle()
210
+	{
211
+		$archangel = new Archangel();
212
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
213
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
214
+
215
+		$this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
216
+	}
217
+
218
+	public function testSetSubject()
219
+	{
220
+		$archangel = new Archangel();
221
+		$archangel->setSubject('Test Subject');
222
+
223
+		$this->assertAttributeEquals('Test Subject', 'subject', $archangel);
224
+	}
225
+
226
+	public function testSetPlainMessage()
227
+	{
228
+		$archangel = new Archangel();
229
+		$archangel->setPlainMessage('Plain text message');
230
+
231
+		$this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
232
+	}
233
+
234
+	public function testSetHTMLMessage()
235
+	{
236
+		$archangel = new Archangel();
237
+		$archangel->setHTMLMessage('<p>An HTML message.</p>');
238
+
239
+		$this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
240
+	}
241
+
242
+	public function testAddAttachment()
243
+	{
244
+		$archangel = new Archangel();
245
+		$archangel->addAttachment('path', 'type');
246
+
247
+		$this->assertAttributeContains(
248
+			array('path' => 'path', 'type' => 'type', 'title' => ''),
249
+			'attachments',
250
+			$archangel
251
+		);
252
+	}
253
+
254
+	public function testAddAttachmentMultiple()
255
+	{
256
+		$archangel = new Archangel();
257
+		$archangel->addAttachment('pathOne', 'typeOne');
258
+		$archangel->addAttachment('pathTwo', 'typeTwo');
259
+
260
+		$this->assertAttributeContains(
261
+			array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
262
+			'attachments',
263
+			$archangel
264
+		);
265
+		$this->assertAttributeContains(
266
+			array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
267
+			'attachments',
268
+			$archangel
269
+		);
270
+	}
271
+
272
+	public function testAddAttachmentWithTitle()
273
+	{
274
+		$archangel = new Archangel();
275
+		$archangel->addAttachment('path', 'type', 'title');
276
+
277
+		$this->assertAttributeContains(
278
+			array('path' => 'path', 'type' => 'type', 'title' => 'title'),
279
+			'attachments',
280
+			$archangel
281
+		);
282
+	}
283
+
284
+	/**
285
+	 * @dataProvider dataCheckRequiredFields
286
+	 */
287
+	public function testCheckRequiredFields(
288
+		$expectedResult,
289
+		$toAddresses,
290
+		$subject,
291
+		$plainMessage,
292
+		$htmlMessage,
293
+		$attachments
294
+	) {
295
+		$archangel = new Archangel();
296
+
297
+		if (!empty($toAddresses)) {
298
+			$toAddressesProperty = $this->getProtectedProperty('toAddresses');
299
+			$toAddressesProperty->setValue($archangel, $toAddresses);
300
+		}
301
+
302
+		if (!empty($subject)) {
303
+			$subjectProperty = $this->getProtectedProperty('subject');
304
+			$subjectProperty->setValue($archangel, $subject);
305
+		}
306
+
307
+		if (!empty($plainMessage)) {
308
+			$plainMessageProperty = $this->getProtectedProperty('plainMessage');
309
+			$plainMessageProperty->setValue($archangel, $plainMessage);
310
+		}
311
+
312
+		if (!empty($htmlMessage)) {
313
+			$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
314
+			$htmlMessageProperty->setValue($archangel, $htmlMessage);
315
+		}
316
+
317
+		if (!empty($attachments)) {
318
+			$attachmentsProperty = $this->getProtectedProperty('attachments');
319
+			$attachmentsProperty->setValue($archangel, $attachments);
320
+		}
321
+
322
+		$checkMethod = $this->getProtectedMethod('checkRequiredFields');
323
+		$isValid = $checkMethod->invoke($archangel);
324
+
325
+		if ($expectedResult == true) {
326
+			$this->assertTrue($isValid);
327
+			return;
328
+		}
329
+		$this->assertNotTrue($isValid);
330
+	}
331
+
332
+	public function dataCheckRequiredFields()
333
+	{
334
+		return array(
335
+			array(
336
+				'expectedResult' => false,
337
+				'toAddresses' => array(),
338
+				'subject' => '',
339
+				'plainMessage' => '',
340
+				'htmlMessage' => '',
341
+				'attachments' => array(),
342
+			),
343
+			array(
344
+				'expectedResult' => false,
345
+				'toAddresses' => array('[email protected]'),
346
+				'subject' => '',
347
+				'plainMessage' => '',
348
+				'htmlMessage' => '',
349
+				'attachments' => array(),
350
+			),
351
+			array(
352
+				'expectedResult' => false,
353
+				'toAddresses' => array('[email protected]'),
354
+				'subject' => 'Test Subject',
355
+				'plainMessage' => '',
356
+				'htmlMessage' => '',
357
+				'attachments' => array(),
358
+			),
359
+			array(
360
+				'expectedResult' => false,
361
+				'toAddresses' => array(),
362
+				'subject' => 'Test Subject',
363
+				'plainMessage' => '',
364
+				'htmlMessage' => '',
365
+				'attachments' => array(),
366
+			),
367
+			array(
368
+				'expectedResult' => false,
369
+				'toAddresses' => array(),
370
+				'subject' => 'Test Subject',
371
+				'plainMessage' => 'Plain text message',
372
+				'htmlMessage' => '',
373
+				'attachments' => array(),
374
+			),
375
+			array(
376
+				'expectedResult' => true,
377
+				'toAddresses' => array('[email protected]'),
378
+				'subject' => 'Test Subject',
379
+				'plainMessage' => 'Plain text message',
380
+				'htmlMessage' => '',
381
+				'attachments' => array(),
382
+			),
383
+			array(
384
+				'expectedResult' => true,
385
+				'toAddresses' => array('[email protected]'),
386
+				'subject' => 'Test Subject',
387
+				'plainMessage' => '',
388
+				'htmlMessage' => '<p>An HTML message.</p>',
389
+				'attachments' => array(),
390
+			),
391
+			array(
392
+				'expectedResult' => true,
393
+				'toAddresses' => array('[email protected]'),
394
+				'subject' => 'Test Subject',
395
+				'plainMessage' => '',
396
+				'htmlMessage' => '',
397
+				'attachments' => array(
398
+					array('path' => 'path', 'type' => 'type'),
399
+				),
400
+			),
401
+			array(
402
+				'expectedResult' => true,
403
+				'toAddresses' => array('[email protected]'),
404
+				'subject' => 'Test Subject',
405
+				'plainMessage' => 'Plain text message',
406
+				'htmlMessage' => '<p>An HTML message.</p>',
407
+				'attachments' => array(
408
+					array('path' => 'path', 'type' => 'type'),
409
+				),
410
+			),
411
+	   );
412
+	}
413
+
414
+	public function testGetBoundary()
415
+	{
416
+		$archangel = new Archangel();
417
+		$boundaryMethod = $this->getProtectedMethod('getBoundary');
418
+		$boundary = $boundaryMethod->invoke($archangel);
419
+		$expectedBoundary = sprintf('PHP-mixed-%s', uniqid());
420
+
421
+		$this->assertEquals($expectedBoundary, $boundary);
422
+	}
423
+
424
+	public function testGetAlternativeBoundary()
425
+	{
426
+		$archangel = new Archangel();
427
+		$boundaryMethod = $this->getProtectedMethod('getAlternativeBoundary');
428
+		$boundary = $boundaryMethod->invoke($archangel);
429
+		$expectedBoundary = sprintf('PHP-alternative-%s', uniqid());
430
+
431
+		$this->assertEquals($expectedBoundary, $boundary);
432
+	}
433
+
434
+	protected function getProtectedProperty($property)
435
+	{
436
+		$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
437
+		$reflectedProperty = $reflectedArchangel->getProperty($property);
438
+		$reflectedProperty->setAccessible(true);
439
+
440
+		return $reflectedProperty;
441
+	}
442
+
443
+	protected function getProtectedMethod($method)
444
+	{
445
+		$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
446
+		$reflectedMethod = $reflectedArchangel->getMethod($method);
447
+		$reflectedMethod->setAccessible(true);
448
+
449
+		return $reflectedMethod;
450
+	}
451 451
 }
Please login to merge, or discard this patch.