Completed
Push — develop ( 206b96...63ef56 )
by Peter
01:59
created

MailTrait::waitAssertNoMail()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 0
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
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()) {
0 ignored issues
show
Bug introduced by
It seems like hasFailed() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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