1 | <?php |
||
10 | class RawMailContext extends RawDrupalContext { |
||
11 | |||
12 | /** |
||
13 | * The mail manager. |
||
14 | * |
||
15 | * @var \Drupal\DrupalMailManagerInterface |
||
16 | */ |
||
17 | protected $mailManager; |
||
18 | |||
19 | /** |
||
20 | * The number of mails received so far in this scenario, for each mail store. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $mailCount = []; |
||
25 | |||
26 | /** |
||
27 | * Get the mail manager service that handles stored test mail. |
||
28 | * |
||
29 | * @return \Drupal\DrupalMailManagerInterface |
||
30 | * The mail manager service. |
||
31 | */ |
||
32 | protected function getMailManager() { |
||
40 | |||
41 | /** |
||
42 | * Get collected mail, matching certain specifications. |
||
43 | * |
||
44 | * @param array $matches |
||
45 | * Associative array of mail fields and the values to filter by. |
||
46 | * @param bool $new |
||
47 | * Whether to ignore previously seen mail. |
||
48 | * @param null|int $index |
||
49 | * A particular mail to return, e.g. 0 for first or -1 for last. |
||
50 | * @param string $store |
||
51 | * The name of the mail store to get mail from. |
||
52 | * |
||
53 | * @return \stdClass[] |
||
54 | * An array of mail, each formatted as a Drupal 8 |
||
55 | * \Drupal\Core\Mail\MailInterface::mail $message array. |
||
56 | */ |
||
57 | protected function getMail($matches = [], $new = FALSE, $index = NULL, $store = 'default') { |
||
58 | $mail = $this->getMailManager()->getMail($store); |
||
59 | $previousMailCount = $this->getMailCount($store); |
||
60 | $this->mailCount[$store] = count($mail); |
||
61 | |||
62 | // Ignore previously seen mail. |
||
63 | if ($new) { |
||
64 | $mail = array_slice($mail, $previousMailCount); |
||
65 | } |
||
66 | |||
67 | // Filter mail based on $matches; keep only mail where each field mentioned |
||
68 | // in $matches contains the value specified for that field. |
||
69 | $mail = array_values(array_filter($mail, function ($singleMail) use ($matches) { |
||
70 | return ($this->matchesMail($singleMail, $matches)); |
||
71 | })); |
||
72 | |||
73 | // Return an individual mail if specified by an index. |
||
74 | if (is_null($index) || count($mail) === 0) { |
||
75 | return $mail; |
||
76 | } |
||
77 | else { |
||
78 | return array_slice($mail, $index, 1)[0]; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get the number of mails received in a particular mail store. |
||
84 | * |
||
85 | * @return int |
||
86 | * The number of mails received during this scenario. |
||
87 | */ |
||
88 | protected function getMailCount($store) { |
||
97 | |||
98 | /** |
||
99 | * Determine if a mail meets criteria. |
||
100 | * |
||
101 | * @param array $mail |
||
102 | * The mail, as an array of mail fields. |
||
103 | * @param array $matches |
||
104 | * The criteria: an associative array of mail fields and desired values. |
||
105 | * |
||
106 | * @return bool |
||
107 | * Whether the mail matches the criteria. |
||
108 | */ |
||
109 | protected function matchesMail($mail = [], $matches = []) { |
||
121 | |||
122 | /** |
||
123 | * Compare actual mail with expected mail. |
||
124 | * |
||
125 | * @param array $actualMail |
||
126 | * An array of actual mail. |
||
127 | * @param array $expectedMail |
||
128 | * An array of expected mail. |
||
129 | */ |
||
130 | protected function compareMail($actualMail, $expectedMail) { |
||
150 | |||
151 | /** |
||
152 | * Assert there is the expected number of mails, or that there are some mails |
||
153 | * if the exact number expected is not specified. |
||
154 | * |
||
155 | * @param array $actualMail |
||
156 | * An array of actual mail. |
||
157 | * @param int $expectedCount |
||
158 | * Optional. The number of mails expected. |
||
159 | */ |
||
160 | protected function assertMailCount($actualMail, $expectedCount = NULL) { |
||
182 | |||
183 | /** |
||
184 | * Sort mail by to, subject and body. |
||
185 | * |
||
186 | * @param array $mail |
||
187 | * An array of mail to sort. |
||
188 | * |
||
189 | * @return array |
||
190 | * The same mail, but sorted. |
||
191 | */ |
||
192 | protected function sortMail($mail) { |
||
229 | |||
230 | /** |
||
231 | * Get the mink context, so we can visit pages using the mink session. |
||
232 | */ |
||
233 | protected function getMinkContext() { |
||
240 | |||
241 | } |
||
242 |
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.