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() |
||
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') |
||
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) |
||
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 = []) |
||
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) |
||
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.