Completed
Push — master ( 987505...ce3c0b )
by Hamish
8s
created

SilverStripe/BehatExtension/Utility/TestMailer.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SilverStripe\BehatExtension\Utility;
4
5
/**
6
 * Same principle as core TestMailer class,
7
 * but saves emails in {@link TestSessionEnvironment}
8
 * to share the state between PHP calls (CLI vs. browser).
9
 */
10
class TestMailer extends \Mailer {
11
12
	/**
13
	 * @var TestSessionEnvironment
14
	 */
15
	protected $testSessionEnvironment;
16
17
	public function __construct() {
18
		$this->testSessionEnvironment = \Injector::inst()->get('TestSessionEnvironment');
19
	}
20
21
	/**
22
	 * Send a plain-text email.
23
	 * TestMailer will merely record that the email was asked to be sent, without sending anything.
24
	 */
25 View Code Duplication
	public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) {
26
		$this->saveEmail(array(
27
			'Type' => 'plain',
28
			'To' => $to,
29
			'From' => $from,
30
			'Subject' => $subject,
31
			'Content' => $plainContent,
32
			'PlainContent' => $plainContent,
33
			'AttachedFiles' => $attachedFiles,
34
			'CustomHeaders' => $customHeaders,
35
		));
36
37
		return true;
38
	}
39
	
40
	/**
41
	 * Send a multi-part HTML email
42
	 * TestMailer will merely record that the email was asked to be sent, without sending anything.
43
	 */
44 View Code Duplication
	public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customHeaders = false,
45
			$plainContent = false, $inlineImages = false) {
0 ignored issues
show
The parameter $inlineImages is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
47
		$this->saveEmail(array(
48
			'Type' => 'html',
49
			'To' => $to,
50
			'From' => $from,
51
			'Subject' => $subject,
52
			'Content' => $htmlContent,
53
			'PlainContent' => $plainContent,
54
			'AttachedFiles' => $attachedFiles,
55
			'CustomHeaders' => $customHeaders,
56
		));
57
58
		return true;
59
	}
60
	
61
	/**
62
	 * Clear the log of emails sent
63
	 */
64
	public function clearEmails() {
65
		$state = $this->testSessionEnvironment->getState();
66
		if(isset($state->emails)) unset($state->emails);
67
		$this->testSessionEnvironment->applyState($state);
68
	}
69
70
	/**
71
	 * Search for an email that was sent.
72
	 * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
73
	 * 
74
	 * @param $to
75
	 * @param $from
76
	 * @param $subject
77
	 * @param $content
78
	 * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles',
79
	 *               'customHeaders', 'htmlContent', 'inlineImages'
80
	 */
81
	public function findEmail($to = null, $from = null, $subject = null, $content = null) {
82
		$matches = $this->findEmails($to, $from, $subject, $content);
83
                //got the count of matches emails
84
                $emailCount = count($matches);
85
                //get the last(latest) one
86
		return $matches ? $matches[$emailCount-1] : null;
87
	}
88
89
	/**
90
	 * Search for all emails.
91
	 * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
92
	 * 
93
	 * @param $to
94
	 * @param $from
95
	 * @param $subject
96
	 * @param $content
97
	 * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles',
98
	 *               'customHeaders', 'htmlContent', 'inlineImages'
99
	 */
100
	public function findEmails($to = null, $from = null, $subject = null, $content = null) {
101
		$matches = array();
102
		$args = func_get_args();
103
		$state = $this->testSessionEnvironment->getState();
104
		$emails = isset($state->emails) ? $state->emails : array();
105
		foreach($emails as $email) {
106
			$matched = true;
107
108
			foreach(array('To','From','Subject','Content') as $i => $field) {
109
				if(!isset($email->$field)) continue;
110
				$value = (isset($args[$i])) ? $args[$i] : null;
111
				if($value) {
112
					if($value[0] == '/') $matched = preg_match($value, $email->$field);
113
					else $matched = ($value == $email->$field);
114
					if(!$matched) break;
115
				}
116
			}
117
			if($matched) $matches[] = $email;
118
		}
119
120
		return $matches;
121
	}
122
123
	protected function saveEmail($data) {
124
		$state = $this->testSessionEnvironment->getState();
125
		if(!isset($state->emails)) $state->emails = array();
126
		$state->emails[] = array_filter($data);
127
		$this->testSessionEnvironment->applyState($state);
128
	}
129
130
}
131