1 | <?php |
||
10 | class RawMailContext extends RawDrupalContext |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The mail manager. |
||
15 | * |
||
16 | * @var \Drupal\DrupalMailManagerInterface |
||
17 | */ |
||
18 | protected $mailManager; |
||
19 | |||
20 | /** |
||
21 | * The number of mails received so far in this scenario, for each mail store. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $mailCount = []; |
||
26 | |||
27 | /** |
||
28 | * Get the mail manager service that handles stored test mail. |
||
29 | * |
||
30 | * @return \Drupal\DrupalMailManagerInterface |
||
31 | * The mail manager service. |
||
32 | */ |
||
33 | protected function getMailManager() |
||
34 | { |
||
35 | // Persist the mail manager between invocations. This is necessary for |
||
36 | // remembering and reinstating the original mail backend. |
||
37 | if (is_null($this->mailManager)) { |
||
38 | $this->mailManager = new DrupalMailManager($this->getDriver()); |
||
39 | } |
||
40 | return $this->mailManager; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get collected mail, matching certain specifications. |
||
45 | * |
||
46 | * @param array $matches |
||
47 | * Associative array of mail fields and the values to filter by. |
||
48 | * @param bool $new |
||
49 | * Whether to ignore previously seen mail. |
||
50 | * @param null|int $index |
||
51 | * A particular mail to return, e.g. 0 for first or -1 for last. |
||
52 | * @param string $store |
||
53 | * The name of the mail store to get mail from. |
||
54 | * |
||
55 | * @return \stdClass[] |
||
56 | * An array of mail, each formatted as a Drupal 8 |
||
57 | * \Drupal\Core\Mail\MailInterface::mail $message array. |
||
58 | */ |
||
59 | protected function getMail($matches = [], $new = false, $index = null, $store = 'default') |
||
60 | { |
||
61 | $mail = $this->getMailManager()->getMail($store); |
||
62 | $previousMailCount = $this->getMailCount($store); |
||
63 | $this->mailCount[$store] = count($mail); |
||
64 | |||
65 | // Ignore previously seen mail. |
||
66 | if ($new) { |
||
67 | $mail = array_slice($mail, $previousMailCount); |
||
68 | } |
||
69 | |||
70 | // Filter mail based on $matches; keep only mail where each field mentioned |
||
71 | // in $matches contains the value specified for that field. |
||
72 | $mail = array_values(array_filter($mail, function ($singleMail) use ($matches) { |
||
73 | return ($this->matchesMail($singleMail, $matches)); |
||
74 | })); |
||
75 | |||
76 | // Return an individual mail if specified by an index. |
||
77 | if (is_null($index) || count($mail) === 0) { |
||
78 | return $mail; |
||
79 | } else { |
||
80 | return array_slice($mail, $index, 1)[0]; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get the number of mails received in a particular mail store. |
||
86 | * |
||
87 | * @return int |
||
88 | * The number of mails received during this scenario. |
||
89 | */ |
||
90 | protected function getMailCount($store) |
||
91 | { |
||
92 | if (array_key_exists($store, $this->mailCount)) { |
||
93 | $count = $this->mailCount[$store]; |
||
94 | } else { |
||
95 | $count = 0; |
||
96 | } |
||
97 | return $count; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Determine if a mail meets criteria. |
||
102 | * |
||
103 | * @param array $mail |
||
104 | * The mail, as an array of mail fields. |
||
105 | * @param array $matches |
||
106 | * The criteria: an associative array of mail fields and desired values. |
||
107 | * |
||
108 | * @return bool |
||
109 | * Whether the mail matches the criteria. |
||
110 | */ |
||
111 | protected function matchesMail($mail = [], $matches = []) |
||
112 | { |
||
113 | // Discard criteria that are just zero-length strings. |
||
114 | $matches = array_filter($matches, 'strlen'); |
||
115 | // For each criteria, check the specified mail field contains the value. |
||
116 | foreach ($matches as $field => $value) { |
||
117 | // Case insensitive. |
||
118 | if (stripos($mail[$field], $value) === false) { |
||
119 | return false; |
||
120 | } |
||
121 | } |
||
122 | return true; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Compare actual mail with expected mail. |
||
127 | * |
||
128 | * @param array $actualMail |
||
129 | * An array of actual mail. |
||
130 | * @param array $expectedMail |
||
131 | * An array of expected mail. |
||
132 | */ |
||
133 | protected function compareMail($actualMail, $expectedMail) |
||
134 | { |
||
135 | // Make sure there is the same number of actual and expected. |
||
136 | $expectedCount = count($expectedMail); |
||
137 | $this->assertMailCount($actualMail, $expectedCount); |
||
138 | |||
139 | // For each row of expected mail, check the corresponding actual mail. |
||
140 | // Make the comparison insensitive to the order mails were sent. |
||
141 | $actualMail = $this->sortMail($actualMail); |
||
142 | $expectedMail = $this->sortMail($expectedMail); |
||
143 | foreach ($expectedMail as $index => $expectedMailItem) { |
||
144 | // For each column of the expected, check the field of the actual mail. |
||
145 | foreach ($expectedMailItem as $fieldName => $fieldValue) { |
||
146 | $expectedField = [$fieldName => $fieldValue]; |
||
147 | $match = $this->matchesMail($actualMail[$index], $expectedField); |
||
148 | if (!$match) { |
||
149 | throw new \Exception(sprintf("The #%s mail did not have '%s' in its %s field. It had:\n'%s'", $index, $fieldValue, $fieldName, mb_strimwidth($actualMail[$index][$fieldName], 0, 30, "..."))); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Assert there is the expected number of mails, or that there are some mails |
||
157 | * if the exact number expected is not specified. |
||
158 | * |
||
159 | * @param array $actualMail |
||
160 | * An array of actual mail. |
||
161 | * @param int $expectedCount |
||
162 | * Optional. The number of mails expected. |
||
163 | */ |
||
164 | protected function assertMailCount($actualMail, $expectedCount = null) |
||
186 | |||
187 | /** |
||
188 | * Sort mail by to, subject and body. |
||
189 | * |
||
190 | * @param array $mail |
||
191 | * An array of mail to sort. |
||
192 | * |
||
193 | * @return array |
||
194 | * The same mail, but sorted. |
||
195 | */ |
||
196 | protected function sortMail($mail) |
||
234 | |||
235 | /** |
||
236 | * Get the mink context, so we can visit pages using the mink session. |
||
237 | */ |
||
238 | protected function getMinkContext() |
||
246 | } |
||
247 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.