1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk/) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDev/ for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk/) |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WebinoDev\Test; |
11
|
|
|
|
12
|
|
|
use DirectoryIterator; |
13
|
|
|
use RegexIterator; |
14
|
|
|
use WebinoDev\Test\Exception\RuntimeException; |
15
|
|
|
use WebinoDev\Mail\Message; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* WebDriver test mail trait |
19
|
|
|
* |
20
|
|
|
* Use this trait when you want a mail testing. |
21
|
|
|
*/ |
22
|
|
|
trait MailTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Returns path to the mail directory |
26
|
|
|
*/ |
27
|
|
|
protected abstract function getMailDir(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Assert that no mail was sent |
31
|
|
|
* |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
protected function assertNoMail() |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$this->readMail(); |
38
|
|
|
} catch (\Exception $exc) { |
39
|
|
|
$this->assertSame('No mail found', $exc->getMessage()); |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->fail(); |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Read mail from temporary directory |
49
|
|
|
* |
50
|
|
|
* @return Message |
51
|
|
|
* @throws RuntimeException |
52
|
|
|
*/ |
53
|
|
|
protected function readMail() |
54
|
|
|
{ |
55
|
|
|
$files = []; |
56
|
|
|
foreach ($this->createMailDirIterator() as $fileInfo) { |
57
|
|
|
/* @var $fileInfo \SplFileInfo */ |
58
|
|
|
$files[$fileInfo->getFilename()] = $fileInfo->getPathname(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (empty($files)) { |
62
|
|
|
throw new RuntimeException('No mail found'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
ksort($files); |
66
|
|
|
|
67
|
|
|
$path = current($files); |
68
|
|
|
$message = Message::fromString(join(PHP_EOL, file($path, FILE_IGNORE_NEW_LINES))); |
69
|
|
|
|
70
|
|
|
unlink($path); |
71
|
|
|
return $message; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Wait for mail to read |
76
|
|
|
* |
77
|
|
|
* @return Message |
78
|
|
|
* @throws RuntimeException |
79
|
|
|
*/ |
80
|
|
|
protected function waitForMail() |
81
|
|
|
{ |
82
|
|
|
$tout = 30; |
83
|
|
|
$tn = 0; |
84
|
|
|
|
85
|
|
|
while (empty($mail)) { |
86
|
|
|
$tn++; |
87
|
|
|
sleep(1); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$mail = $this->readMail(); |
91
|
|
|
} catch (\Exception $exc) { |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($tn >= $tout) { |
96
|
|
|
throw new RuntimeException('Timeout exceeded'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $mail; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Checks no mail For a certain time |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
protected function waitAssertNoMail() |
109
|
|
|
{ |
110
|
|
|
$tout = 5; |
111
|
|
|
$tn = 0; |
112
|
|
|
|
113
|
|
|
while (1) { |
114
|
|
|
$tn++; |
115
|
|
|
sleep(1); |
116
|
|
|
|
117
|
|
|
$this->assertNoMail(); |
118
|
|
|
if ($this->hasFailed()) { |
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($tn >= $tout) { |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns mail directory iterator |
132
|
|
|
* |
133
|
|
|
* @return RegexIterator |
134
|
|
|
*/ |
135
|
|
|
protected function createMailDirIterator() |
136
|
|
|
{ |
137
|
|
|
return new RegexIterator(new DirectoryIterator($this->getMailDir()), '~ZendMail_~'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.